侧边栏壁纸
博主头像
一只小松徐吖博主等级

A Good Boy ⛵️⛵️⛵️

  • 累计撰写 41 篇文章
  • 累计创建 40 个标签
  • 累计收到 6 条评论

目 录CONTENT

文章目录

Java8新特性-Function接口与BiFunction接口

超级管理员
2022-03-27 / 0 评论 / 0 点赞 / 535 阅读 / 6426 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2024-02-20,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Function 接口

  • JDK8新增的函数式接口
  • 接口只有一个抽象方法apply,接受一个T类型参数,返回一个R类型参数,T, R表示泛型,可以相同
  • 除了一个抽象的apply方法之外,Function存在两个默认的default方法,compose和andThen,这两个方法都是用来组合不同的Function的

apply 方法

/**
 * 输入一个T类型的参数
 * 返回一个R类型的值
 */
R apply(T t);

compose 方法

/**
 * 接口默认方法
 * 组合两个函数方法, 返回一个新的函数
 * before的apply方法输入一个V类型的参数, 返回一个T类型的参数
 * 当前的Funtion的apply方法是输入一个T类型的参数, 返回一个R类型的参数
 * 两个函数组合之后, 返回一个新的Function, 新的Function输入一个V类型的参数, 返回一个R类型的参数
 * 简单来说, 入参功能 : V -> T, 当前函数功能 : T -> R, 返回函数功能 : V -> R
 */
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
    // 非空判断
    Objects.requireNonNull(before);
    return (V v) -> apply(before.apply(v));
}

andThen 方法

/**
 * 组合两个函数方法, 返回一个新的函数
 * after的apply方法输入一个R类型的参数, 返回一个V类型的参数
 * 当前的Funtion的apply方法是输入一个T类型的参数, 返回一个R类型的参数
 * 两个函数组合之后, 返回一个新的Function, 新的Function输入一个T类型的参数, 返回一个V类型的参数
 * 简单来说, 入参功能 : R -> V, 当前函数功能 : T -> R, 返回函数功能 : T -> V
 */
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
    // 非空判断
    Objects.requireNonNull(after);
    return (T t) -> after.apply(apply(t));
}

BiFunction 接口

  • JDK8新增函数式接口
  • 可以理解为Function的一种扩展, Function接口接收一个参数, 返回一个参数; BiFunction接口接受两个参数, 返回一个参数
  • 唯一的抽象方法apply, 接收两个参数, 返回一个参数
  • 存在一个默认方法addThen, 由于apply方法接收两个参数, 所以不存在compose方法

apply 方法

/**
 * 输入两个参数, 类型分别是T和U, 返回一个结果, 类型为R
 *
 * @param t 函数第一个输入参数
 * @param u 第二个输入参数
 * @return 返回结果
 */
R apply(T t, U u);

andThen 方法

/**
 * 传入一个Function类型,该Function类型输入参数为R, 返回结果类型为V
 * 当前BiFunction的apply函数输入参数类型为T, U; 返回结果类型为R
 * 将两个参数组合之后返回一个新的BiFunction方法, 输入参数是T,U,返回结果类型为V
 * 简单来说就是当前的BiFunction是(T, U) -> R, 传入的Function是R -> V
 * 所以组合后的新的Function是(T, U) -> V, 即把BiFunction的返回结果作为入参再传入Function中
 */
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));
}

测试

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * @author xiaoxuxuy
 * @date 2022/3/27 12:31 下午
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoFunctionTest {

    @Test
    public void function() {
        // Function函数的使用
        Integer addResult = compute(3, value -> value + value);
        System.out.println("加法结果:" + addResult);

        Integer subResult = compute(3, value -> value - 1);
        System.out.println("减法结果:" + subResult);

        Integer multiplicationResult = compute(3, value -> value * value);
        System.out.println("乘法结果:" + multiplicationResult);

        Integer divisionResult = compute(6, value -> value / 3);
        System.out.println("除法结果:" + divisionResult);

        // 使用compose场景, 从右向左处理, 这里就是 (6 * 6) + 10 = 46
        Integer composeResult = computeForCompose(6,
                value -> value + 10,
                value -> value * value);
        System.out.println("Function compose 结果:" + composeResult);

        // 使用andThen场景, 从左向右处理, 这里就是(3 + 20) - 10 = 13
        Integer andThenResult = computeForAndThen(3,
                value -> value + 20,
                value -> value - 10);
        System.out.println("Function andThen 结果:" + andThenResult);

        // 使用 BiFunction场景, 这里是 2 + 3 = 5
        Integer biFuncResult = computeForBiFunction(2, 3,
                (v1, v2) -> v1 + v2);
        System.out.println("BiFunction 结果:" + biFuncResult);

        // 使用 BiFunction andThen场景, 这里是 (2 * 3) + 6 = 12
        Integer biFuncAndThenResult = computeForBiFunctionAndThen(2, 3,
                (v1, v2) -> v1 * v2, v1 -> v1 + 6);
        System.out.println("BiFunction andThen 结果:" + biFuncAndThenResult);
    }

    /**
     * 使用Function函数
     *
     * @param num
     * @param function
     * @return
     */
    private Integer compute(Integer num, Function<Integer, Integer> function) {
        return function.apply(num);
    }

    /**
     * 使用compose函数,简单的说,就是从右向左处理。
     *
     * @param num
     * @param function1
     * @param function2
     * @return
     */
    private Integer computeForCompose(Integer num,
                                      Function<Integer, Integer> function1,
                                      Function<Integer, Integer> function2) {
        return function1.compose(function2).apply(num);
    }

    /**
     * 使用andThen函数,简单的说,就是从左向右处理。
     *
     * @param num
     * @param function1
     * @param function2
     * @return
     */
    private Integer computeForAndThen(Integer num,
                                      Function<Integer, Integer> function1,
                                      Function<Integer, Integer> function2) {
        return function1.andThen(function2).apply(num);
    }

    /**
     * 使用BiFunction函数
     *
     * @param num1
     * @param num2
     * @param biFunction
     * @return
     */
    private Integer computeForBiFunction(Integer num1, Integer num2,
                                         BiFunction<Integer, Integer, Integer> biFunction) {
        return biFunction.apply(num1, num2);
    }

    /**
     * 使用BiFunction andThen方法
     *
     * @param num1
     * @param num2
     * @param biFunction
     * @param function
     * @return
     */
    private Integer computeForBiFunctionAndThen(Integer num1, Integer num2,
                                                BiFunction<Integer, Integer, Integer> biFunction,
                                                Function<Integer, Integer> function) {
        return biFunction.andThen(function).apply(num1, num2);
    }

}
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区