源码
点击查看
@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));
}
}
说明:
apply:接收两个参数,返回一个结果;
andThen:先执行自己的函数,在执行传入的Function
类型的函数;
示例
@Test
public void test8() {
BiFunction<Integer, Integer, Integer> biFunction = (param1, param2) -> param1 + param2;
Function<Integer, Integer> function = param -> param * 10;
// 2 + 3 ;执行 biFunction
System.out.println(biFunction.apply(2, 3));
// ( 2 + 3 ) * 10;先执行 biFunction,再执行 function
System.out.println(biFunction.andThen(function).apply(2, 3));
}
输出结果:
5
50