In JavaStream API there is a method allMatch() which is used to check if all the elements in the Stream match the given condition. There is also a noneMatch() method in Java Stream API to check whether no elements of this stream match the provided predicate.
Syntax of noneMatch() method
boolean noneMatch(Predicate<? super T> predicate)
Here predicate is a parameter of type Predicate functional interface that provides the condition to be applied to elements of this stream.
Method returns true if no elements of the stream match the provided predicate, otherwise false. If Stream is empty then also true is returned.
noneMatch is a short-circuiting terminal operation. It’s a terminal operation means the stream pipeline is considered consumed after allMatch() operation, and can no longer be used. It is also short-circuiting which means when presented with infinite input, it may terminate in finite time.
noneMatch() Java examples
1. In this example we’ll use noneMatch() to quickly verify if all the elements in the List are less than 50. If we use noneMatch with the conditon to check if any element is greater than 50 then it should return true for our requirement (all elements less than 50).
import java.util.Arrays; import java.util.List; public class NoneMatchStream { public static void main(String[] args) { List<Integer> numList = Arrays.asList(30, 40, 22, 45, 27, 49, 47); boolean result = numList.stream().noneMatch(n -> n > 50); System.out.println(result); } }
Output
true2. In this example we’ll have a Stream of Employee objects where we want to check if none of the employee has salary less than 8000.
Employee class
public class Employee { private String empId; private int age; private String name; private char gender; private int salary; Employee(String empId, int age, String name, char gender, int salary){ this.empId = empId; this.age = age; this.name = name; this.gender = gender; this.salary = salary; } public String getEmpId() { return empId; } public int getAge() { return age; } public String getName() { return name; } public char getGender() { return gender; } public int getSalary() { return salary; } @Override public String toString() { return "Emp Id: " + getEmpId() + " Name: " + getName() + " Age: " + getAge(); } }
public class NoneMatchStream { public static void main(String[] args) { List<Employee> empList = Arrays.asList(new Employee("E001", 40, "Ram", 'M', 5000), new Employee("E002", 35, "Shelly", 'F', 7000), new Employee("E003", 24, "Mark", 'M', 9000), new Employee("E004", 37, "Ritu", 'F', 11000), new Employee("E005", 32, "Anuj", 'M', 12000), new Employee("E006", 28, "Amy", 'F', 14000)); boolean result = empList.stream().noneMatch(e -> e.getSalary() < 8000); System.out.println(result); } }
Output
false
Result is false as not there are employees with salary less than 8000.
That's all for this topic Java Stream - noneMatch() With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Advanced Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment