java中BiFunction和Function详解

关于Lambda详见lambda描述。关于函数式接口详见函数式接口

首先我们来看一下BiFunction类,代码如下:

package java.util.function;

import java.util.Objects;

/**
 * Represents a function that accepts two arguments and produces a result.
 * This is the two-arity specialization of {@link Function}.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #apply(Object, Object)}.
 *
 * @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是java8的特性,通过这个关键字来设置函数的默认实现,这样实现类就可以不用实现这个接口,而采用接口中默认的      //实现
    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));
    }
}

//下面的一行代码返回的是BitFunction<T,U,V>类型对象。

return (T t, U u) -> after.apply(apply(t, u));

首先,执行this.apply(t,u),这里面的this是调用andThen函数的BitFunction类对象。

然后,将返回值传到after类对象的apply函数中。

最后,返回BiFunction类对象。

 

这行代码等价于:

return new BiFunction<T,U,V>(){

     V apply(T t, U u){

           return after.apply(value);//这里面的value为this调用apply返回的结果。

     }

}

举一个例子:

public int compute2(int a, b,BitFunction<Integer, Integer,Integer> function1, Function<Integer, Integer> function2) {

      return function1.andThen(function2).apply(a);

}

调用这个方法:

test.compute2(2,3, (v1,v2) -> v1+ v2, value -> value * value);

(value,value) -> value + value是一个BitFunction<Integer, Integer,Integer>类对象,等价于

return new BiFunction<T,U,V>(){

     V apply(T t, U u){

           return after.apply(V);

     }

}值就是function1

同样value -> value * value等价于

return new Function<R,V>(){

     V apply(T t, U u){

           return after.apply(V);

     }

}值就是function2

所以return function1.andThen(function2).apply(a);执行步骤:

第一步:执行function1.apply(2,3)得到2+3=5。返回一个BiFunction类对象,该类对象中的apply函数执行function2.apply(5)

第二步:执行function2.apply(5)得到5*5=25。

第三步:BiFunction类对象的apply函数中返回25。

详细的描述请看详细介绍

 

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值