LongStream anyMatch() method in Java

Java 8Object Oriented ProgrammingProgramming

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

The syntax is as follows.

boolean anyMatch(LongPredicate predicate)

Here, the parameter predicate is the stateless predicate to apply to elements of this stream. The LongPredicate represents a predicate of one long-valued argument.

To use the LongStream class in Java, import the following package.

import java.util.stream.LongStream;

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

Example

 Live Demo

import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(100L, 150L, 200L, 300L, 400L, 500L);
      boolean res = longStream.anyMatch(a -> a > 350L);
      System.out.println("Do any of the element match the predicate? "+res);
   }
}

Here is the output. It returns TRUE since atleast one of the element match the predicate mentioned in the anyMatch() method.

Output

Do any of the element match the predicate? true
raja
Updated on 30-Jul-2019 22:30:25

Advertisements