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 原理.运行流程.内部 ...
随机推荐
- L - 贪心 基础
Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shor ...
- sysman 账号 oracle em console的一些问题
前几天用下面的命令修改了 sysman的密码. [oracle@racnode1 ~]$ emctl setpasswd dbconsole Oracle Enterprise Manager 11g ...
- (二)模板引擎之Velocity脚本基本的语法全
velocity velocity三种reference 变量:对java对象的一种字符串化表示,返回值调用了java的toString()方法的结果. 方法:调用的是对象的某个方法. ...
- 创建hive整合hbase的表总结
[Author]: kwu 创建hive整合hbase的表总结.例如以下两种方式: 1.创建hive表的同步创建hbase的表 CREATE TABLE stage.hbase_news_compan ...
- jquery ajax CORS 跨域訪问 WebService
JS代码: var word = document.getElementById("word").value; $.ajax({ type: "POST", c ...
- 【Allwinner ClassA20类库分析】4.GPIO类的使用
从本节起,開始使用ClassA20类库完毕操作外设的功能,请先在https://github.com/tjCFeng/ClassA20下载ClassA20类库. 封装的目的就是简化操作,试想一 ...
- Codeforces Round #330 (Div. 2)B. Pasha and Phone 容斥
B. Pasha and Phone Pasha has recently bought a new phone jPager and started adding his friends' ph ...
- HDU 2665(主席树,无修改第k小)
Kth number Time Limit: 15000/5000 MS (Java/Others) ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- P1314 聪明的质监员 二分答案
这个题我第一反应是线段树(雾),然后看了一眼题解之后就后悔了...前缀和...然后二分答案,然后就没有然后了. 题干: 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 nnn 个矿石 ...