allMatch() Method Example In Java 8

The stream allMatch() method returns whether all elements of this stream match the provided predicate. Its check whether all elements are present or not in the given stream.

  1. The allMatch method returns whether all elements of this stream match the provided predicate.
  2. Its check whether all elements are present or not in the given stream.
  3. boolean result = list.stream().anyMatch(n -> n == 5); It will check whether 5 is available in the list.
  4. Return type Boolean, returns true if all elements are present

Syntax:

 boolean allMatch(Predicate predicate) 

Example of allMatch() method

//Importing required classes
import java.util.ArrayList;
import java.util.List;
import java.util.stream.*;

public class AllMatchMethodExample {
 public static void main(String args[]){
    //Declaring List
    List<Integer> intList = new ArrayList< Integer >();
    intList.add( 6 );
    intList.add( 11 );
    intList.add( 14 );
    intList.add( 7 );
    intList.add( 3 );
    intList.add( 2 );
    intList.add( 16 );
    intList.add( 15 );
    intList.add( 19 );

    boolean result = intList.stream().anyMatch( n -> n%2 == 0 ); 

    System.out.println( result )

  }
}

Output:
    false

Example of allMatch() method

//Importing required classes
import java.util.ArrayList;
import java.util.List;
import java.util.stream.*;

public class AllMatchMethodExample {
 public static void main(String args[]){
  //Declaring List
  List<Integer> intList = new ArrayList< Integer >();
  intList.add(6);
  intList.add(14);
  intList.add(2);
  intList.add(16);

  boolean result = intList.stream().anyMatch( n -> n%2 == 0 ); 

  System.out.println( result )

  }
}

Output:
    true