Java Streams anyMatch vs Streams noneMatch

In this tutorial, we will see the small difference between anyMatch and noneMatch method in streams java.

Lets say you have integer array and you want to see a particular number presented in array or not.

int[] numbers = new int[]{2,4,5,6};
 boolean isPresented = Arrays.stream(numbers).anyMatch(x->x==5);
System.out.println("Number presented: "+isPresented);

output is : true  . Since 5 is presented in numbers array

For same requirement if we use noneMatch will return false. That mean something is matching.

int[] numbers = new int[]{2,4,5,6};
        boolean isPresented = Arrays.stream(numbers).noneMatch(x->x==5);
        System.out.println("Number presented: "+isPresented);

output is : false . Since 5 is presented in numbers array hence noneMatch returned false.

Here is full video :

https://youtu.be/fSjT4NQheQE

Loading


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *