apply
public class FunctionInter {
public static void method(String s, Function<String,Integer> fun) {
int apply = fun.apply(s);
System.out.println(apply);
}
public static void main(String[] args) {
String s = "1234";
method(s,(String str)->{
return Integer.parseInt(str);
});
}
}
andThen
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210625163100301.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3E0NDUxNzI4,size_16,color_FFFFFF,t_70#pic_center)
import java.util.function.Function;
public class AndThenDemo {
public static void andThenPra(String s, Function<String,Integer> fun1, Function<Integer,String> fun2) {
String ss = fun1.andThen(fun2).apply(s);
System.out.println(ss);
}
public static void main(String[] args) {
String s = "123";
andThenPra(s,(String s1)->{
return Integer.parseInt(s1)+10;
},(Integer i)->{
return i + "";
});
}
}
Function练习
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210626093614457.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3E0NDUxNzI4,size_16,color_FFFFFF,t_70#pic_center)
public class FunctionClass {
public static int exchange(String s, Function<String,String> fun1,Function<String,Integer> fun2,Function<Integer,Integer> fun3) {
return fun1.andThen(fun2).andThen(fun3).apply(s);
}
public static void main(String[] args) {
String str = "赵丽颖,20";
int num = exchange(str,(String str1)->{
return str1.split(",")[1];
},(String str2)->{
return Integer.parseInt(str2);
},(Integer i)->{
return i + 100;
});
System.out.println(num);
}
}