In the last tutorials we have seen the anyMatch() and noneMatch() methods. In this guide, we will discuss stream allMatch() method, which returns true if all the elements of stream satisfy the given predicate, else it returns false.
Example: Stream allMatch()
In this example we have a stream of student details that consists student id, name and age. We have three predicates in this example, predicate p1 says that the student name starts with “A”, predicate p2 says that the student age is less than 40 and predicate p3 says that the age must be less than 40 and name should start with “P”.
allMatch() method returns false for predicate p1 because there are students whose names do not start with “A”.
allMatch() method returns true for predicate p2 because all students have age less than 40.
allMatch() method returns false for predicate p3 because details of all the students do not statisfy the condition of age <40 and name start with “P”.
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, "Tim")); list.add(new Student(32, 30, "Daniel")); 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("A"); Predicate<Student> p2 = s -> s.stuAge < 40; Predicate<Student> p3 = s -> s.stuAge < 40 && s.stuName.startsWith("P"); List<Student> list = Student.getStudents(); /* allMatch() method returns true if all the elements of stream satisfy the * given predicate, else it returns false */ /* This will return false because all student names do not start with "A" */ boolean b1 = list.stream().allMatch(p1); System.out.println("list.stream().allMatch(p1): "+b1); /* This will return true because all students have age less than 40 */ boolean b2 = list.stream().allMatch(p2); System.out.println("list.stream().allMatch(p2): "+b2); /* This will return false because all the students do not satisfy the predicate: * Age must be less than 40 and name starts with letter "P" */ boolean b3 = list.stream().allMatch(p3); System.out.println("list.stream().allMatch(p3): "+b3); } }
Output:
list.stream().allMatch(p1): false list.stream().allMatch(p2): true list.stream().allMatch(p3): false
Leave a Reply