谓词复合

package com.ant.jdk8.chap03;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class PredicateCompositeDemo {
    public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(
                new Apple("green",200,"China"),
                new Apple("green",130,"Japan"),
                new Apple("red",150,"America"),
                new Apple("brown",170,"Thailand"));
        Predicate<Apple> redPredicate = apple->"red".equals(apple.getColor());
        Predicate<Apple> notRedAndHeavyPredicate = redPredicate.negate().and(a->a.getWeight()>150);
        apples.stream().filter(notRedAndHeavyPredicate).
                forEach(a-> System.out.println(a.getColor()+"->"+a.getCountry()+"->"+a.getWeight()));
    }
}

posted on 2018-09-02 22:53  shammgod_code  阅读(83)  评论(0编辑  收藏  举报

导航