文章对java 8 BiFunction接口使用及源码做解释,在java 8 后interface 支持方法体,但是需要用default 修饰
import java.util.Objects;
/**
* Represents a function that accepts two arguments and produces a result.
* This is the two-arity specialization of {@link Function}.
*
* This is a functional interface
* whose functional method is {@link #apply(Object, Object)}.
*
* @param the type of the first argument to the function
* @param the type of the second argument to the function
* @param 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 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));
}
}
1.@FunctionalInterface 函数接口注解
2. apply 函数参数说明:将此函数应用于给定的参数。
参数:T– 第一个函数参数, U– 第二个函数参数 返回:函数结果,R返回函数执行完结果
3. andThen先来解释下入参:
R是第一个函数执行完的结果参数,V返回参数
该函数先执行R函数运行完结果在执行函数after函数
return (T t, U u) -> after.apply(apply(t, u));
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* @author zhaoyy
* @version 1.0
* @description TODO
* @date 2022/4/14
**/
public class BiFunctionTest {
public static void main(String[] args) {
System.out.println("--------------Integer----------------------------");
Integer r = consumer(2, 3, (x, y) -> x * y);
System.out.println(r);
System.out.println("-------------字符串-----------------------------");
String r1 = consumer(new StringBuffer("sfda"), new StringBuffer("TTT"), (x, y) -> x.append(y).toString());
System.out.println(r1);
System.out.println("----------------AndThen--------------------------");
Integer r2 = consumerAndThen(2, 3, (x, y) -> x * y, y -> y * y);
System.out.println(r2);
System.out.println("----------------AndThen-字符串-------------------------");
String r3 = consumerAndThen("abc", "def", (x, y) -> x + y, y -> y + y);
System.out.println(r3);
}
public static <D, T, R> R consumer(D a, T b, BiFunction<D, T, R> function) {
return function.apply(a, b);
}
public static <D, T, R, V> V consumerAndThen(D a, T b, BiFunction<D, T, R> function, Function<R, V> after) {
return function.andThen(after).apply(a, b);
}
}
输出结果:
--------------Integer----------------------------
6
-------------字符串-----------------------------
sfdaTTT
----------------AndThen--------------------------
36
----------------AndThen-字符串-------------------------
abcdefabcdef
输出解释:
第一次输出:x *y =2 * 3=6 输出结果:6
第二次输出:使用字符串,x+y=afds+TTT=afdsTTT 输出结果:afdsTTT
第三次输出:使用方法 addThen , x * y=R ,R * R =V 输出结果:36 。计算步骤(2 * 3=6, 6 * 6=36)
第四次输出:同第三输出步骤一样,结果输出abcdefabcdef
备注:方便扩展使用,举例使用乏类模式出入参数,你也可以使用具体参数类型测试