Function
- Function作爲一個函數式接口,主要方法apply接收一個參數,返回一個值
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
}
首先我們來寫一個計算數字的方法
public int compute(int a, Function<Integer, Integer> function) {
int result = function.apply(a);
return result;
}
然後我們調用這個方法
test.compute(5,value -> value * value) //25 計算平方
test.compute(5,value -> value + value) //10 求和
test.compute(5,value -> value - 2) //3
可以看到我們定義一個方法就可以實現多種功能,這就是前面說過的Lambda表達式傳遞的是一種行爲,我們把想要做的事在調用的時候,以一種行爲的方式傳遞進來,程序讀起來也更加直觀
- Function compose方法
compose方法是一個默認方法,這個方法接收一個function作爲參數,將參數function執行的結果作爲參數給調用的function,以此來實現兩個function組合的功能。
public interface Function<T, R> {
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
}
下面我們來舉例看一下:
public int compute(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.compose(function2).apply(a);
}
調用這個方法:
test.compute(2, value -> value * 3, value -> value * value)
大家可以猜一下上面的結果是多少?
我們來分析一下:
function1.compose(function2).apply(a);
compose方法內部代碼是:
return (V v) -> apply(before.apply(v));
返回的是一個Function,輸入一個參數,返回一個參數值,這個Function 在調用apply時首先執行的是 before.apply(v)
before在這裏就是value -> value * value
,也就是 2*2,將得到的結果4,作爲參數傳遞給function1,在這裏就是value -> value * 3
,所以結果是:12
3.Function andThen
瞭解了compose方法,我們再來看andThen方法就好理解了,聽名字就是“接下來”,andThen方法也是接收一個function作爲參數,與compse不同的是,先執行本身的apply方法,將執行的結果作爲參數給參數中的function。
public interface Function<T, R> {
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
}
下面我們來舉例看一下:
public int compute2(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.andThen(function2).apply(a);
}
調用這個方法:
test.compute2(2, value -> value * 3, value -> value * value)
這次結果是多少呢?我想大家應該很容易就知道了,首先執行的是 value -> value * 3
結果是6,然後執行的是 value * value
最終結果是36
BiFunction
瞭解了Function以後,有的小夥伴可能會問了,Function只能接收一個參數,如果我要傳遞兩個參數呢,這一點Java8也替我們考慮到了,就是我們截下來要講到的 BiFunction,首先還是直接上源碼:
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t, U u) -> after.apply(apply(t, u));
}
}
可以看到BiFunction的apply方法,接收兩個參數,返回一個值,來看一個例子
public int compute3(int a, int b, BiFunction<Integer, Integer, Integer> biFunction) {
return biFunction.apply(a, b);
}
我們定義了一個方法,可以用來計算兩個數的很多運算
test.compute3(2, 3, (v1, v2) -> v1 + v2) //5
test.compute3(2, 3, (v1, v2) -> v1 - v2) //-1
test.compute3(2, 3, (v1, v2) -> v1 * v2) //6
這個還是蠻簡單的,我們再來看一下,BiFunction中有一個andThen方法,參數是一個Function,方法主要是將BiFunction返回的結果作爲Function的參數,得出一個結果,舉例:
public int compute4(int a, int b, BiFunction<Integer, Integer, Integer> biFunction,Function<Integer, Integer> function) {
return biFunction.andThen(function).apply(a, b);
}
test.compute4(2, 3, (v1, v2) -> v1 + v2, v1 -> v1 * v1)
首先執行(v1, v2) -> v1 + v2,然後執行 v1 -> v1 * v1。
有的同學可能會問爲什麼BiFunction沒有compose方法呢,大家仔細想一想,如果有compose方法的話,那就是先執行Function的apply方法,但是執行完畢後只返回一個參數,而BiFunction需要兩個參數,所以肯定是不行的。