In this article we will see how to use allMatch() and anyMatch() functions in java 8 with examples, while using as intemediatetry functions in a stream.
class Employee {
	private int id;
	private String name;

	// Getter Setters, constructor and toString()

}


1) anyMatch() function in java 8

anyMatch(), return true if any of the element in a stream matches the specified condition passed in anyMatch() function, returns false otherwise.
List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));

		boolean match = employees.stream().anyMatch(i -> i.getName().equals("Zenny"));

		System.out.println("Any Match:" + match);
Output: Output of above code will look something like this:


Any Match:true



2) allMatch() function in java 8

allMatch(), return true if all of the element in a stream matches the specified condition passed in allMatch() function, returns false otherwise.
		List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));

		boolean match = employees.stream().allMatch(i -> i.getName().equals("Zenny"));

		System.out.println("All match:" + match);
	
Output: Output of above code will look something like this:


All match:false


3) noneMatch() function in java 8

noneMatch(), return true if none of the element in a stream matches the specified condition passed in noneMatch() function, returns false otherwise.
		List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));

		boolean match = employees.stream().noneMatch(i -> i.getName().equals("Zenny"));

		System.out.println("None match:" + match);
	
	
Output: Output of above code will look something like this:


None match:false


  • By Techburps.com
  • Oct 6, 2018
  • Java 8