1Function
函数式接口
1.1基本用法
抽象接口
apply
—将此函数应用于给定的参数
默认实现接口
compose
—将compose
传入函数的返参作为当前函数的入参andThen
—将当前函数的返参作为andThen
的入参
1.2 compose方法
public class FunctionTest {
public static void main(String[] args) {
FunctionTest functionTest = new FunctionTest();
System.out.println(functionTest.compute(1, value -> 2 * value));
System.out.println(functionTest.compute(2, value -> 5 + value));
System.out.println(functionTest.compute(3, value -> value * value));
System.out.println(functionTest.convert(4, value -> value + " convert"));
//
Function<Integer, Integer> function = value -> 2 * value;
System.out.println(functionTest.compute(1, function));
}
public int compute(int a, Function<Integer, Integer> function){
int result = function.apply(a);
return result;
}
public String convert(int a, Function<Integer, String> function){
return function.apply(a);
}
}
高阶函数:
—接收一个函数作为参数,或者将一个函数作为返回值的函数
1.3 compose方法
示例
public class FunctionTest2 {
public int composeTest(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.compose(function2).apply(a);
}
public static void main(String[] args) {
FunctionTest2 functionTest2 = new FunctionTest2();
System.out.println(functionTest2.composeTest