Phân biệt anyMatch, allMatch và noneMatch trong Java Stream API

Trong Java Stream API có 3 method thường được sử dụng để đặt điều kiện trên một tập kết quả là allMatch, noneMatch và anyMatch, chúng trả về kết quả dạng boolean phụ thuộc vào method và điều kiện được sử dụng. 

Trong bài viết này chúng ta sẽ cùng nhau tìm hiểu khái niệm và cách sử dụng của từng method trên.

Chuẩn bị

Trước khi tìm hiểu các method trên chúng ta sẽ cần chuẩn bị Employee class sẽ được sử dụng trong các ví dụ sau.

public class Employee {
 
    private String name;
    private String account;
    private Integer salary;
 
    public Employee(String name, String account, Integer salary) {
        super();
        this.name = name;
        this.account = account;
        this.salary = salary;
    }
 
    @Override
    public String toString() {
 
        return "name: "+ this.name +" | account: "+ this.account +" | salary: "+this.salary;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getAccount() {
        return account;
    }
 
    public void setAccount(String account) {
        this.account = account;
    }
 
    public Integer getSalary() {
        return salary;
    }
 
    public void setSalary(Integer salary) {
        this.salary = salary;
    }
}

Stream#AnyMatch

Stream#anyMatch trả về TRUE nếu có một phần tử nào đó trong stream thỏa mãn điều kiện được đặt ra. Ngược lại Stream#anyMatch trả về FALSE nếu stream rỗng hoặc không có bất kỳ phần tử nào thỏa điều kiện.

public class StreamMatchEx {
 
    public static void main(String a[]) {
 
        List<Employee> empList = new ArrayList<>();
        empList.add(new Employee("Nataraja G", "Accounts", 8000));
        empList.add(new Employee("Nagesh Y", "Admin", 15000));
        empList.add(new Employee("Vasu V", "Security", 2500));
        empList.add(new Employee("Amar", "Admin", 12500));
 
        boolean result1 = empList.stream().anyMatch(emp->emp.getAccount().equals("Admin"));
        System.out.println(result1 );

       boolean result2 = empList.stream().anyMatch(emp->emp.getAccount().equals("Users"));
       System.out.println(result2 );
    }
}

Output:

True
False

Stream#allMatch

Stream#allMatch trả về TRUE nếu tất cả các phần tử trong stream đều thỏa mãn điều kiện cho trước, ngược lại FALSE.

public class StreamMatchEx {
 
    public static void main(String a[]) {
 
        List<Employee> empList = new ArrayList<>();
        empList.add(new Employee("Nataraja G", "Accounts", 8000));
        empList.add(new Employee("Nagesh Y", "Admin", 15000));
        empList.add(new Employee("Vasu V", "Security", 2500));
        empList.add(new Employee("Amar", "Admin", 12500));
 
        boolean result1 = empList.stream().allMatch(emp->emp.getSalary() >= 15000);
        System.out.println(result1 );

       boolean result2 = empList.stream().anyMatch(emp->emp.getSalary() >= 2500);
       System.out.println(result2 );
    }
}

Output:

False
True

Stream#noneMatch

Ngược lại với Stream#allMatch, Stream#noneMatch trả về TRUE nếu tất cả các phần tử trong stream đều không thỏa điều kiện được đặt ra.

public class StreamMatchEx {
 
    public static void main(String a[]) {
 
        List<Employee> empList = new ArrayList<>();
        empList.add(new Employee("Nataraja G", "Accounts", 8000));
        empList.add(new Employee("Nagesh Y", "Admin", 15000));
        empList.add(new Employee("Vasu V", "Security", 2500));
        empList.add(new Employee("Amar", "Admin", 12500));
 
        boolean result1 = empList.stream().noneMatch(emp->emp.getSalary() >= 50000);
        System.out.println(result1);

       boolean result2 = empList.stream().noneMatch(emp->emp.getSalary() >= 2500);
       System.out.println(result2 );
    }
}

Output:

True
False

Tóm lược

Một điểm lưu ý quan trọng bạn cần để tâm đó là 3 method trên không nhất thiết phải duyệt hết tất cả các phần tử trong stream để tính toán kết quả. Ví dụ hàm anyMatch đang duyệt đến phần tử thứ 5 trong tổng số 1000 phần tử, nếu phần tử thứ 5 này thỏa mãn điều kiện thì nó sẽ lập trức trả về kết quả TRUE mà không cần duyệt đến hết 1000 phần tử.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x