Java提供了函数式编程Function
https://blog.csdn.net/qq_27416233/article/details/84832013
在JDK1.8中,对集合的处理上用的比较多
R apply(T t)
将给定的参数应用到这个函数上,传入的参数类型为T返回类型为R
import java.util.function.Function;
public class FunctionTest {
public static void main(String[] args) {
Integer a = new FunctionTest().executeFunction(Calc::add1, 100);
Integer b = new FunctionTest().executeFunction(Calc::string2Integer, "200");
System.out.println(a);
System.out.println(b);
}
private <T, R> R executeFunction(Function<T, R> function, T a) {
R apply = function.apply(a);
return apply;
}
}
class Calc {
public static Integer add1(Integer a) {
return a + 1;
}
public static Integer add2(Integer a) {
return a + 2;
}
public static Integer string2Integer(String str) {
return Integer.valueOf(str);
}
}
网友评论