Java8 Function和BiFunction
来源:互联网 发布:手机数据采集软件 编辑:程序博客网 时间:2024/05/29 07:31
深入理解和使用Function
public static void main(String[] args) { //这个例子讲解Function中compose和andThen的区别 Test09Function test = new Test09Function(); int result = test.functionCompose(10, value -> value * 2, value -> value * value); System.out.println("result = [" + result + "]"); int result2 = test.functionAndThen(10, value -> value * 2, value -> value * value); System.out.println("result2 = [" + result2 + "]"); System.out.println("---------------------------"); /** * 我们调用Function的apply(a)可以传入一个参数得到一个结果,但是这个类无法做到输入2个参数,得到一个结果, * BiFunction可以做到这一点。Bi==Bidirectional双向的意思 */ int result3 = test.biFunctionCaculate(30, 20, (value1, value2) -> value1 + value2); System.out.println("result3 = [" + result3 + "]"); int result4 = test.biFunctionCaculate(30, 20, (value1, value2) -> value1 - value2); System.out.println("result4 = [" + result4 + "]"); int result5 = test.biFunctionAndThen(30, 20, (value1, value2) -> value1 - value2, value -> value * value); System.out.println("result5 = [" + result5 + "]"); } public int functionCompose(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2){ //(V v) -> apply(before.apply(v)); // 先应用function2的apply,并将结果作为参数再应用function1的apply return function1.compose(function2).apply(a); } public int functionAndThen(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2){ //(T t) -> after.apply(apply(t)) // 先应用function1的apply,并将结果作为参数再应用function2的apply return function1.andThen(function2).apply(a); } public int biFunctionCaculate(int a, int b, BiFunction<Integer, Integer, Integer> biFunction){ return biFunction.apply(a, b); } //BiFunction没有compose方法 /*@Deprecated public int biFunctionCompose(int a, int b, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function){ //return biFunction.compose(function).apply(a, b); }*/ public int biFunctionAndThen(int a, int b, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function){ return biFunction.andThen(function).apply(a, b); }
阅读全文
0 0
- Java8 Function和BiFunction
- Java8 伪函数指针(Function、BiFunction)
- java8-bifunction源码
- 【java8】持续精进-之BiFunction
- Java8的Function接口学习(compose和andThen)
- Java8-Function实例
- JAVA8学习笔记-function
- JAVA8学习笔记-Function
- java8-function源码
- 【Java8】Guava——Function
- JAVA8 Function及Stream编程
- java8 Function,Consumer,Predicate 接口
- java8中Collectors的使用方法举例和Function<T,R>简介
- Java8学习笔记(四)-Function接口详解
- JAVA8 Function、Consumer、Predicate、Supplier接口
- jquery (function($){}) 和 $(function(){})
- Function和function
- java8:Supplier和Stream
- 接口与内部类
- PHP命名空间解惑
- 从Tensorflow代码中理解LSTM网络
- 10月集训test16
- opencv学习调整图像对比度
- Java8 Function和BiFunction
- Linux下Hadoop2.7.1集群环境的搭建(超详细版)
- scrapy 抓取内涵社区
- AC自动机+有向拓扑判环 Taboo Kattis
- AsyncTask的基础使用
- 机器学习实现双十一购物清单的自动商品标签归类
- Math对象总结(向上取整、向下取整、四舍五入、取随机数,取最大值、取最小值....)
- TCP和UDP的区别
- ES6-正则的扩展-后行断言