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 – allMatch() example

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

In the last tutorials we have seen the anyMatch() and noneMatch() methods. In this guide, we will discuss stream allMatch() method, which returns true if all the elements of stream satisfy the given predicate, else it returns false.

Example: Stream allMatch()

In this example we have a stream of student details that consists student id, name and age. We have three predicates in this example, predicate p1 says that the student name starts with “A”, predicate p2 says that the student age is less than 40 and predicate p3 says that the age must be less than 40 and name should start with “P”.
allMatch() method returns false for predicate p1 because there are students whose names do not start with “A”.
allMatch() method returns true for predicate p2 because all students have age less than 40.
allMatch() method returns false for predicate p3 because details of all the students do not statisfy the condition of age <40 and name start with “P”.

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, "Tim")); 
      list.add(new Student(32, 30, "Daniel")); 
      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("A"); 
      Predicate<Student> p2 = s -> s.stuAge < 40; 
      Predicate<Student> p3 = s -> s.stuAge < 40 && s.stuName.startsWith("P");
      List<Student> list = Student.getStudents();

      /* allMatch() method returns true if all the elements of stream satisfy the 
       * given predicate, else it returns false 
       */ 

      /* This will return false because all student names do not start with "A"  
       */ 
      boolean b1 = list.stream().allMatch(p1); 
      System.out.println("list.stream().allMatch(p1): "+b1); 

      /* This will return true because all students have age less than 40  
       */ 
      boolean b2 = list.stream().allMatch(p2); 
      System.out.println("list.stream().allMatch(p2): "+b2); 

      /* This will return false because all the students do not satisfy the predicate: 
       * Age must be less than 40 and name starts with letter "P" 
       */ 
      boolean b3 = list.stream().allMatch(p3); 
      System.out.println("list.stream().allMatch(p3): "+b3);
   }
}

Output:

list.stream().allMatch(p1): false
list.stream().allMatch(p2): true
list.stream().allMatch(p3): false

Top Related Articles:

  1. Java 9 – @SafeVarargs Annotation (with examples)
  2. Java 8 – Adding Days to the LocalDate
  3. Java 8 forEach method with example
  4. Java String valueOf() method
  5. Java 8 features with examples

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