package LambdaTest.LambdaTest05;
import java.util.function.Function;
/**
* FileName: FunctionTest01
* Author: lps
* Date: 2022/4/9 19:47
* Sign:刘品水 Q:1944900433
* default <V> Function<T,V> andThen(Function<? super R,? extends V> after)
* 返回一个由功能首次采用该函数的输入,然后将 after函数的结果。
* R apply(T t)
* 将此函数应用于给定的参数。
* default <V> Function<V,R> compose(Function<? super V,? extends T> before)
* 返回一个由功能,首先应用 before函数的输入,然后将该函数的结果。
* static <T> Function<T,T> identity()
* 返回一个函数,它总是返回它的输入参数。
*/
public class FunctionTest01 {
public static void main(String[] args) {
convert("202249", s -> Integer.parseInt(s));
//convert("2001823",Integer::parseInt);
convert(2022, i -> String.valueOf(i + 123));
convert("2021", Integer::parseInt, i -> String.valueOf(i + 1));
}
//定义一个方法 吧一个字符串转换成int类型
private static void convert(String s, Function<String, Integer> fun) {
Integer i = fun.apply(s);
System.out.println(i);
}
//吧int类型数据加上一个整数之后 转换为字符串
private static void convert(int i, Function<Integer, String> fun) {
String s = fun.apply(i);
System.out.println(s);
}
//把一个字符串转换为int类型 吧int加个整数后 转为字符串在控制台输出
private static void convert(String s, Function<String, Integer> fun1, Function<Integer, String> fun2) {
// Integer i = fun1.apply(s);
// String s0 = fun2.apply(i);
// System.out.println(s0);
// * default <V> Function<T,V> andThen(Function<? super R,? extends V> after)
// * 返回一个由功能首次采用该函数的输入,然后将 after函数的结果。
String ss=fun1.andThen(fun2).apply(s);
System.out.println(ss);
//default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
// Objects.requireNonNull(after);
// return (T t) -> after.apply(apply(t));
// }
}
}
![]()
![]()