Function
Function作為一個函數(shù)式接口严衬,主要方法apply接收一個參數(shù),返回一個值
@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);
}
首先我們來寫一個計算數(shù)字的方法
public int compute(int a, Function<Integer, Integer> function) {
int result = function.apply(a);
return result;
}
然后我們調(diào)用這個方法
test.compute(5,value -> value * value) //25 計算平方
test.compute(5,value -> value + value) //10 求和
test.compute(5,value -> value - 2) //3
可以看到我們定義一個方法就可以實現(xiàn)多種功能枫慷,這就是前面說過的Lambda表達式傳遞的是一種行為夜牡,我們把想要做的事在調(diào)用的時候湖雹,以一種行為的方式傳遞進來勤庐,程序讀起來也更加直觀
Function compose方法
compose方法是一個默認(rèn)方法嗜桌,這個方法接收一個function作為參數(shù)奥溺,將參數(shù)function執(zhí)行的結(jié)果作為參數(shù)給調(diào)用的function,以此來實現(xiàn)兩個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);
}
調(diào)用這個方法:
test.compute(2, value -> value * 3, value -> value * value)
大家可以猜一下上面的結(jié)果是多少谚赎?
我們來分析一下:
function1.compose(function2).apply(a);
compose方法內(nèi)部代碼是:
return (V v) -> apply(before.apply(v));
返回的是一個Function,輸入一個參數(shù)诱篷,返回一個參數(shù)值壶唤,這個Function 在調(diào)用apply時首先執(zhí)行的是 before.apply(v) before在這里就是value -> value * value,也就是 2*2棕所,將得到的結(jié)果4闸盔,作為參數(shù)傳遞給function1,在這里就是value -> value * 3 琳省,所以結(jié)果是:12
3.Function andThen
了解了compose方法迎吵,我們再來看andThen方法就好理解了,聽名字就是“接下來”针贬,andThen方法也是接收一個function作為參數(shù)击费,與compse不同的是,先執(zhí)行本身的apply方法桦他,將執(zhí)行的結(jié)果作為參數(shù)給參數(shù)中的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);
}
調(diào)用這個方法:
test.compute2(2, value -> value * 3, value -> value * value)
這次結(jié)果是多少呢?我想大家應(yīng)該很容易就知道了快压,首先執(zhí)行的是 value -> value * 3 結(jié)果是6圆仔,然后執(zhí)行的是 value * value 最終結(jié)果是36
BiFunction
了解了Function以后,有的小伙伴可能會問了蔫劣,F(xiàn)unction只能接收一個參數(shù)坪郭,如果我要傳遞兩個參數(shù)呢,這一點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方法歪沃,接收兩個參數(shù),返回一個值嫌松,來看一個例子
public int compute3(int a, int b, BiFunction<Integer, Integer, Integer> biFunction) {
return biFunction.apply(a, b);
}
我們定義了一個方法沪曙,可以用來計算兩個數(shù)的很多運算
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方法珊蟀,參數(shù)是一個Function,方法主要是將BiFunction返回的結(jié)果作為Function的參數(shù),得出一個結(jié)果育灸,舉例:
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)
首先執(zhí)行(v1, v2) -> v1 + v2腻窒,然后執(zhí)行 v1 -> v1 * v1。
有的同學(xué)可能會問為什么BiFunction沒有compose方法呢磅崭,大家仔細(xì)想一想儿子,如果有compose方法的話,那就是先執(zhí)行Function的apply方法砸喻,但是執(zhí)行完畢后只返回一個參數(shù)柔逼,而BiFunction需要兩個參數(shù),所以肯定是不行的割岛。