闲话不多说,直接看代码,注释都写的很清楚了。

package com;

import java.util.function.BiFunction;
import java.util.function.Function; 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); Integer subResult = t1.compute(3, value -> value - 1);
System.out.println("减法结果:" + subResult); Integer multipResult = t1.compute(3, value -> value * value);
System.out.println("乘法结果:" + multipResult); Integer divisionResult = t1.compute(6, value -> value / 3);
System.out.println("除法结果:" + divisionResult); // 使用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); } /**
* @param num
* @param function
* @return
* @desc 使用JDK8 Function函数
*/
private Integer compute(Integer num, Function<Integer, Integer> function) {
Integer result = function.apply(num);
return result;
} /**
* @param num
* @param function1
* @param function2
* @return
* @desc 使用compose函数,简单的说,就是从右向左处理。
*/
private Integer computeForCompose(Integer num,
Function<Integer, Integer> function1,
Function<Integer, Integer> function2) {
return function1.compose(function2).apply(num);
} /**
* @param num
* @param function1
* @param function2
* @return
* @desc 使用andThen函数,简单的说,就是从左向右处理。
*/
private Integer computeForAndThen(Integer num,
Function<Integer, Integer> function1,
Function<Integer, Integer> function2) {
return function1.andThen(function2).apply(num);
} /**
* @param num1
* @param nuum2
* @param biFunction
* @return
* @desc 使用BiFunction
*/
private Integer computeForBiFunction(Integer num1, Integer num2,
BiFunction<Integer, Integer, Integer> biFunction) {
return biFunction.apply(num1, num2);
} /**
* @param num1
* @param num2
* @param biFunction
* @param function
* @return
* @desc 使用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);
} }

最新文章

  1. bootstrap在jsp中的应用
  2. ios 性能优化策略
  3. [界面开发新秀]免费的AYUI,开发360领航版系列教程[2/40]
  4. 【python cookbook】【数据结构与算法】1将序列分解为单独的变量
  5. Operfire/XMPP
  6. VIM中文乱码(_vimrc配置文件备份)
  7. 手机时间选择插件 Jquery
  8. Android四个多线程分析:MessageQueue实现
  9. linux extract rar files
  10. L11,one good turn deserves another
  11. 17年年终总结——走过2017,迎来2018Flag
  12. 四、Tensorflow的分布式训练
  13. Linux_常用命令简单介绍(netstat,awk,top,tail,head,less,more,cat,nl)
  14. Windows下pip命令无法使用的解决办法
  15. Python if语句
  16. [leetcode]339. Nested List Weight Sum嵌套列表加权和
  17. 中南大学2018年ACM暑期集训前期训练题集(入门题) Q: Simple Line Editor
  18. OEM、ODM、OBM、OPM概念,作用与区别
  19. Hadoop2.2.0安装配置手册!完全分布式Hadoop集群搭建过程~(心血之作啊~~)
  20. 关于正则表达式的“\b”

热门文章

  1. 如何下载官网最新版 win10 系统?
  2. Sword libcurl库CURLE_COULDNT_CONNECT错误
  3. BUG处理流程图
  4. [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表
  5. 一键安装docker-ce
  6. Maven中解决jar包冲突的三种方式
  7. lay-verify
  8. LeetCode 290. 单词规律(Word Pattern) 41
  9. Fiddler如何监听PC和手机
  10. 嵌入式02 STM32 实验01 端口复用和重映射