进阶day06-Function的练习&&函数式接口的总结

This commit is contained in:
2026-03-14 13:56:30 +08:00
parent 2725c1d6ec
commit 64e5edb203

View File

@@ -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);
}
}