January 11, 2022

Java Stream allMatch() With Examples

In the tutorial Java Stream anyMatch() With Examples we discussed anyMatch() method that returns true if any element matches the Predicate. There is also a method allMatch() in Java Stream API that checks whether all elements of this stream match the provided predicate.

allMatch() Method in Java

Syntax of allMatch() method is as given below.

boolean allMatch(Predicate<? super T> predicate)

Here argument passed is of type Predicate functional interface. Method returns true if all elements of the stream match the provided predicate, otherwise false.

If the stream is empty then true is returned and the predicate is not evaluated.

allMatch() is a short-circuiting terminal operation. It's a terminal operation means the stream pipeline is considered consumed after allMatch() operation, and can no longer be used. It is also short-circuiting which means when presented with infinite input, it may terminate in finite time.

allMatch() Java examples

1. Using allMatch() to verify that all the elements in a List are less than 20.

import java.util.Arrays;
import java.util.List;

public class AllMatchDemo {
  public static void main(String[] args) {
    List<Integer> nameList = Arrays.asList(10, 13, 12, 15, 17, 5, 7);
    boolean result = nameList.stream().allMatch(n -> n < 20);
    System.out.println(result);
  }
}
Output
true

2. Using allMatch() along with filter method to check whether all the students in Science stream have got more than 75 marks or not.

Student class
public class Student {
  private int rollNo;
  private String name;
  private String stream;
  private int marks;
  Student(int rollNo, String name, String stream, int marks){
    this.rollNo = rollNo;
    this.name = name;
    this.stream = stream;
    this.marks = marks;
  }
  public int getRollNo() {
    return rollNo;
  }
  public String getName() {
    return name;
  }

  public String getStream() {
    return stream;
  }

  public int getMarks() {
    return marks;
  }

  @Override
  public String toString() {
    return "Roll Number: " +  getRollNo() 
        + " Name: " + getName() + " Marks: " + getMarks();
  }
}
public class AllMatchDemo {
  public static void main(String[] args) {
    List<Student> studentList = Arrays.asList(new Student(1, "Peter", "Science", 75),
              new Student(2, "Ram", "Science", 99),
              new Student(3, "Priscilla", "Art", 68),
              new Student(4, "Mahesh", "Art", 62),
              new Student(5, "Scott", "Commerce", 72));
    boolean result = studentList.stream()
          .filter(s -> s.getStream().equals("Science"))
          .allMatch(s -> s.getMarks() > 75);
    System.out.println(result);
  }
}
Output
false

Here result is false as out of the 2 students in Science stream one has marks not greater than 75.

That's all for the topic Java Stream allMatch() With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment