anyMatch() Method Example In Java 8

The anyMatch() method returns elements whether any match with the provided predicate. The anyMatch() method executes untill determining the result. Its terminate the loop execution when found any match and returns the value.

  1. The anyMatch method returns elements whether any match with the provided predicate.
  2. The anyMatch method executes untill determining the result.
  3. Its terminate the loop execution when found any match and returns the value.
  4. boolean result = list.stream().anyMatch(n -> n == 5); It will check whether 5 is available or not in the list.
  5. Return type Boolean, returns true if matches or returns false if does not matches.

Syntax:

 boolean anyMatch(Predicate predicate) 

Example of anyMatch() method

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

public class AnyMatchMethodExample {
 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 );

    //calling anymatch method
    boolean result = intList.stream().anyMatch( n -> n==14 ); 

    System.out.println( result )

  }
}


Output:
    true