Java 8 Stream allMatch, anyMatch & noneMatch method Example

9 months ago codedestine 0

In this tutorial, we will learn about Java 8 stream allMatch(), anyMatch() and noneMatch() method.

java 8 stream allMatch

allMatch() Method :-

This method takes one Predicate as an argument and check whether all elements of this stream match the given predicate. If all elements matches then return true otherwise return false. If the stream is empty then true is returned.

Syntax :- boolean allMatch(Predicate<? super T> predicate)

Reference :-  allMatch() method JavaDocs 

Problem Statement :-

You have given a list of integers, check if all integers in a list are 2.

Java 7 :- To implement this, first iterate through the list of integers, check if any integers in a list is not 2, if yes return false else return true.


  public static void main(String[] args){
 
    Integer[] num = new Integer[]{3,2,2,2,2};
    List<Integer> numList = Arrays.asList(num);
 
    boolean isAllMatch = true;
    for(Integer i : numList){
      if(i != 2){
        isAllMatch = false;
        break;
      }
    }
    System.out.println(isAllMatch); 
  }

Java 8 :- Implementation using stream API.


  public static void main(String[] args){
 
    Integer[] num = new Integer[]{2,2,2,2,2};
    List<Integer> numList = Arrays.asList(num);
 
    boolean isAllMatch = numList.stream().allMatch( e -> e ==2); 
    System.out.println(isAllMatch);
  }

Result :- 

true

anyMatch() Method :-

This method takes one Predicate as an argument and check whether any elements of this stream match the given predicate. If any one elements matches then return true otherwise return false. If the stream is empty then false is returned.

Syntax :- boolean anyMatch(Predicate<? super T> predicate)

Reference :-  anyMatch() method JavaDocs 

Problem Statement :-

You have given a list of integers, check if any integers in a list is 3.

Java 7 :- To implement this, first iterate through the list of integers, check if any integers in a list is 3, if yes return true else return false.


  public static void main(String[] args){
 
    Integer[] num = new Integer[]{3,2,2,2,2};
    List<Integer> numList = Arrays.asList(num);
 
    boolean isAnyMatch = false;
    for(Integer i : numList){
      if(i == 3){
        isAnyMatch = true;
        break;
      }
    }
    System.out.println(isAnyMatch); 
  }  

Java 8 :- Implementation using stream API.

  public static void main(String[] args){
 
    Integer[] num = new Integer[]{3,2,2,2,2};
    List<Integer> numList = Arrays.asList(num);
 
    boolean isAnyMatch = numList.stream().anyMatch( e -> e == 3); 
    System.out.println(isAnyMatch);
  }

Result :- 

true

noneMatch() Method :-

This method takes one Predicate as an argument and check whether no elements of this stream match the given predicate. If no elements matches then return true otherwise even if any one element is matched the given predicate then return false. If the stream is empty then true is returned.

Syntax :- boolean noneMatch(Predicate<? super T> predicate)

Reference :-  noneMatch() method JavaDocs

Problem Statement :-

You have given a list of integers, check if no integers in a list is 3.

Java 7 :- To implement this, first iterate through the list of integers, check if any integers in a list is 3, if yes return false else return true.

  public static void main(String[] args){
 
    Integer[] num = new Integer[]{3,2,2,2,2};
    List<Integer> numList = Arrays.asList(num);
 
    boolean isNoneMatch = true;
    for(Integer i : numList){
      if(i == 3){
        isNoneMatch = false;
        break;
      }
    }
    System.out.println(isNoneMatch); 
  }

Java 8 :- Implementation using stream API.

  public static void main(String[] args){
 
    Integer[] num = new Integer[]{3,2,2,2,2};
    List<Integer> numList = Arrays.asList(num);
 
    boolean isNoneMatch = numList.stream().noneMatch( e -> e == 3); 
    System.out.println(isNoneMatch);
  }

Result :- 

false

That’s all for Java 8 Stream allMatch, anyMatch and noneMatch method Example. If you liked it, please share your thoughts in comments section and share it with others too.