andThen和compose区别
// 先定义两个函数 这里的函数泛型1是入参类型也就是x的类型泛型2就是返回值的类型
Function<Integer,Integer> a = x -> x+1;
Function<Integer,Integer> b = x -> x*2;
Function<Integer,Integer> c = a.andThen(b);
Function<Integer,Integer> d = a.compose(b);
c.apply(1); // 输出结果是:4
d.apply(1); // 输出结果是:3
- 这里用一张图来形容下andThen和compose的运行先后顺序
- 从图中可以很清楚的看出来,andThen会先执行调用方的函数而compose会先执行传入方的函数