Friday, January 21, 2022

Java Stream - allMatch() With Examples

In JavaStream API there are two methods anyMatch() and allMatch() where anyMatch() is used to check whether any element in the Stream matches the given condition, allMatch() is used to check if all the elements in the Stream match the given condition. In this tutorial we’ll try to understand allMatch() method in Java Stream.

Syntax of allMatch() method

boolean allMatch(Predicate<? super T> predicate)

Here predicate is a parameter of type Predicate functional interface that provides the condition to be applied to elements of this stream.

Method returns true if all elements of the stream match the provided predicate, otherwise false. If Stream is empty then also true is returned.

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. In this example we’ll use allMatch() to quickly verify if all the elements in the List are even numbers or not.

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

public class AllMatchStream {

  public static void main(String[] args) {
    List<Integer> numList = Arrays.asList(2, 4, 6, 12, 20);
    boolean result = numList.stream().allMatch(n -> (n%2 == 0));
    System.out.println(result);
  }
}

Output

true

2. In this example we’ll have a Stream of Employee objects where we want to check whether all employees have salary greater than 10000 or not.

Employee class

public class Employee {
  private String empId;
  private int age;
  private String name;
  private char gender;
  private int salary;
  Employee(String empId, int age, String name, char gender, int salary){
    this.empId = empId;
    this.age = age;
    this.name = name;
    this.gender = gender;
    this.salary = salary;
  }
  public String getEmpId() {
    return empId;
  }

  public int getAge() {
    return age;
  }

  public String getName() {
    return name;
  }

  public char getGender() {
    return gender;
  }

  public int getSalary() {
    return salary;
  }
  @Override
  public String toString() {
    return "Emp Id: " +  getEmpId() + " Name: " + getName() + " Age: " + getAge();
  }
}
public class AllMatchStream {

  public static void main(String[] args) {
    List<Employee> empList = Arrays.asList(new Employee("E001", 40, "Ram", 'M', 5000), 
                new Employee("E002", 35, "Shelly", 'F', 7000), 
                new Employee("E003", 24, "Mark", 'M', 9000), 
                new Employee("E004", 37, "Ritu", 'F', 11000),
                new Employee("E005", 32, "Anuj", 'M', 12000), 
        new Employee("E006", 28, "Amy", 'F', 14000)); 
    boolean result = empList.stream().allMatch(e -> e.getSalary() > 10000);
    System.out.println(result);
  }
}

Output

false
Result is false as not all of the employee objects satisfy the condition.

That's all for this topic Java Stream - allMatch() With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Java Stream - Convert Stream to Set
  2. Java Stream - Convert Stream to Map
  3. Java Stream - boxed() With Examples
  4. Java Stream API Interview Questions And Answers

You may also like-

  1. @FunctionalInterface Annotation in Java
  2. JDBC Tutorial - Java JDBC Overview
  3. Package in Java
  4. Java Collections Interview Questions And Answers
  5. Binary Tree Traversal Using Depth First Search Java Program
  6. Circular Dependency in Spring Framework
  7. Distributed Cache in Hadoop MapReduce
  8. Class And Object in Python

No comments:

Post a Comment