java 8中 predicate chain的使用

java 8中 predicate chain的使用java

简介

Predicate是一个FunctionalInterface,表明的方法须要输入一个参数,返回boolean类型。一般用在stream的filter中,表示是否知足过滤条件。git

boolean test(T t);

基本使用

咱们先看下在stream的filter中怎么使用Predicate:github

@Test
    public void basicUsage(){
        List<String> stringList=Stream.of("a","b","c","d").filter(s -> s.startsWith("a")).collect(Collectors.toList());
        log.info("{}",stringList);
    }

上面的例子很基础了,这里就很少讲了。测试

使用多个Filter

若是咱们有多个Predicate条件,则可使用多个filter来进行过滤:code

public void multipleFilters(){
        List<String> stringList=Stream.of("a","ab","aac","ad").filter(s -> s.startsWith("a"))
                .filter(s -> s.length()>1)
                .collect(Collectors.toList());
        log.info("{}",stringList);
    }

上面的例子中,咱们又添加了一个filter,在filter又添加了一个Predicate。接口

使用复合Predicate

Predicate的定义是输入一个参数,返回boolean值,那么若是有多个测试条件,咱们能够将其合并成一个test方法:ip

@Test
    public void complexPredicate(){
        List<String> stringList=Stream.of("a","ab","aac","ad")
                .filter(s -> s.startsWith("a") &&  s.length()>1)
                .collect(Collectors.toList());
        log.info("{}",stringList);
    }

上面的例子中,咱们把s.startsWith("a") && s.length()>1 做为test的实现。get

组合Predicate

Predicate虽然是一个interface,可是它有几个默认的方法能够用来实现Predicate之间的组合操做。string

好比:Predicate.and(), Predicate.or(), 和 Predicate.negate()。it

下面看下他们的例子:

@Test
    public void combiningPredicate(){
        Predicate<String> predicate1 = s -> s.startsWith("a");
        Predicate<String> predicate2 =  s -> s.length() > 1;
        List<String> stringList1 = Stream.of("a","ab","aac","ad")
                .filter(predicate1.and(predicate2))
                .collect(Collectors.toList());
        log.info("{}",stringList1);

        List<String> stringList2 = Stream.of("a","ab","aac","ad")
                .filter(predicate1.or(predicate2))
                .collect(Collectors.toList());
        log.info("{}",stringList2);

        List<String> stringList3 = Stream.of("a","ab","aac","ad")
                .filter(predicate1.or(predicate2.negate()))
                .collect(Collectors.toList());
        log.info("{}",stringList3);

    }

实际上,咱们并不须要显示的assign一个predicate,只要是知足
predicate接口的lambda表达式均可以看作是一个predicate。一样能够调用and,or和negate操做:

List<String> stringList4 = Stream.of("a","ab","aac","ad")
                .filter(((Predicate<String>)a -> a.startsWith("a"))
                        .and(a -> a.length() > 1))
                .collect(Collectors.toList());
        log.info("{}",stringList4);

Predicate的集合操做

若是咱们有一个Predicate集合,咱们可使用reduce方法来对其进行合并运算:

@Test
    public void combiningPredicateCollection(){
        List<Predicate<String>> allPredicates = new ArrayList<>();
        allPredicates.add(a -> a.startsWith("a"));
        allPredicates.add(a -> a.length() > 1);

        List<String> stringList = Stream.of("a","ab","aac","ad")
                .filter(allPredicates.stream().reduce(x->true, Predicate::and))
                .collect(Collectors.toList());
        log.info("{}",stringList);
    }

上面的例子中,咱们调用reduce方法,对集合中的Predicate进行了and操做。

总结

本文介绍了多种Predicate的操做,但愿你们在实际工做中灵活应用。

本文的例子https://github.com/ddean2009/learn-java-streams/tree/master/predicate-chain

欢迎关注个人公众号:程序那些事,更多精彩等着您!
更多内容请访问 www.flydean.com
相关文章
相关标签/搜索