Java 8 has many features, but stream API is one of the important feature to simplify the code. In the current post, I will give some examples of anyMatch(), allMatch() and noneMatch.
anyMatch() - any element of a stream match with predicate then will return true else false.
allMatch() - all elements of a stream should match with predicate then will return true else false.
noneMatch() - all elements of stream should not match with predicate then will return true else false.
Example:-
package com.practice;
import java.util.ArrayList;
import java.util.List;
public class StreamMatchExample {
public static void main(String[] args) {
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("Mahesh", "Male", "abc@gmail.com"));
employeeList.add(new Employee("Sathish", "Male", "abc@gmail.com"));
employeeList.add(new Employee("Mahesh", "Male", "abc@gmail.com"));
employeeList.add(new Employee("Pooja", "Female", "abc@gmail.com"));
boolean allMatch = employeeList.stream().allMatch(
emp -> emp.getEmail().equalsIgnoreCase("abc@gmail.com"));
System.out.println("allMatch - " + allMatch);
boolean anyMatch = employeeList.stream().anyMatch(
emp -> emp.getName().equalsIgnoreCase("Mahesh"));
System.out.println("anyMatch - " + anyMatch);
boolean noneMatch = employeeList.stream().noneMatch(
emp -> emp.getName().equalsIgnoreCase("Anil"));
System.out.println("noneMatch - " + noneMatch);
}
}
Output :--
allMatch - true
anyMatch - true
noneMatch - true
Related Posts:-