Java Stream allMatch() anyMatch() Methods Tutorial

In this section, we will learn what the Stream allMatch() and anyMatch() methods are and how to use them in Java.

What is Java Stream allMatch() Method?

The Java Stream allMatch() method is used to run a set of instructions on each element in a stream to see if they match the specified conditions.

If the entire elements matched the conditions, the final result of this method will be true. Otherwise, the value false will return instead.

Note: This method is a terminal and eager.

Java allMatch() Method Syntax:

boolean allMatch(Predicate<? super T> predicate)

allMatch() Method Parameters:

The method takes one parameter and that is of type Predicate, which is a functional interface.

This means we need to pass a reference to another method as the argument of the allMatch() method.

Here’s the signature of the reference method:

  • The method has one parameter and that is of the same type as the elements in the stream.
  • The return value of this method is of type Boolean.

Inside the body of this reference method is the place where we define the conditions that will be executed per each element of the stream.

allMatch() Method Return Value:

If the result of the reference method for all the elements in the stream was “true” (which means all of them matched the conditions in the body of the method) then the result of the allMatch() method is “true” as well.

Otherwise, even if one element in the stream didn’t match the conditions in the reference method and caused this method to return false, then the result of the allMatch() method will be false.

Example: using Stream allMatch() method

import java.util.stream.Stream;
class Main{
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10,3,23,4,23,423,2,34,56,7,86,54,5);
        
        boolean result = stream.allMatch(e->e<1000?true:false);
        System.out.println(result);
    }
}

Output:

true

What is Java Stream anyMatch() Method?

The Java Stream anyMatch() method is used to check the elements of a stream to see if any of them match a condition.

If even one element in the stream matched the condition defined in the method, then the final result of this method will be true as well.

Note: this method is a terminal and eager one.

Java anyMatch() Method Syntax:

boolean anyMatch(Predicate<? super T> predicate)

anyMatch() Method Parameters:

The method takes one parameter and that is of type Predicate, which is a functional interface.

This means we need to pass a reference to another method as the argument of the anyMatch() method.

Here’s the signature of the reference method:

  • The method has one parameter and that is of the same type as the elements in the stream.
  • The return value of this method is of type Boolean.

Inside the body of this reference method is the place where we define the conditions that will be executed per each element of the stream.

anyMatch() Method Return Value:

The return value of the anyMatch() method will be true if just one element in the stream caused the reference method to return true.

Otherwise, if the reference method returned false for the entire elements in the stream, then the result of the anyMatch() method will be false as well.

Example: using Stream anyMatch() method

import java.util.stream.Stream;
class Main{
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10,3,23,4,23,423,2,34,56,7,86,54,5);
        
        boolean result = stream.anyMatch(e->e>100?true:false);
        System.out.println(result);
    }
}

Output:

true

The entire elements of this stream are less than the value 100 except for one, which is the value 423. And because the result of the reference method for this element is true, then the result of the anyMatch() method will be true as well.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies