java 8 之函数编程BiFunction

文章对java 8 BiFunction接口使用及源码做解释,在java 8 后interface 支持方法体,但是需要用default 修饰

  1. 我们先来看下源码
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 <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));
  1. 来个举例:

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

备注:方便扩展使用,举例使用乏类模式出入参数,你也可以使用具体参数类型测试

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟-要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值