In Java 8 we can filter a List/Collection using Java 8 Streams's filter(), we will use "Employee" class list "employees" to filter and collect result in a list.
class Employee {
	private int id;
	private String name;

	// Getter Setters, constructor and toString()

}


1) Java 8 Streams filter() example

		List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));
		employees.add(new Employee(5, "John"));
		employees.add(new Employee(4, "Ben"));

		System.out.println("employees before filter");
		employees.forEach(i -> System.out.println(i));

		System.out.println("employees after filter");

		// this will filter employees with name "John"
		employees.stream().filter(i -> i.getName().equals("John")).forEach(i -> System.out.println(i));
Output: Output of above code will look something like this:

employees before filter

Employee [id=3, name=Zenny]
Employee [id=1, name=John]
Employee [id=2, name=Ben]
Employee [id=5, name=John]
Employee [id=4, name=Ben]

employees after filter

Employee [id=1, name=John]
Employee [id=5, name=John]


2) Java 8 Streams filter() and collect() example

		List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));
		employees.add(new Employee(5, "John"));
		employees.add(new Employee(4, "Ben"));

		System.out.println("employees before filter");
		employees.forEach(i -> System.out.println(i));

		System.out.println("employees after filter");

		// this will filter employees with name "John" in another list
		List<Employee> filteredEmployees = employees.stream().filter(i -> i.getName().equals("John"))
				.collect(Collectors.toList());
		filteredEmployees.forEach(i -> System.out.println(i));
Output: Output of above code will look something like this:

employees before filter

Employee [id=3, name=Zenny]
Employee [id=1, name=John]
Employee [id=2, name=Ben]
Employee [id=5, name=John]
Employee [id=4, name=Ben]

employees after filter

Employee [id=1, name=John]
Employee [id=5, name=John]


3) How to use a predicate in Java 8

We can also pass a predicate in filter() function to make filtering on complex logic, as shown in the example below:
		List<Employee> employees = new ArrayList<>();
		employees.add(new Employee(3, "Zenny"));
		employees.add(new Employee(1, "John"));
		employees.add(new Employee(2, "Ben"));
		employees.add(new Employee(5, "John"));
		employees.add(new Employee(4, "Ben"));

		System.out.println("employees before filter");
		employees.forEach(i -> System.out.println(i));

		System.out.println("employees after filter");

		// this predicate will filter employees with name "John"
		Predicate<Employee> predicate = new Predicate<Employee>() {
			@Override
			public boolean test(Employee t) {
				return t.getName().equals("John");
			}
		};

		// this will filter employees with name "John" in another list
		List<Employee> filteredEmployees = employees.stream().filter(predicate).collect(Collectors.toList());
		filteredEmployees.forEach(i -> System.out.println(i));
	
Output: Output of above code will look something like this:

employees before filter

Employee [id=3, name=Zenny]
Employee [id=1, name=John]
Employee [id=2, name=Ben]
Employee [id=5, name=John]
Employee [id=4, name=Ben]

employees after filter

Employee [id=1, name=John]
Employee [id=5, name=John]
  • By Techburps.com
  • Oct 6, 2018
  • Java 8