Course contentsShow
← Java
Lesson 1537 of 2,87033. Streams API: Terminal OperationsFree lesson

noneMatch: Testing If No Elements Match

Use noneMatch to confirm that no elements in the stream satisfy the predicate, the logical inverse of anyMatch.

noneMatch: Testing If No Elements Match

You'll learn how to use noneMatch to verify that no elements in a stream satisfy a given condition, completing the trio of match operations.

What is noneMatch?

The noneMatch terminal operation checks whether no elements in the stream match the provided predicate. It returns true only if every single element fails the test—in other words, it's the logical inverse of anyMatch. If even one element matches, noneMatch returns false.

Think of it like checking a guest list: noneMatch confirms that none of the people on your list are wearing red shoes. If you find even one person in red shoes, the answer becomes false.

Basic Syntax

boolean noneMatch(Predicate<? super T> predicate)

How It Works

noneMatch evaluates elements lazily and short-circuits as soon as it finds a matching element. The moment any element satisfies the predicate, it immediately returns false without processing the rest of the stream.

List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10);

boolean hasNoOdds = numbers.stream()
    .noneMatch(n -> n % 2 != 0);
// true, because no numbers are odd

boolean hasNoLargeNumbers = numbers.stream()
    .noneMatch(n -> n > 5);
// false, because 6, 8, and 10 are all > 5

Relationship to Other Match Operations

noneMatch(predicate) is equivalent to !anyMatch(predicate), but using noneMatch makes your intent clearer and more readable.

// These are logically equivalent:
noneMatch(n -> n < 0)
!anyMatch(n -> n < 0)

Key Takeaway: Use noneMatch to confirm that no stream elements satisfy a condition; it short-circuits on the first match, making it efficient for large streams and conveying intent more clearly than negating anyMatch.