IntStream anyMatch() method in Java

Java 8Object Oriented ProgrammingProgramming

The anyMatch() method in the IntStream class in Java returns whether any elements of this stream match the provided predicate.

The syntax is as follows

boolean anyMatch(IntPredicate predicate)

To work with the IntStream class in Java, import the following package

import java.util.stream.IntStream;

Here, the predicate parameter is a stateless predicate to apply to elements of this stream.

Create an IntStream and add some elements

IntStream intStream = IntStream.of(20, 40, 60, 80, 100);

Now, use the anyMatch() method to set for a condition. If any of the element match with the condition, TRUE is returned

boolean res = intStream.anyMatch(a -> a < 50);

The following is an example to implement IntStream anyMatch() method in Java

Example

 Live Demo

import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(20, 40, 60, 80, 100);
      boolean res = intStream.anyMatch(a -> a < 50);
      System.out.println(res);
   }
}

It finds a value less than 50, therefore true is returned

Output

true
raja
Updated on 30-Jul-2019 22:30:25

Advertisements