Stream noneMatch() Method in Java with Examples

The noneMatch() method in Java's Stream API is an essential function employed to evaluate whether none of the elements in a given stream satisfy a particular condition. It is particularly useful when we need to prove that none of the items in a collection match a predicate, this method returns 'true' or 'false,' depending on whether no items meet the specified condition.

Method Signature

boolean: The noneMatch() method returns the boolean value true if there is no element in the stream that matches the given predicate else returns false.

noneMatch: It is the method name which indicates the operation that this method performs in the Stream API.

Predicate<? super T>: The parameter predicate is a functional interface from the Java util.function package. It defines a single method test() that takes an argument of type T (or its supertype) and returns a boolean value.

Example 1: Checking if all strings are non-empty

In this example, we have a list of words containing "Assam", "Banglore", and "Chennai". Using words.stream(), we convert the list into a stream. The noneMatch(String::isEmpty) checks if none of the strings in the stream are empty using a method reference to String::isEmpty. The output will be true because all strings in words are non-empty.

Filename: NoneMatchStringExample.java

Output:

 
None of the words are empty: true 

Example 2: Checking if any employee has an age less than 18

In this example, we define an Employee class with name and age attributes. We create three instances of Employee representing different ages. The employees.stream() converts the list of employees into a stream. The noneMatch(emp -> emp.getAge() < 18) checks if none of the employees have an age less than 18 using a lambda expression as a predicate. The output will be false because one employee (Bob) is underage (age < 18).

Filename: NoneMatchEmployeeExample.java

Output:

 
None of the employees are underage: false   

Example 3: Checking if a list of books contains a specific genre

In this example, we define a Book class with title and genre attributes. We create three instances of Book representing different genres. The books.stream() converts the list of books into a stream. The noneMatch(book -> book.getGenre().equals(genreToCheck)) checks if none of the books in the stream belong to the specified genre using a lambda expression as a predicate. The output will be true because none of the books in books belong to the genre "Science Fiction".

Filename: NoneMatchBookExample.java

Output:

 
None of the books are in the genre 'Science Fiction': true   

Example 4: Checking if all grades are above a certain threshold

We have a list grades containing doubles representing students' grades. The noneMatch(grade -> grade < passThreshold) checks if none of the grades in the stream are below the pass threshold using a lambda expression as a predicate. The output will be true because none of the grades in grades are below 70.0.

Filename: NoneMatchGradeExample.java

Output:

 
None of the grades are below 70.0: true   

Example 5: Checking if all transactions are above a certain amount

In this example, we define a nested Transaction class with id and amount attributes. We create a list of transactions containing instances of Transaction. The transactions.stream() converts the list of transactions into a stream. The noneMatch(transaction -> transaction.getAmount() < thresholdAmount) checks if none of the transactions in the stream have an amount less than thresholdAmount using a lambda expression as a predicate. The output will be false because transaction "T3" has an amount below $100.0.

Filename: TransactionExample.java

Output:

 
None of the transactions are below $100.0: false   

Example 6: Checking if a list of tasks are all marked as completed

In this example, we define a nested Task class with description and isCompleted attributes. We create a list of tasks containing instances of Task. The tasks.stream() converts the list of tasks into a stream. The noneMatch(task -> !task.isCompleted()) checks if none of the tasks in the stream are not completed using a lambda expression as a predicate. The output will be false because "Review code" is not completed.

Filename: TaskCompletionExample.java

Output:

 
All tasks are completed: false   

Example 7: Checking if a list of appointments are all confirmed

In this example, we define a nested Appointment class with time and isConfirmed attributes. We create a list of appointments containing instances of Appointment. The appointments.stream() converts the list of appointments into a stream. The noneMatch(appointment -> !appointment.isConfirmed()) checks if none of the appointments in the stream are unconfirmed using a lambda expression as a predicate. The output will be false because the "11:00 AM" appointment is not confirmed.

Filename: AppointmentConfirmationExample.java

Output:

 
All appointments are confirmed: false