Friday, January 21, 2022

Java Stream - anyMatch() With Examples

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

Syntax of anyMatch() method

boolean anyMatch(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 any element of the stream matches the provided predicate, otherwise false. If Stream is empty then false is returned and the predicate is not evaluated.

anyMatch is a short-circuiting terminal operation. It’s a terminal operation means the stream pipeline is considered consumed after anyMatch() 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.

anyMatch() Java examples

1. In this example we’ll use anyMatch() to quickly verify if any integer in a List of integers is greater than 100.

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

public class AnyMatchStream {
  public static void main(String[] args) {
    List<Integer> numList = Arrays.asList(97, 102, 60, 15, 20);
    boolean result = numList.stream().anyMatch(n -> n > 100);
    System.out.println(result);
  }
}

Output

true

2. In this example we’ll have a Stream of Employee objects where we want to check if there is any employee with age greater than 60.

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 AnyMatchStream {
  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().anyMatch(e -> e.getAge() > 60);
    System.out.println(result);
  }
}

Output

false

Result is false as there is no employee matching the condition age greater than 60.

That's all for this topic Java Stream - anyMatch() 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 - noneMatch() With Examples
  2. Java Stream - Collectors.joining() With Examples
  3. Java Stream - Convert Stream to Map
  4. Java Stream - Convert Stream to Array
  5. Spliterator in Java

You may also like-

  1. Java ReentrantLock With Examples
  2. Unmodifiable or Immutable Set in Java
  3. Java Automatic Numeric Type Promotion
  4. Core Java Basics Interview Questions And Answers
  5. Doubly Linked List Implementation Java Program
  6. Spring depends-on Attribute and @DependsOn With Examples
  7. What is SafeMode in Hadoop
  8. Constructor in Python - __init__() function

No comments:

Post a Comment