In this section, we will learn what the Stream noneMatch() method is and how to use it in Java.
What is Java Stream noneMatch() Method?
The Java Stream noneMatch() method is used to run a set of instructions on each element in a stream to see if they don’t match the specified conditions.
If the entire elements didn’t match the conditions, the final result of this method will be true. Otherwise, the value false will return instead.
Note: This method is a terminal and eager.
Java noneMatch() Method Syntax:
boolean noneMatch(Predicate<? super T> predicate)
noneMatch() Method Parameters:
The method takes one parameter and that is of type Predicate, which is a functional interface.
This means we need to pass a reference of another method as the argument of the noneMatch() method.
Here’s the signature of the reference method:
- The method has one parameter and that is of the same type as the elements in the stream.
- The return value of this method is of type Boolean.
Inside the body of this reference method is the place where we define the conditions that will be executed per each element of the stream.
noneMatch() Method Return Value:
If the result of the reference method for all the elements in the stream was “false” (which means none of them matched the conditions in the method’s body) then the result of the noneMatch() method is “true”.
Otherwise, even if one element in the stream matched the conditions in the reference method and caused this reference method to return true, then the eventual result of the noneMatch() method will be false.
Example: using Stream noneMatch() method
import java.util.stream.Stream; class Main{ public static void main(String[] args) { Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10,3,23,4,23,423,2,34,56,7,86,54,5); boolean result = stream.noneMatch(e->e>1000?true:false); System.out.println(result); } }
Output:
true