Java8新特性Function、BiFunction使用
闲话不多说,直接看代码,注释都写的很清楚了。
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);
} }
Java8新特性Function、BiFunction使用的更多相关文章
- Java8 新特性之Stream----java.util.stream
这个包主要提供元素的streams函数操作,比如对collections的map,reduce. 例如: int sum = widgets.stream() .filter(b -> b.ge ...
- 这可能是史上最好的 Java8 新特性 Stream 流教程
本文翻译自 https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/ 作者: @Winterbe 欢迎关注个人微信公众 ...
- java8新特性--Stream的基本介绍和使用
什么是Stream? Stream是一个来自数据源的元素队列并可以进行聚合操作. 数据源:流的来源. 可以是集合,数组,I/O channel, 产生器generator 等 聚合操作:类似SQL语句 ...
- 乐字节-Java8新特性之方法引用
上一篇小乐介绍了<Java8新特性-函数式接口>,大家可以点击回顾.这篇文章将接着介绍Java8新特性之方法引用. Java8 中引入方法引用新特性,用于简化应用对象方法的调用, 方法引用 ...
- Java8新特性之Lambda表达式
lambda表达式是java8给我们带来的几个重量级新特性之一,借用lambda表达式,可以让我们的java程序设计更加简洁.最近新的项目摒弃了1.6的版本,全面基于java8进行开发,本文是java ...
- java8 新特性学习笔记
Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...
- 2020你还不会Java8新特性?
Java8(1)新特性介绍及Lambda表达式 前言: 跟大娃一块看,把原来的电脑拿出来放中间看视频用 --- 以后会有的课程 难度 深入Java 8 难度1 并发与netty 难度3 JVM 难度4 ...
- Java8 新特性_Lambda 表达式
1. Java8新特性_简介 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 减少空指针异常的容器 Optional 2. ...
- 【Java8新特性】还没搞懂函数式接口?赶快过来看看吧!
写在前面 Java8中内置了一些在开发中常用的函数式接口,极大的提高了我们的开发效率.那么,问题来了,你知道都有哪些函数式接口吗? 函数式接口总览 这里,我使用表格的形式来简单说明下Java8中提供的 ...
随机推荐
- 数据库事务和spring事务的区别
数据库事务和spring事务 本质上其实是同一个概念,spring的事务是对数据库的事务的封装,最后本质的实现还是在数据库,假如数据库不支持事务的话,spring的事务是没有作用的.数据库的事务说简单 ...
- 运行okvis-mono
./build/okvis_app_synchronous config/config_fpga_p2_euroc1.yaml ../mav0
- [LeetCode] 582. Kill Process 终止进程
Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...
- OpenSSL 创建自签名证书
1.生成服务器私钥 openssl genrsa -out client.key 4096 2.生成证书签名请求(CSR) openssl req -new -key client.key -ou ...
- 说一说ORACLE的监听
1.讲在前面 本文档仅适用于如下范围:Oracle以dedicate (专有)连接模式通过TCP/IP协议连接的场景. 2.监听的作用 在谈监听的作用之前,有必要先看看监听的工作原理图: 客户端进程发 ...
- C++标准异常与自定义异常
参见https://www.runoob.com/cplusplus/cpp-exceptions-handling.html C++ 标准的异常 C++ 提供了一系列标准的异常,定义在 中,我们可以 ...
- Jenkins+Git+Maven+Tomcat详细安装步骤
jenkins安装 jenkins的war包安装 以下war包的安装是直接使用war包内嵌的页面访问,也可以将war包放到tomcat的webapps下通过tomcat访问,在下面的tomcat步骤有 ...
- 数据分析——matplotlib的用法
Matplotlib是一个强大的Python绘图和数据可视化的工具包.数据可视化也是我们数据分析的最重要的工作之一,可以帮助我们完成很多操作,例如:找出异常值.必要的一些数据转换等.完成数据分析的最终 ...
- My Swift Study
参考资源 <swifter> https://github.com/iOS-Swift-Developers/Swift 闭包逃逸 swift3中,闭包默认是非逃逸的.如果一个函数参数可能 ...
- mysql数据库优化实战--日期及IP地址的正确存储方式