BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java 8 Stream – anyMatch() example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial we will see the example of Java 8 Stream anyMatch() method. This method returns true if any elements of the Stream matches the given predicate.

Lets see an example to understand the use of anyMatch() method.

Example: Java 8 Stream anyMatch() method

import java.util.List;
import java.util.function.Predicate;
import java.util.ArrayList;
class Student{
   int stuId;
   int stuAge; 
   String stuName; 
   Student(int id, int age, String name){ 
      this.stuId = id;  
      this.stuAge = age; 
      this.stuName = name; 
   } 
   public int getStuId() { 
      return stuId; 
   } 
   public int getStuAge() { 
      return stuAge; 
   }
   public String getStuName() { 
      return stuName; 
   } 
   public static List<Student> getStudents(){        
       List<Student> list = new ArrayList<>();        
       list.add(new Student(11, 28, "Lucy"));        
       list.add(new Student(28, 27, "Kiku"));        
       list.add(new Student(32, 30, "Dani"));        
       list.add(new Student(49, 27, "Steve"));       
       return list;    
   }
}
public class Example {   
   public static void main(String[] args) {
      Predicate<Student> p1 = s -> s.stuName.startsWith("S");
      Predicate<Student> p2 = s -> s.stuAge < 28 && s.stuName.startsWith("Z");       
      List<Student> list = Student.getStudents();

      /* anyMatch() method checks whether any Stream element matches
       * the specified predicate
       */
      boolean b3 = list.stream().anyMatch(p1);
      System.out.println(b3);
      boolean b4 = list.stream().anyMatch(p2);
      System.out.println(b4);
   }
}

Output:

true
false

In the above example we have two predicates, predicate p1 has condition that the student name starts with letter “S” and the predicate p2 has two conditions that the student name starts with letter “Z” and their age must be less than 28.

When we pass predicate as an argument to the anyMatch() method, it iterates over the elements of the stream to find the match. Since their is a match for predicate p1 (Student “Steve” name starts with “S”), it returns true, however none of the element matches predicate p2 so the anyMatch() method returns false for p2.

Related Posts:

  1. Java 8 – Stream Collectors examples
  2. Java 8 – filter null values from a Stream
  3. Java 8 – Stream Filter examples
  4. Java Lambda expressions

Top Related Articles:

  1. Java 9 – @SafeVarargs Annotation (with examples)
  2. Java 8 Stream – noneMatch() example
  3. Java 8 – Adding Days to the LocalDate
  4. Java 8 Interface Changes – default method and static method
  5. Java 8 forEach method with example

Tags: Java8-Features

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap