Differences Between anyMatch() vs. allMatch() vs. noneMatch() in Java

Image from https://unsplash.com/.

Overview

1. Introduction

One common task when working with streams in Java is to perform boolean operations on its elements. To accomplish this, Java provides three methods: allMatch(), anyMatch(), and noneMatch().

In this tutorial, I'll show you the differences between these methods and how to use them in Java Streams with code examples.

2. What is allMatch() in Java Stream

allMatch() is a method in Java Streams that allows checking if all stream elements match a given predicate. It returns true if all stream elements satisfy the predicate and false otherwise.

Below is the signature for allMatch():

1boolean allMatch(Predicate<? super T> predicate)

Note: If you're not familiar with the ? super T expression, check out my Introduction to Java Generics.

Nothing better than a code example to understand a method's functionality. Suppose you have a list of integers and must check if all numbers are greater than zero. To validate that, you can pass the n -> n > 0 lambda expression to allMatch(), as follows:

1List<Integer> myNumbers = Arrays.asList(1, -1, 32, 41, 60);
2boolean allGreaterThanZero = myNumbers.stream().allMatch(n -> n > 0);
3System.out.print(allGreaterThanZero);

The code above outputs false because at least one element (-1) is less than 0.

3. What is anyMatch() in Java Stream

Suppose you must validate if a single element matches a predicate instead of checking all elements. For that, Java provides the anyMatch() method. It returns true if at least one element in the stream satisfies the predicate and false otherwise.

The anyMatch() have the exact same signature as allMatch() (except for the name, of course):

1boolean anyMatch(Predicate<? super T> predicate)

However, its internal functionality changes. You can check that with the example below:

1List<Integer> myNumbers = Arrays.asList(1, 3, 5, 6, 7);
2boolean anyEven = myNumbers.stream().anyMatch(n -> n % 2 == 0);
3System.out.print(anyEven);

In the example above, I've used a predicate to check if any element of numbers is even. To do that, I passed the n -> n % 2 == 0 as the _anyMatch()_argument. Finally, the stream terminates, and the code outputs true since 6 is even.

4. What is noneMatch() in Java Stream

Finally, to check if none of the elements matches a given predicate, use the noneMatch() method. It returns true if none of the elements in the stream satisfy the predicate and false otherwise.

Again, the noneMatch() signature is equivalent to the previous methods shown here:

1boolean noneMatch(Predicate<? super T> predicate)

To illustrate, suppose you have a list of strings, and you must check if none of the strings in the list are empty:

1List<String> strings = Arrays.asList("apple", "banana", "melon");
2boolean noneEmpty = strings.stream().noneMatch(String::isEmpty);
3System.out.print(noneEmpty);

In this example, I used the String::isEmpty method reference and passed it to noneMatch to accomplish the requirement. Note that String::isEmpty is equivalent to s -> s.isEmpty(). The code outputs true because all strings contain at least one character.

5. Summarizing the Differences

It's important to mention that each method short-circuits as soon as it determines the result. For example, if you use allMatch() on a stream and one element does not match the predicate, the method immediately returns false without processing more elements.

The table below summarizes the differences between the three methods:

Method Return True When Short-Circuits When
allMatch() all elements matches predicate finds the first element where predicate is false
anyMatch() any element matches predicate finds the first element where predicate is true
noneMatch() none of the elements matches predicate finds the first element where predicate is true

Another difference between these methods is the performance impact they may have. For example, using anyMatch() may be faster than allMatch() or noneMatch() since it needs to evaluate only one element. On the other hand, allMatch() and noneMatch() potentially need to process all elements in the stream to determine the result.

6. Conclusion

In summary, you must understand the functional differences between the three methods to use them effectively in Java Streams.

The key idea is to evaluate the requirements of your application and choose the correct method that satisfies them.