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:

    public void basicUsage(){
        List<String> stringList=Stream.of("a","b","c","d").filter(s -> s.startsWith("a")).collect(Collectors.toList());"{}",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)

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:

    public void complexPredicate(){
        List<String> stringList=Stream.of("a","ab","aac","ad")
                .filter(s -> s.startsWith("a") &&  s.length()>1)

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:

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

        List<String> stringList2 = Stream.of("a","ab","aac","ad")

        List<String> stringList3 = Stream.of("a","ab","aac","ad")


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

Set operation of Predicate

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

    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(>true, Predicate::and))

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


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

Examples of this article

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

Topics: Programming Java Lambda github