zoukankan      html  css  js  c++  java
  • Java8之Function、BiFunction使用

    • BiFunction<T,U,R> 接收 2个参数 ,返回一个结果
    public class DemoFunction {
    
        public static void main(String[] args) {
            DemoFunction t1 = new DemoFunction();
            // Function函数的使用
            Integer addResult = t1.compute(3, value -> value + value);
            System.out.println("加法结果:" + addResult); //3+3
    
            Integer subResult = t1.compute(3, value -> value - 1);
            System.out.println("减法结果:" + subResult); //3-1
    
            Integer multipResult = t1.compute(3, value -> value * value);
            System.out.println("乘法结果:" + multipResult); //3*3
    
            Integer divisionResult = t1.compute(6, value -> value / 3);
            System.out.println("除法结果:" + divisionResult); //6/3
    
            // 使用compose场景, 从右向左处理, 这里就是 (6 * 6) + 10 = 46
            Integer composeResult = t1.computeForCompose(6,
                    value -> value + 10,
                    value -> value * value);
            System.out.println("Function compose 结果:" + composeResult);
    
            // 使用andThen场景, 从左向右处理, 这里就是(3 + 20) - 10 = 13
            Integer andThenResult = t1.computeForAndThen(3,
                    value -> value + 20,
                    value -> value - 10);
            System.out.println("Function andThen 结果:" + andThenResult);
    
    
            // 使用 BiFunctioin场景, 这里是 2 + 3 = 5
            Integer biFuncResult = t1.computeForBiFunction(2, 3,
                    (v1, v2) -> v1 + v2);
            System.out.println("BiFunction 结果:" + biFuncResult);
    
            // 使用 BiFunctioin andThen场景, 这里是 (2 * 3) + 6 = 12
            Integer biFuncAndThenResult = t1.computeForBiFunctionAndThen(2, 3,
                    (v1, v2) -> v1 * v2, v1 -> v1 + 6);
            System.out.println("BiFunction andThen 结果:" + biFuncAndThenResult);
    
        }
    
        /**
         * 使用JDK8 Function函数
         *
         * @param num       入参
         * @param function  函数
         * @return          Integer
         */
        private Integer compute(Integer num, Function<Integer, Integer> function) {
            return function.apply(num);
        }
    
        /**
         * 使用compose函数,简单的说,就是从右向左处理。
         *
         * @param num        变量
         * @param function1  函数1
         * @param function2  函数2
         * @return           Integer
         */
        private Integer computeForCompose(Integer num,
                                          Function<Integer, Integer> function1,
                                          Function<Integer, Integer> function2) {
            return function1.compose(function2).apply(num);
        }
    
        /**
         * 使用andThen函数,简单的说,就是从左向右处理。
         *
         * @param num       变量
         * @param function1 函数1
         * @param function2 函数2
         * @return          Integer
         */
        private Integer computeForAndThen(Integer num,
                                          Function<Integer, Integer> function1,
                                          Function<Integer, Integer> function2) {
            return function1.andThen(function2).apply(num);
        }
    
        /**
         *  使用BiFunction
         *
         * @param num1         变量1
         * @param num2         变量2
         * @param biFunction   函数
         * @return             Integer
         */
        private Integer computeForBiFunction(Integer num1, Integer num2,
                                             BiFunction<Integer, Integer, Integer> biFunction) {
            return biFunction.apply(num1, num2);
        }
    
        /**
         * 使用BiFunction andThen方法
         */
        private Integer computeForBiFunctionAndThen(Integer num1, Integer num2,
                                                    BiFunction<Integer, Integer, Integer> biFunction,
                                                    Function<Integer, Integer> function) {
            return biFunction.andThen(function).apply(num1, num2);
        }
    
    }

    输出结果

    作者:donleo123
    本文如对您有帮助,还请多推荐下此文,如有错误欢迎指正,相互学习,共同进步。
  • 相关阅读:
    python 并发编程 多线程 event
    python 并发编程 多线程 定时器
    python 并发编程 多线程 信号量
    linux top 查看CPU命令
    python 并发编程 多线程 GIL与多线程
    python 并发编程 多线程 死锁现象与递归锁
    python 并发编程 多线程 GIL与Lock
    python GIL全局解释器锁与互斥锁 目录
    python 并发编程 多线程 GIL全局解释器锁基本概念
    执行python程序 出现三部曲
  • 原文地址:https://www.cnblogs.com/donleo123/p/15621066.html
Copyright © 2011-2022 走看看