从Function的定义中可以看到,它其实是一个接口,可以接收一个泛型的输入参数,和一个泛型的返回参数,使用也很简单,可以看下面的demo
/**
* @param num
* @param function
* @return
* @desc 使用JDK8 Function函数
*/
private Integer compute(Integer num, Function<Integer, Integer> function) {
Integer result = function.apply(num);
return result;
}
String calculatePlus(Integer num1, Integer num2, BiFunction<Integer,Integer,String> biFunction){
//这里按照声明的BiFunction 传入2个Integer类型的形参,返回一个String类型的返回值
String result = biFunction.apply(num1,num2);
System.out.println("闭包的返回值"+result);
return result.concat("Hello World");
}
使用compose函数,简单的说,就是从右向左处理
@Test
public void contextLoads() {
System.out.println(
calculatePlusPlus(2,"加班啊",(value1) -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是function1 " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " value = " + value1);
return value1 + value1;
}, (value2) -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是function2 " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " value = " + value2);
return String.valueOf(value2 + value2);
})
);
}
String calculatePlusPlus(Integer num1, String str1, Function<Integer,Integer> function1, Function<Integer,String> function2){
//这里 function2 会把 function1 返回的 Integer 类型的返回值作为输入值,然后参与 function2 自己的闭包运算,后面的那个 apply(num1) 其实是function1的输入
String function2Result = function2.compose(function1).apply(num1);
/* //还能这里的执行顺序的从后往前的,先执行最后的 function1 再执行 倒数第二个 function1 最后执行 function2
//为什么呢,主要是 compose 方法里先执行的传入的参数的 apply
String function2Result = function2.compose(function1).compose(function1).apply(num1);*/
System.out.println("闭包的返回值" + function2Result);
return function2Result.concat(" Hello World");
}
@Test
public void contextLoads() {
System.out.println(
calculatePlusPlusPlus(2,"加班啊",(value1) -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是function3 " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " value = " + value1);
return String.valueOf(value1 + value1);
}, (value2) -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是function4 " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " value = " + value2);
return value2 + value2;
})
);
}
/**
* andThen 从左向右执行
* @param num1
* @param str1
* @param function3
* @param function4
* @return
*/
String calculatePlusPlusPlus(Integer num1, String str1, Function<Integer,String> function3, Function<Integer,Integer> function4){
//这里 function3 会把 function4 返回的 Integer 类型的返回值作为输入值,然后参与 function3 自己的闭包运算,后面的那个 apply(num1) 其实是 function4 的输入
String function2Result = function4.andThen(function3).apply(num1);
System.out.println("闭包的返回值" + function2Result);
return function2Result.concat(" Hello World");
}
整体demo的代码
package com.felix.event;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.function.BiFunction;
import java.util.function.Function;
@SpringBootTest
public class EventApplicationTests {
@Test
public void contextLoads() {
System.out.println(calculate(6,(value) ->{
return value + value;
}));
System.out.println(calculatePlus(1,2,(value1,value2) -> {
return String.valueOf(value1 * value2);
}));
System.out.println(
calculatePlusPlus(2,"加班啊",(value1) -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是function1 " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " value = " + value1);
return value1 + value1;
}, (value2) -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是function2 " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " value = " + value2);
return String.valueOf(value2 + value2);
})
);
System.out.println(
calculatePlusPlusPlus(2,"加班啊",(value1) -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是function3 " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " value = " + value1);
return String.valueOf(value1 + value1);
}, (value2) -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我是function4 " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " value = " + value2);
return value2 + value2;
})
);
}
/**
* andThen 从左向右执行
* @param num1
* @param str1
* @param function3
* @param function4
* @return
*/
String calculatePlusPlusPlus(Integer num1, String str1, Function<Integer,String> function3, Function<Integer,Integer> function4){
//这里 function3 会把 function4 返回的 Integer 类型的返回值作为输入值,然后参与 function3 自己的闭包运算,后面的那个 apply(num1) 其实是 function4 的输入
String function2Result = function4.andThen(function3).apply(num1);
System.out.println("闭包的返回值" + function2Result);
return function2Result.concat(" Hello World");
}
/**
* compose 从右向左执行
* @param num1
* @param str1
* @param function1
* @param function2
* @return
*/
String calculatePlusPlus(Integer num1, String str1, Function<Integer,Integer> function1, Function<Integer,String> function2){
//这里 function2 会把 function1 返回的 Integer 类型的返回值作为输入值,然后参与 function2 自己的闭包运算,后面的那个 apply(num1) 其实是function1的输入
String function2Result = function2.compose(function1).apply(num1);
/* //还能这里的执行顺序的从后往前的,先执行最后的 function1 再执行 倒数第二个 function1 最后执行 function2
//为什么呢,主要是 compose 方法里先执行的传入的参数的 apply
String function2Result = function2.compose(function1).compose(function1).apply(num1);*/
System.out.println("闭包的返回值" + function2Result);
return function2Result.concat(" Hello World");
}
Integer calculate(Integer num, Function<Integer,Integer> function){
//这里按照声明的Function 传入1个 Integer 类型的形参,返回一个 Integer 类型的返回值
Integer result = function.apply(num);
System.out.println("闭包的返回值"+result);
return result + result;
}
String calculatePlus(Integer num1, Integer num2, BiFunction<Integer,Integer,String> biFunction){
//这里按照声明的BiFunction 传入2个Integer类型的形参,返回一个String类型的返回值
String result = biFunction.apply(num1,num2);
System.out.println("闭包的返回值"+result);
return result.concat("Hello World");
}
}
参考自大佬的博文
https://www.cnblogs.com/webor2006/p/8204591.html