首页 > 其他分享 >常用的函数式接口-Function接口-默认方法-andThen和Function练习-自定义函数模型拼接

常用的函数式接口-Function接口-默认方法-andThen和Function练习-自定义函数模型拼接

时间:2022-09-23 11:26:23浏览次数:50  
标签:Function andThen 函数 18 接口 Integer String

常用的函数式接口-Function接口-默认方法-andThen

Function接口中的默认方法andThen:用来进行组合操作
需求:
把String类型的"123",转换为Inteter类型,把转换后的结果加10
把增加之后的Integer类型的数据,转换为String类型
分析:
转换了两次
第一次是把String类型转换为了Integer类型
所以我们可以使用Function<String,Integer> fun1
Integer i = fun1.apply("123")+10;
第二次是把Integer类型转换为String类型
所以我们可以使用Function<Integer,String> fun2
String s = fun2.apply(i);
我们可以使用andThen方法,把两次转换组合在一起使用
String s = fun1.andThen(fun2).apply("123");
fun1先调用apply方法,把字符串转换为Integer
fun2再调用apply方法,把Integer转换为字符串

Function练习-自定义函数模型拼接

练习:自定义函数模型拼接
题目:请使用Function进行函数模型拼接,按照顺序需要执行的多个函数的操作为:
String str = "张三,18"
分析:
1.将字符串截取数字年龄部分,得到字符串;
Function<String,String> "张三,18" -> "18"
2.将上一步的字符串转换成为int类型的数字;
Function<String,Integer> "18" -> 18
3.将上一步的int数字累加100,得到结果int数组。
Function<Integer,Integer> 18 -> 118

标签:Function,andThen,函数,18,接口,Integer,String
From: https://www.cnblogs.com/wsfj/p/16722014.html

相关文章