jdk8新特性之Function接口的使用
Function<Numbers, Integer> test,相当于一个待实现的接口,告诉你入参是 Numbers 类型 ,出参是Integer 类型。
示例:
package com.wdbyte;
import java.util.function.Function;
public class Java8Function {
public static void main(String[] args) {
Function<String, String> toUpperCase = str -> str.toUpperCase();
String result = toUpperCase.apply("www.wdbyte.com");
System.out.println(result);
}
}
输出结果:14
Function andThen
Function 函数接口的 andThen()
方法可以让多个 Function 函数连接使用。
package com.wdbyte;
import java.util.function.Function;
public class Java8FunctionAndThen {
public static void main(String[] args) {
Function<String, Integer> lengthFunction = str -> str.length();
Function<Integer, Integer> doubleFunction = length -> length * 2;
Integer doubleLength = lengthFunction.andThen(doubleFunction).apply("www.wdbyte.com");
System.out.println(doubleLength);
}
}
输出结果:28