JDK8新特性 -- Function接口: apply,andThen,compose
1 Function<T, R>中的T, R表示接口输入、输出的数据类型。
R apply(T t)
- apply:
.例子:
func是定义好的Function接口类型的变量,他的输入、输出都是Integer类型,调用calculate方法时,将func作为参数传入,对参数5进行处理。FunctionTest functionTest = new FunctionTest();
// return e + 5;就是apply方法的具体实现
Function<Integer, String> func = e -> {return String.valueOf(e + 6);};
String result = functionTest.calculate(5, func);
System.out.println(result);public String calculate(Integer a, Function<Integer, String> function) {
return function.apply(a);
}andThen:
- 先处理参数,再对返回值使用
操作after进行处理。
Function<Integer, Integer> func = e -> {return e + 5;};
Function<Integer, Integer> func2 = e -> {return e * 5;};
//func2即after
func.andThen(func2).apply(5); // 50
compose:
- 和
andThen刚好相反:先使用操作before处理参数,再对返回值进行处理。
Function<Integer, Integer> func = e -> {return e + 5;};
Function<Integer, Integer> func2 = e -> {return e * 5;};
//func2即before
func.compose(func2).apply(5); // 30 compose源码:
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));//第一个apply是调用当前接口的方法
}- 注意
compose方法的返回值依然是Function<T, R>类型,所以不是
return this.apply(before.apply(v));
案例:
- 先处理参数,再对返回值使用
public class FunctionTest2 {
public static void main(String[] args) {
FunctionTest2 functionTest2 = new FunctionTest2();
int result1 = functionTest2.compute(5, e -> e * 5, e -> e + 5);
int result2 = functionTest2.compute2(5, e -> e * 5, e -> e + 5);
int result3 = functionTest2.compute3(5, e -> e * 5, e -> e + 5);
int result4 = functionTest2.compute4(5, e -> e * 5, e -> e + 5);
System.out.println(result1);//50
System.out.println(result2);//30
System.out.println(result3);//130
System.out.println(result4);//250
}public int compute(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.compose(function2).apply(source);
}
public int compute2(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.andThen(function2).apply(source);
}
public int compute3(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.andThen(function2).compose(function1).apply(source); //从后往前 25 125 130
}
public int compute4(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.compose(function2).andThen(function1).apply(source); } //10*5 50*5
}
JDK8新特性 -- Function接口: apply,andThen,compose的更多相关文章
- JDK8新特性之接口
在JDK7及以前的版本中,接口中都是抽象方法,不能定义方法体,但是从jdk8开始,接口中可以定义静态的非抽象的方法,直接使用接口名调用静态方法,但是它的实现类的类名或者实例却不可以调用接口中的静态方法 ...
- JDK8新特性:接口的静态方法和默认方法
在jdk8之前,interface之中可以定义变量和方法,变量必须是public.static.final的,方法必须是public.abstract的.由于这些修饰符都是默认的,所以在JDK8之前, ...
- JDK8新特性之接口默认方法与静态方法
接口默认方法与静态方法 有这样一些场景,如果一个接口要添加一个方法,那所有的接口实现类都要去实现,而某些实现类根本就不需要实现这个方法也要写一个空实现,所以接口默认方法就是为了解决这个问题. 接口静态 ...
- jdk8新特性--函数式接口的使用
函数式接口的概念: 函数式接口的格式: 示例: 函数式接口的使用: 简化lambda表达式:
- JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序
大家对java接口Comparator和Comparable都不陌生,JDK8里面Comparable还和以前一样,没有什么改动:但是Comparator在之前基础上增加了很多static和defau ...
- JDK8 新特性
JDK8 新特性目录导航: Lambda 表达式 函数式接口 方法引用.构造器引用和数组引用 接口支持默认方法和静态方法 Stream API 增强类型推断 新的日期时间 API Optional 类 ...
- JDK1.8新特性——Collector接口和Collectors工具类
JDK1.8新特性——Collector接口和Collectors工具类 摘要:本文主要学习了在Java1.8中新增的Collector接口和Collectors工具类,以及使用它们在处理集合时的改进 ...
- JDK8新特性一览
转载自:http://blog.csdn.net/qiubabin/article/details/70256683 官方新特性说明地址 Jdk8新特性.png 下面对几个常用的特性做下重点说明. 一 ...
- 一次电话Java面试的问题总结(JDK8新特性、哈希冲突、HashMap原理、线程安全、Linux查询命令、Hadoop节点)
面试涉及问题含有: Java JDK8新特性 集合(哈希冲突.HashMap的原理.自动排序的集合TreeSet) 多线程安全问题 String和StringBuffer JVM 原理.运行流程.内部 ...
随机推荐
- 树屋阶梯(codevs 1741)
题目描述 Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处的树屋露营.小龙分配的树屋建立在 ...
- hdu_1861_游船出租_201402282130
游船出租 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- hdu_1018_Big Number_201308191556
Big NumberTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- windows与linux下配置ant
转自:http://www.cnblogs.com/wuxinrui/archive/2012/01/06/2314392.html 1.下载:到ANT官方网站http://ant.apache.or ...
- iOS:解决pod的Insecure world writable dir问题
当我们运行pod setup的命令的时候,有时候会碰到这个警告: /Library/Ruby/Gems/2.0.0/gems/cocoapods-0.33.1/lib/cocoapods/execut ...
- Java学习笔记之 IO包 字节流
IO包最重要的五个类和一个接口 File/OutputStream/InputStream(字节流)/Writer/Reader(字符流) 一个接口:Serializable File类: 字节流 ...
- cppunit 单元测试程序
原文:http://www.cnblogs.com/wang-kai/p/5106740.html 极限编程(XP)推崇测试优先原则,由此引发了软件开发方法从传统的瀑布模型转向以测试为驱动的敏捷开发模 ...
- [WebGL入门]二十三,反射光的光照效果
注:文章译自http://wgld.org/,原作者杉本雅広(doxas).文章中假设有我的额外说明.我会加上[lufy:],另外,鄙人webgl研究还不够深入.一些专业词语.假设翻译有误.欢迎大家指 ...
- linux中的alsa工具与Android中的tinyalsa工具【转】
本文转载自:http://blog.csdn.net/luckywang1103/article/details/48053015 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?) ...
- 【BZOJ 2252】 矩阵距离
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2252 [算法] 将所有是”1“的点入队,然后广度优先搜索,即可 [代码] #incl ...