Java Stream noneMatch() Example

1. Introduction

This tutorial explains the noneMatch() method of the Java Stream API. The noneMatch() method is used to check if no elements of this stream match the provided predicate. It's a short-circuiting terminal operation that can be used for validations or conditions across collections.

Key Points

1. noneMatch() checks whether no elements of the stream match the given predicate.

2. It returns a boolean value: true if no elements match the predicate, otherwise false.

3. It is a short-circuiting operation, meaning it does not need to process the entire stream if a match is found early.

2. Program Steps

1. Create a Stream of elements.

2. Apply noneMatch() with a specific predicate to the Stream.

3. Print the result of the noneMatch() operation.

3. Code Program


public class StreamNoneMatchExample {

    public static void main(String[] args) {
        // Stream of integers
        Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5);

        // Check if no element is greater than 10
        boolean noneAboveTen = numberStream.noneMatch(num -> num > 10);
        System.out.println("No elements greater than 10: " + noneAboveTen);

        // Stream of strings
        Stream<String> stringStream = Stream.of("apple", "banana", "cherry");

        // Check if no element contains the letter 'z'
        boolean noneContainZ = stringStream.noneMatch(str -> str.contains("z"));
        System.out.println("No elements contain 'z': " + noneContainZ);


No elements greater than 10: true
No elements contain 'z': true


1. Stream.of(1, 2, 3, 4, 5) creates a stream of integers.

2. numberStream.noneMatch(num -> num > 10) checks if there are no numbers greater than 10 in the stream, returning true because all numbers are 10 or below.

3. Stream.of("apple", "banana", "cherry") creates a stream of string values.

4. stringStream.noneMatch(str -> str.contains("z")) evaluates whether no strings contain the letter 'z', which is true, as none of the strings 'apple', 'banana', or 'cherry' contain 'z'.