top of page
Writer's pictureThe Tech Platform

How to use Stream allMatch() and anyMatch() function in Java?



Stream allMatch:

We often run into problems where we need to check a condition on a list of values. To give an example, let’s say there’s an array with 500 objects and we need to check if each one of them is divisible by 3 or not.


Now, the normal way is to run a loop through the array and check the condition for all objects. But wait, why so much boilerplate code? We can use streams and this would be completed in just a single line! Yes, that’s true.


The syntax of the Stream allMatch function is as below:

allMatch(Predicate predicate) 

Here, the predicate is the condition on which each element will be checked. We can also provide multiple such predicates to stream. Below is a working example of how the function works.


As shown in the code, we can see how it works. we have an array of size 5 with different numbers inside it. we convert it to Stream using Arrays.stream() function.


We have used IntPredicate. similarly, we can use different types of Predicates.


Code:

import java.util.Arrays;
import java.util.function.IntPredicate;

public class StreamExample {

    public static void main(String[] args) {
        int[] arr = new int[] {3, 18, 15, 30, 60};

        IntPredicate isDivisibleByThree = a -> (a%3 == 0);
        IntPredicate isDivisibleByFive = a -> (a%5 == 0);

        IntPredicate isDivisibleByThreeAndFive = isDivisibleByThree
                .and(isDivisibleByFive);

        boolean isDivisibleByThreeResult = Arrays.stream(arr)
                .allMatch(isDivisibleByThree);
        System.out.println("divisible by three: "+isDivisibleByThreeResult);
        boolean isDivisibleByFiveResult = Arrays.stream(arr)
                .allMatch(isDivisibleByFive);
        System.out.println("divisible by five: "+isDivisibleByFiveResult);
        boolean isDivisibleByThreeAndFiveResult = Arrays.stream(arr)
                .allMatch(isDivisibleByThreeAndFive);
        System.out.println("divisible by three and five: "
                +isDivisibleByThreeAndFiveResult);

    }

}

Output:


Stream anyMatch:

Now, after observing the above code, you guys must be pretty confident about what stream anyMatch does. anyMatch basically searches the array/list or any collection for any match. Just 1 is enough for the answer to be true.


So continuing our previous code and example, let’s understand how this happens. But before that, let’s see the syntax of stream anyMatch


The syntax of stream anyMatch is as below:

anyMatch(Predicate predicate)

Here, the predicate is the condition on which each element will be checked. We can also provide multiple such predicates to stream. Below is a working example of how the function works. Extending and reusing our previous example.


Let's use the same array and see what happens with the anyMatch function.


Code:

import java.util.Arrays;
import java.util.function.IntPredicate;

public class StreamExample {

    public static void main(String[] args) {
        int[] arr = new int[] {3, 18, 15, 30, 60};

        IntPredicate isDivisibleByThree = a -> (a%3 == 0);
        IntPredicate isDivisibleByFive = a -> (a%5 == 0);

        IntPredicate isDivisibleByThreeAndFive = isDivisibleByThree
                .and(isDivisibleByFive);

        boolean isDivisibleByThreeResult = Arrays.stream(arr)
                .anyMatch(isDivisibleByThree);
        System.out.println("divisible by three: "+isDivisibleByThreeResult);
        boolean isDivisibleByFiveResult = Arrays.stream(arr)
                .anyMatch(isDivisibleByFive);
        System.out.println("divisible by five: "+isDivisibleByFiveResult);
        boolean isDivisibleByThreeAndFiveResult = Arrays.stream(arr)
                .anyMatch(isDivisibleByThreeAndFive);
        System.out.println("divisible by three and five: "
                +isDivisibleByThreeAndFiveResult);

    }

}

Output:

Important points to remember:

  1. What if the stream is empty? anyMatch - false, allMatch - true. There is the answer returned is the streams are empty.

  2. Both functions are short-circuiting terminal operations. This means, if an infinite stream is presented, the functions may terminate in finite time.


Stream.allMatch() returns true for empty stream and anyMatch returns false. This may sound a little bit confusing. but no worries friends, we are here to solve all the queries and questions.

allMatch() returns true.



Resource: Java67, javapoint


The Tech Platform

0 comments

Recent Posts

See All

Comments


bottom of page