publicclassFunctionTest{privatestatic List<Function<Integer,Integer>> list =newArrayList<>();privatestatic Function<Integer,Integer> a = value -> value +10;privatestatic Function<Integer,Integer> b = value -> value +20;privatestatic Function<Integer,Integer> c = value -> value +30;privatestatic Function<Integer,Integer> d = value -> value *2;static{
list = Arrays.asList(a,b,c,d);}publicstaticvoidmain(String[] args){
FunctionTest functionTest =newFunctionTest();
System.out.println(functionTest.computeByDESC(2, list));// 64
System.out.println(functionTest.computeByASC(2, list));// 124}/**
* 倒叙执行
*/publicintcomputeByDESC(int a, List<Function<Integer,Integer>> list){
Function<Integer,Integer> result = list.get(0);for(int i =0; i < list.size(); i++){if(i+1<list.size()){
result = result.compose(list.get(i+1));}}return result.apply(a);}/**
* 正序执行
*/publicintcomputeByASC(int a, List<Function<Integer,Integer>> list){
Function<Integer,Integer> result = list.get(0);for(int i =0; i < list.size(); i++){if(i+1<list.size()){
result = result.andThen(list.get(i+1));}}return result.apply(a);}}