java8中的函数式借口提供了允许你进行复合的方法,可以把多个lambda表达式复合成更复杂的表达式,就好像与操作(and),或操作(or),非操作(not),以及组合操作
Predicate系列的组合操作
接口 相关方法
Predicate<T> Predicate<T> negate()
Predicate<T> or(Predicate<? super T> other)
Predicate<T> and(Predicate<? super T> other)
DoublePredicate negate()
DoublePredicate DoublePredicate and(DoublePredicate other)
DoublePredicate or(DoublePredicate other)
IntPredicate negate()
IntPredicate IntPredicate and(IntPredicate other)
IntPredicate or(IntPredicate other)
LongPredicate negate()
LongPredicate LongPredicate and(LongPredicate other)
LongPredicate or(LongPredicate other)
BiPredicate<T,U> negate()
BiPredicate<T,U> BiPredicate<T,U> and(BiPredicate<? super T,? super U> other)
BiPredicate<T,U> or(BiPredicate<? super T,? super U> other)
Predicate系列的每个借口都含有and,or,not操作,可以和其他相同的借口进行组合
对于苹果类,根据苹果的颜色和重量筛选符合要求的苹果可以有
//筛选颜色为红色的苹果
Predicate<Apple> redApple = (Apple apple) -> apple.getColor().equals("red");
//筛选苹果重量大于15的苹果
Predicate<Apple> weightApple = apple -> apple.getWeight() > 15;
//对苹果的颜色和重量筛选进行复合,筛选出即是红色重量有大于15的苹果
List<Apple> filterApples = filter(inventory,redApple.and(weightApple));
//筛选出是红色或者重量大于15的苹果
filterApples = filter(inventory,redApple.or(weightApple));
//筛选出既不是颜色既不是红色重量也不大于15的苹果
filterApples = filter(inventory,redApple.negate().or(weightApple.negate()));
Function系列的组合操作
接口 相关方法
Function<T,R> Function<T,V> addThen(Function<? super R,? extends V> after)
Function<V,R> compose(Function<? super V,? extends T> before)
BiFunction<T,U,R> BiFunction<T,U,V> addThen(Function<? super R,? extends V> after)
Function系列的接口只有Function<T,R>和BiFunction<T,U,R>可以进行复合操作
Function的意思是函数,作为函数,具有参数和返回值,类似于f(x) = x + 1,
那么Function<T,V>的addThen方法可以理解为将我的返回值作为别的函数的参数,在别的函数中进行函数运算。
如果说有两个函数 f(x) = x + 1 和 g(y) = y + 2,那么addThen可以将f(x)的返回值作为g(x)的参数传递进去,类似于g(f(x)) = f(x) + 2 = (x + 1) + 2这种
同样compose方法可以将别的函数的返回值作为自己的参数进行函数运算
BiFunction<T,U,R>和Function<T,R>不同的是,他具有三个泛型参数,以及他的addThen方法接收的是一个Function<? super V,? extends T>对象
同样也是将BiFunction的运算结果作为Function的参数,返回Function的运算结果
使用Function接口实现为苹果的重量加20,并返回一个列表
//获取苹果的重量
Function<Apple,Integer> getWeight = Apple::getWeight;
//将一个数字加上20,在这里这个数字表示重量
Function<Integer,Integer> addWeight = weight -> weight + 20;
//使用Function的addThen方法将所有苹果的重量加上20并返回一个列表
List<Integer> weights = map(inventory,getWeight.andThen(addWeight));
//使用Function的compose方法将所有苹果的重量加上20并返回一个列表
weights = map(inventory,addWeight.compose(getWeight));
Consumer系列接口
接口 相关方法
Consumer<T> Consumer<T> addThen(Consumer<? super T> after)
DoubleConsumer DoubleConsumer addThen(DoubleConsumer after)
IntConsumer IntConsumer addThen(IntConsumer after)
LongConsuemr LongConsumer addThen(LongConsumer after)
BiConsumer<T,U> BiConsumer<T,U> addThen(BiConsumer<? super T,? super U> after)
Consumer系列的接口只接收一个泛型参数的都可以使用addThen方法,接收两个参数的只有BiConsumer接口可以使用addThen方法
关于addThen方法,很显然,在经过当前的Consumer对象的处理之后,可以继续被某个Consumer对象处理
使用Consumer将苹果的重量在原有的基础上乘2再加上10
//将苹果的重量乘2
Consumer<Apple> two = apple -> apple.setWeight(apple.getWeight() * 2);
//将苹果的重量加10
Consumer<Apple> add = apple -> apple.setWeight(apple.getWeight() + 10);
//使用组合将苹果的重量先乘2再加10
forEach(inventory,two.andThen(add));