进阶day06-Function的练习&&函数式接口的总结
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
package com.inmind.functional_interface02;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/*
|
||||
请使用Function进行函数模型的拼接,按照顺序需要执行的多个函数操作为:
|
||||
|
||||
@@ -6,9 +10,21 @@ package com.inmind.functional_interface02;
|
||||
2. 将上一步的字符串转换成为int类型的数字;(String->integer)
|
||||
3. 将上一步的int数字累加100,得到结果int数字。(integer->integer)
|
||||
|
||||
常见函数式接口总结:
|
||||
1.consumer(消费者) :接收一个参数,没有返回值 p->{}
|
||||
2.Supplier(生产者) :没有参数,有一个返回值 ()->{return ..}
|
||||
3.predicate(数据筛选):接收一个参数,返回一个boolean p->{return ...}
|
||||
4.function(数据转换工厂):接收一个参数,有一个返回值 p->{return ...}
|
||||
*/
|
||||
public class Demo15 {
|
||||
public static void main(String[] args) {
|
||||
String str = "赵丽颖,20";
|
||||
//定义一个数据工厂String->integer
|
||||
Function<String,Integer> function1 = s-> Integer.parseInt(s.split(",")[1]);
|
||||
//定义一个数据工厂integer->integer
|
||||
Function<Integer,Integer> function2 = i->i+100;
|
||||
|
||||
Integer result = function1.andThen(function2).apply(str);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user