Java - Stream Match Operation

Introduction

You can check if any elements in the stream match a predicate, if all elements match a predicate etc.

The following methods in the Stream interface are used to perform find and match operations:

boolean allMatch(Predicate<? super T> predicate)
boolean anyMatch(Predicate<? super T> predicate)
boolean noneMatch(Predicate<? super T> predicate)

IntStream, LongStream, and DoubleStream also contain the same methods for primitive types.

All match operations are terminal operations.

They are also short-circuiting operations.

A short-circuiting operation may not have to process the entire stream to return the result.

The following code shows how to perform find and match operations on streams.

Demo

import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    // Get the list of persons
    List<Person> persons = Person.persons();

    // Check if all persons are males
    boolean allMales = persons.stream().allMatch(Person::isMale);
    System.out.println("All males: " + allMales);

    // Check if any person was born in 1970
    boolean anyoneBornIn1970 = persons.stream().anyMatch(p -> p.getDob().getYear() == 1970);
    System.out.println("Anyone born in 1970: " + anyoneBornIn1970);

    // Check if any person was born in 1955
    boolean anyoneBornIn1955 = persons.stream().anyMatch(p -> p.getDob().getYear() == 1955);
    System.out.println("Anyone born in 1955: " + anyoneBornIn1955);

  }/*from  w ww.  j  av a  2 s. co  m*/
}

class Person {
  // An enum to represent the gender of a person
  public static enum Gender {
    MALE, FEMALE
  }

  private long id;
  private String name;
  private Gender gender;
  private LocalDate dob;
  private double income;

  public Person(long id, String name, Gender gender, LocalDate dob, double income) {
    this.id = id;
    this.name = name;
    this.gender = gender;
    this.dob = dob;
    this.income = income;
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Gender getGender() {
    return gender;
  }

  public boolean isMale() {
    return this.gender == Gender.MALE;
  }

  public boolean isFemale() {
    return this.gender == Gender.FEMALE;
  }

  public void setGender(Gender gender) {
    this.gender = gender;
  }

  public LocalDate getDob() {
    return dob;
  }

  public void setDob(LocalDate dob) {
    this.dob = dob;
  }

  public double getIncome() {
    return income;
  }

  public void setIncome(double income) {
    this.income = income;
  }

  public static List<Person> persons() {
    Person ken = new Person(1, "Java", Gender.MALE, LocalDate.of(1970, Month.MAY, 4), 6020.0);
    Person jeff = new Person(2, "Jack", Gender.MALE, LocalDate.of(1970, Month.JULY, 15), 7320.0);
    Person donna = new Person(3, "Javascript", Gender.FEMALE, LocalDate.of(1972, Month.JULY, 29), 8720.0);
    Person chris = new Person(4, "XML", Gender.MALE, LocalDate.of(1993, Month.DECEMBER, 16), 5800.0);
    Person laynie = new Person(5, "Json", Gender.FEMALE, LocalDate.of(2002, Month.DECEMBER, 13), 0.0);
    Person lee = new Person(6, "Database", Gender.MALE, LocalDate.of(2011, Month.MAY, 9), 2400.0);

    // Create a list of persons
    List<Person> persons = Arrays.asList(ken, jeff, donna, chris, laynie, lee);

    return persons;
  }

  @Override
  public String toString() {
    String str = String.format("(%s, %s, %s, %s, %.2f)", id, name, gender, dob, income);
    return str;
  }
}

Result