进阶day06-Function中的andThen方法(整合工厂)
This commit is contained in:
@@ -0,0 +1,39 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/*
|
||||||
|
学习内容:Function中的andThen方法
|
||||||
|
|
||||||
|
default <V> Function<T,V> andThen(Function after) 合并2个Function接口,生成一个新的,先执行this的apply方法,再执行after的apply方法
|
||||||
|
default <V> Function<V,R> compose(Function before) 与以上的方法完全相反
|
||||||
|
|
||||||
|
需求:
|
||||||
|
1."10"->10
|
||||||
|
2.10->100
|
||||||
|
分析:
|
||||||
|
1.一个Function 将"10" 转换成10
|
||||||
|
2.另一个Function 将10 转换成100
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo14 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
method( s->Integer.parseInt(s),
|
||||||
|
i->i*10
|
||||||
|
,"10"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义一个方法,接收2个数据工厂,1个数据原料,打印最终结果,不需要返回值
|
||||||
|
public static void method(Function<String,Integer> function1 ,Function<Integer,Integer> function2,String s) {
|
||||||
|
/*Integer result1 = function1.apply(s);
|
||||||
|
Integer result2 = function2.apply(result1);*/
|
||||||
|
/* Integer result2 = function2.apply(function1.apply(s));
|
||||||
|
System.out.println(result2);*/
|
||||||
|
|
||||||
|
/*default <V> Function<T,V> andThen(Function after) 合并2个Function接口,
|
||||||
|
生成一个新的,先执行this的apply方法,再执行after的apply方法*/
|
||||||
|
|
||||||
|
System.out.println(function1.andThen(function2).apply(s));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user