/*** Represents a function that accepts one argument and produces a result.** <p>This is a <a href="package-summary.html">functional interface</a>* whose functional method is {@link #apply(Object)}.** @param <T> the type of the input to the function* @param <R> the type of the result of the function** @since 1.8*/
@FunctionalInterface
public interface Function<T, R> {R apply(T t);/*** @return a composed function that first applies the {@code before}* function and then applies this function*/default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v));}/*** @return a composed function that first applies this function and then* applies the {@code after} function*/default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {Objects.requireNonNull(after);return (T t) -> after.apply(apply(t));}/*** 省略*/
}
public class FunctionTest {public static void main(String[] args) {FunctionTest functionTest = new FunctionTest();System.out.println(functionTest.compute1(5,i -> i * 2,i -> i * i));//50System.out.println(functionTest.compute2(5,i -> i * 2,i -> i * i));//100}public int compute1(int i, Function<Integer,Integer> after,Function<Integer,Integer> before){return after.compose(before).apply(i);}public int compute2(int i, Function<Integer,Integer> before,Function<Integer,Integer> after){return before.andThen(after).apply(i);}
}
定義了compute1和compute2兩個方法,compute1方法第一個參數是要計算的數據,第二個參數是后執行的函數,第一個是先執行的函數,因為輸入輸出都是數字類型,所以泛型都指定為Integer類型,通過after.compose(before);將兩個函數串聯起來然后執行組合后的Funtion方法apply(i)。當調用compute1(5,i -> i 2,i -> i i)時,先平方再乘以2所以結果是50。而compute2方法對兩個Function的調用正好相反,所以結果是100。
BiFunction接口 接下來繼續看下另一個很常用的函數式接口BiFunction
/*** This is the two-arity specialization of {@link Function}.* @param <T> the type of the first argument to the function* @param <U> the type of the second argument to the function* @param <R> the type of the result of the function** @see Function* @since 1.8*/
@FunctionalInterface
public interface BiFunction<T, U, R> {/*** Applies this function to the given arguments.** @param t the first function argument* @param u the second function argument* @return the function result*/R apply(T t, U u);/*** Returns a composed function that first applies this function to* its input, and then applies the {@code after} function to the result.* If evaluation of either function throws an exception, it is relayed to* the caller of the composed function.** @param <V> the type of output of the {@code after} function, and of the* composed function* @param after the function to apply after this function is applied* @return a composed function that first applies this function and then* applies the {@code after} function* @throws NullPointerException if after is null*/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));}
}
實際上就是可以有兩個參數的Function,同樣前兩個泛型代表著入參的類型,第三個代表結果類型。
public class BiFunctionTest {public static void main(String[] args) {BiFunctionTest2 biFunctionTest2 = new BiFunctionTest2();System.out.println(biFunctionTest2.compute(4,5,(a,b) -> a * b,a -> a * 2));}public int compute(int a, int b, BiFunction<Integer,Integer,Integer> biFunction,Function<Integer,Integer> function){return biFunction.andThen(function).apply(a,b);}
}