The use of predicate chain in java 8

Posted by ThoughtRiot on Tue, 28 Apr 2020 01:49:17 +0200

The use of predicate chain in java 8

brief introduction

Predicate is a functional interface, which represents a method that needs to enter a parameter and return the boolean type. Usually used in stream filter to indicate whether the filter conditions are met.

    boolean test(T t);

Basic use

Let's first look at how to use Predicate in stream filter:

    [@Test](https://my.oschina.net/azibug)
    public void basicUsage(){
        List<String> stringList=Stream.of("a","b","c","d").filter(s -> s.startsWith("a")).collect(Collectors.toList());
        log.info("{}",stringList);
    }

The above example is very basic. I will not talk about it here.

Use multiple filters

If we have multiple Predicate conditions, we can use multiple filters to filter:

    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);
    }

In the above example, we added a filter and a Predicate to the filter.

Using compound Predicate

The definition of Predicate is to enter a parameter and return a boolean value. If there are multiple test conditions, we can combine them into a test method:

    [@Test](https://my.oschina.net/azibug)
    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);
    }

In the above example, we take s.startswith ("a") & & s.length() > 1 as the implementation of test.

Combine Predicate

Although predict is an interface, it has several default methods to implement the combination operation between predict.

For example: predict. And(), predict. Or(), and predict. Gate().

Here's their example:

[@Test](https://my.oschina.net/azibug)
    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);

    }

In fact, we don't need to assign a predicate as long as it satisfies The lambda expression of the predicate interface can be regarded as a predicate. You can also call and, or, and gate operations:

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);

Set operation of Predicate

If we have a Predicate set, we can use the reduce method to merge it:

[@Test](https://my.oschina.net/azibug)
    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);
    }

In the above example, we call the reduce method to perform the and operation on the Predicate in the collection.

summary

This paper introduces the operation of many kinds of Predicate. I hope you can use it flexibly in practice.

Examples of this article https://github.com/ddean2009/learn-java-streams/tree/master/predicate-chain

Welcome to my official account: the procedures, the more wonderful things waiting for you! More at www.flydean.com

Topics: Programming Java Lambda github