1 Function<T, R>
中的T, R表示接口輸入、輸出的數(shù)據(jù)類型搓译。
-
R apply(T t)
public class FunctionTest {
public static void main(String[] args) {
FunctionTest functionTest = new FunctionTest();
// return e + 5;就是apply方法的具體實(shí)現(xiàn)
Function<Integer, Integer> func = e -> {return e + 5;};
int result = functionTest.calculate(5, func);
System.out.println(result);
}//對單個(gè)參數(shù)進(jìn)行抽象的處理,而具體的處理邏輯在調(diào)用時(shí)傳入:即傳遞的是一種操作 public int calculate(Integer a, Function<Integer, Integer> function) { return function.apply(a); }
func
是定義好的Function
接口類型的變量脐湾,他的輸入诚撵、輸出都是Integer
類型。調(diào)用
calculate
方法時(shí)颤枪,將func
作為參數(shù)傳入,對參數(shù)5
進(jìn)行處理淑际。因此畏纲,
Function
接口抽象出了一種對單個(gè)參數(shù)進(jìn)行處理的操作。而具體的處理邏輯在調(diào)用時(shí)傳入:即傳遞的是一種操作春缕。-
andThen
- 先處理參數(shù)盗胀,再對返回值使用
操作after
進(jìn)行處理。
Function<Integer, Integer> func = e -> {return e + 5;};
Function<Integer, Integer> func2 = e -> {return e * 5;};
//func2即after
func.andThen(func2).apply(5); // 50
- 先處理參數(shù)盗胀,再對返回值使用
-
compose
- 和
andThen
剛好相反:先使用操作before
處理參數(shù)锄贼,再對返回值進(jìn)行處理票灰。
Function<Integer, Integer> func = e -> {return e + 5;};
Function<Integer, Integer> func2 = e -> {return e * 5;};
//func2即before
func.compose(func2).apply(5); // 30 -
compose
源碼
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));//第一個(gè)apply是調(diào)用當(dāng)前接口的方法
} - 注意
compose
方法的返回值依然是Function<T, R>
類型,所以不是
return this.apply(before.apply(v));
- 和
-
擴(kuò)展組合Function
public class FunctionTest2 { public static void main(String[] args) { FunctionTest2 functionTest2 = new FunctionTest2(); int result1 = functionTest2.compute(5, e -> e * 5, e -> e + 5); int result2 = functionTest2.compute2(5, e -> e * 5, e -> e + 5); int result3 = functionTest2.compute3(5, e -> e * 5, e -> e + 5); int result4 = functionTest2.compute4(5, e -> e * 5, e -> e + 5); System.out.println(result1);//50 System.out.println(result2);//30 System.out.println(result3);//130 System.out.println(result4);//250 } public int compute(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) { return function1.compose(function2).apply(source); } public int compute2(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) { return function1.andThen(function2).apply(source); } public int compute3(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) { return function1.andThen(function2).compose(function1).apply(source); //從后往前 } public int compute4(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) { return function1.compose(function2).andThen(function1).apply(source); } }
2 BiFunction<T, U, R>
中的T, U表示接口輸入的第一、第二個(gè)參數(shù)屑迂、R是輸出的數(shù)據(jù)類型浸策。
public class BIFunctionTest {
public static void main(String[] args) {
//定義BiFunction對象,并調(diào)用apply方法
BiFunction<Integer, String, Integer> biFunction = (t, u) -> {
return t + Integer.parseInt(u);
};
System.out.println(biFunction.apply(5, "6")); //11
System.out.println("------------------------------------------");
//方法調(diào)用時(shí)惹盼,作為參數(shù)(操作)傳入
BIFunctionTest biFunctionTest = new BIFunctionTest();
int result = biFunctionTest.compute(5, 6, (t, u) -> {return t + u;});
System.out.println(result); //11
//BiFunction與Function的組合
int result2 = biFunctionTest.compute2(5, 6, (t, u) -> {return t + u;}, t -> t * t);
System.out.println(result2); //121
}
public int compute(Integer a, Integer b, BiFunction<Integer, Integer, Integer> biFunction) {
return biFunction.apply(a, b);
}
//BIFunction的返回作為Function的輸入
public int compute2(Integer a, Integer b, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function) {
return biFunction.andThen(function).apply(a, b);
}}
- 注意庸汗,
BiFunction
中沒有compose
默認(rèn)函數(shù)。