In the last tutorial we discussed java stream anyMatch() method. The stream noneMatch() method works just opposite to the anyMatch() method, it returns true if none of the stream elements match the given predicate, it returns false if any of the stream elements matches the condition specified by the predicate.
Java Stream noneMatch() example
import java.util.List; import java.util.function.Predicate; import java.util.ArrayList; class Student{ int stuId; int stuAge; String stuName; Student(int id, int age, String name){ this.stuId = id; this.stuAge = age; this.stuName = name; } public int getStuId() { return stuId; } public int getStuAge() { return stuAge; } public String getStuName() { return stuName; } public static List<Student> getStudents(){ List<Student> list = new ArrayList<>(); list.add(new Student(11, 28, "Lucy")); list.add(new Student(28, 27, "Kiku")); list.add(new Student(32, 30, "Dani")); list.add(new Student(49, 27, "Steve")); return list; } } public class Example { public static void main(String[] args) { Predicate<Student> p1 = s -> s.stuName.startsWith("L"); Predicate<Student> p2 = s -> s.stuAge < 28 && s.stuName.startsWith("P"); List<Student> list = Student.getStudents(); /* noneMatch() method returns true if none of the stream elements matches * the given predicate */ /* This will return false because there is a element in the stream that * matches the condition specified in the predicate p1. * Condition: Student Name should start with letter "L" * Stream element matched: Lucy */ boolean b1 = list.stream().noneMatch(p1); System.out.println("list.stream().noneMatch(p1): "+b1); /* This will return true because none of the elements * matches the condition specified in the predicate p2. * Condition: Student Name should start with letter "P" and age must be <28 * Stream element matched: none */ boolean b2 = list.stream().noneMatch(p2); System.out.println("list.stream().noneMatch(p2): "+b2); } }
Output:
list.stream().noneMatch(p1): false list.stream().noneMatch(p2): true
The first call to the noneMatch() method with predicate p1 as argument returns false because there is an element in the stream that matches the condition specified by p1.
The second call to the noneMatch() method with predicate p2 as argument returns true because there is no element in the stream that matches the given conditions.
Leave a Reply