In the tutorial Java Stream allMatch() With Examples
we discussed allMatch() method that returns true when all elements of this stream match the provided predicate. There is
also a method noneMatch()
in Java Stream API that checks whether no elements of this stream match the provided predicate.
So noneMatch() can be termed as the opposite of allMatch() method.
noneMatch() Method in Java
Syntax of noneMatch() method is as given below.
boolean noneMatch(Predicate<? super T> predicate)
Here argument passed is of type Predicate functional interface. Method returns true if no element of the stream matches the provided predicate, otherwise false.
If the stream is empty then true is returned and the predicate is not evaluated.
noneMatch() is a short-circuiting terminal operation. It's a terminal operation means the stream pipeline is considered consumed, and can no longer be used after noneMatch() operation. It is also short-circuiting which means when presented with infinite input, it may terminate in finite time.
noneMatch() Java examples
1. Using noneMatch() to verify that all the elements in a List are greater than 20 or in other words there is no element in the list less than 20.
import java.util.Arrays; import java.util.List; public class NoneMatchDemo { public static void main(String[] args) { List<Integer> nameList = Arrays.asList(30, 40, 22, 55, 27, 65, 47); boolean result = nameList.stream().noneMatch(n -> n < 20); System.out.println(result); } }Output
true
2. Using noneMatch() along with filter method to check whether none of the students in Science stream have got less than 75 marks.
Student classpublic class Student { private int rollNo; private String name; private String stream; private int marks; Student(int rollNo, String name, String stream, int marks){ this.rollNo = rollNo; this.name = name; this.stream = stream; this.marks = marks; } public int getRollNo() { return rollNo; } public String getName() { return name; } public String getStream() { return stream; } public int getMarks() { return marks; } @Override public String toString() { return "Roll Number: " + getRollNo() + " Name: " + getName() + " Marks: " + getMarks(); } }
public class NoneMatchDemo { public static void main(String[] args) { List<Student> studentList = Arrays.asList(new Student(1, "Peter", "Science", 72), new Student(2, "Ram", "Science", 99), new Student(3, "Priscilla", "Art", 68), new Student(4, "Mahesh", "Art", 62), new Student(5, "Scott", "Commerce", 72)); boolean result = studentList.stream() .filter(s -> s.getStream().equals("Science")) .noneMatch(s -> s.getMarks() < 75); System.out.println(result); } }Output
false
Here result is false as out of the 2 students in Science stream one has marks less than 75 which means there is a match.
That's all for the topic Java Stream noneMatch() With Examples. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Java Stream anyMatch() With Examples
- Java Stream – Convert a Stream to Set
- LocalDateTime in Java With Examples
- Static Block in Java
- Producer-Consumer Problem Java Program
- Python Multi-threading Tutorial
- Fix LazyInitializationException: could not initialize proxy Error
- Spring Data JPA Auditing Example
No comments:
Post a Comment