Java Streams - Java Stream Match

The Streams API supports different types of match operations on stream elements.

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

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

The primitive type streams such as IntStream, LongStream, and DoubleStream also contain the same methods that work with a predicate and an optional one 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 match operations on streams.

import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.List;
//from  ww  w  . ja va2s.  co  m
public class Main {
  public static void main(String[] args) {
    List<Employee> persons = Employee.persons();

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

    // Check if any person was born in 1971
    boolean anyoneBornIn1971 = -> p.getDob().getYear() == 1971);
    System.out.println("Anyone born  in 1971:  " + anyoneBornIn1971);


class Employee {
  public static enum Gender {

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

  public Employee(long id, String name, Gender gender, LocalDate dob,
      double income) { = id; = name;
    this.gender = gender;
    this.dob = dob;
    this.income = income;
  public boolean isMale() {
    return this.gender == Gender.MALE;

  public LocalDate getDob() {
    return dob;
  public static List<Employee> persons() {
    Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971,
        Month.JANUARY, 1), 2343.0);
    Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972,
        Month.JULY, 21), 7100.0);
    Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973,
        Month.MAY, 29), 5455.0);
    Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974,
        Month.OCTOBER, 16), 1800.0);
    Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975,
        Month.DECEMBER, 13), 1234.0);
    Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976,
        Month.JUNE, 9), 3211.0);

    List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6);

    return persons;

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

The code above generates the following result.