JDK提供了大量常用的函数式接口以丰富Lambda的典型使用场景,它们主要在 java.util.function 包中被提供。 下面是最简单的Function接口及使用示例。
Function接口概述
java.util.function.Function 接口用来根据一个类型的数据得到另一个类型的数据,前者称为前置条件,后者称为后置条件。
@FunctionalInterfacepublic interface Function{
R apply(T t);
......
}
抽象方法:apply
Function 接口中最主要的抽象方法为: R apply(T t) ,根据类型T的参数获取类型R的结果。 使用的场景例如:将 String 类型转换为 Integer 类型。
importjava.util.function.Function;public classDemoFunctionApply {public static voidmain(String[] args) {
method(s->Integer.parseInt(s));
}private static void method(Functionfunction) {int num = function.apply("10");
System.out.println(num+ 20);
}
}
运行程序,控制台输出:
30
当然,最好是通过方法引用的写法。
默认方法:andThen
Function 接口中有一个默认的 andThen 方法,用来进行组合操作。JDK源代码如:
default Function andThen(Function super R, ? extends V>after) {
Objects.requireNonNull(after);return (T t) ->after.apply(apply(t));
}
该方法同样用于“先做什么,再做什么”的场景,和 Consumer 中的 andThen 差不多:
importjava.util.function.Function;public classDemoFunctionAndThen {public static voidmain(String[] args) {
method(
str-> Integer.parseInt(str)+10,
i-> i *= 10);
}private static void method(Function one, Functiontwo) {int num = one.andThen(two).apply("10");
System.out.println(num+ 20);
}
}
运行程序,控制台输出:
220
第一个操作是将字符串解析成为int数字,第二个操作是乘以10。两个操作通过 andThen 按照前后顺序组合到了一 起。
请注意,Function的前置条件泛型和后置条件泛型可以相同。
练习:自定义函数模型拼接
题目
请使用 Function 进行函数模型的拼接,按照顺序需要执行的多个函数操作为:
String str = "赵丽颖,20";
将字符串截取数字年龄部分,得到字符串;
将上一步的字符串转换成为int类型的数字;
将上一步的int数字累加100,得到结果int数字。
解答
importjava.util.function.Function;public classDemoFunction {public static voidmain(String[] args) {
String str= "赵丽颖,20";int age =getAgeNum(
str,
s-> s.split(",")[1],
s->Integer.parseInt(s),
n-> n += 100);
System.out.println(age);
}private static intgetAgeNum(String str,
Functionone,
Functiontwo,
Functionthree) {returnone.andThen(two).andThen(three).apply(str);
}
}
运行程序,控制台输出:
120