Streams – anyMatch() operation

Stream anyMatch() in Java is a short-circuiting terminal operation, which means that we can not execute any other operation after it.

It returns whether any elements of the Stream match the provided predicate. However, it is a lazy evaluation, so it may not evaluate the predicate on all elements if not necessary for determining the result.

The anyMatch() method returns true if at least one of the elements matches the given predicate, otherwise false.


  boolean anyMatch(Predicate<? super T> predicate)

Java Stream anyMatch() operation – examples

Example 1
Check if any of the numbers is greater than 25.

class Test {

  public static void main(String[] args) {
    List<Integer> numbers = new ArrayList<>(Arrays.asList(20, 12, 12, 8, 9, 5, 32, 19));

            .anyMatch(number -> number > 25));

Output: true

Example 2
Check if any of the students have a grade higher than 8.

class Test {

  public static void main(String[] args) {
            .anyMatch(student -> student.getGrade() > 8));

  private static List<Student> getStudents() {
    List<Student> students = new ArrayList<>();
    students.add(new Student("Steve", "Rogers", 8));
    students.add(new Student("John", "Doe", 5));
    students.add(new Student("Melissa", "Smith", 7));
    students.add(new Student("Megan", "Norton", 4));
    students.add(new Student("Tom", "Johnson", 9));

    return students;

class Student {
  private String firstName;
  private String lastName;
  private int grade;

  public Student(String firstName, String lastName, int grade) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.grade = grade;

  public int getGrade() {
    return this.grade;

Output: true

That’s it!


Leave a Reply

Your email address will not be published.