Java 8 Stream filter() Method Example


filter() method introduced in the Stream interface of Java 8 version under the package java.util.stream. filter() method is an Intermediate Operation that returns a stream consisting of the elements of this stream that match the given predicate.

Stream<T> filter(Predicate<? super T> predicate);

In this tutorial, we’ll solve the below questions with the help of filter() method:

1. Get all even numbers from a list of integers.
2. Get all numbers from the list of integers which is greater than 40.
3. Get all names, start with the letter S from a given list of strings.
4. Get all names from a given list of strings whose name length is greater than 4.
5. Get the list of all employees who belong to the SALES department.
6. Get the list of all employees whose salary is greater than 15000.
7. Get the list of all employees who belong to the IT department and their salary > 15000.

1. Even numbers

// List of Integers
 List<Integer> numbers = Arrays.asList(22, 79, 45, 88, 3, 9, 100);

        
List<Integer> evenNumbers = numbers.stream()
                                   .filter(num -> num % 2 == 0)
                                   .collect(Collectors.toList());

System.out.println("Even numbers: " + evenNumbers);
// Output: Even numbers: [22, 88, 100]

2. Numbers >  40

// List of Integers
 List<Integer> numbers = Arrays.asList(22, 79, 45, 88, 3, 9, 100);

        
List<Integer> greaterThan40 = numbers.stream()
									 .filter(num -> num > 40)
									 .collect(Collectors.toList());
									 
System.out.println("Numbers > 40: " + greaterThan40);
// Output: Numbers > 40: [79, 45, 88, 100]

3. Name start with S

// List of Strings
List<String> names = Arrays.asList("Sagar", "Priyanka", "Shilpa", "Atul", "Sumeet", "Manish");

List<String> nameStarWithS = names.stream()
								  .filter(name -> name.startsWith("S"))
								  .collect(Collectors.toList());
								  
System.out.println("Names start with S: " + nameStarWithS);
// Output: Names start with S: [Sagar, Shilpa, Sumeet]

4. Name length > 4

// List of Strings
List<String> names = Arrays.asList("Sagar", "Priyanka", "Shilpa", "Atul", "Sumeet", "Manish");

List<String> nameLength = names.stream()
							   .filter(name -> name.length() > 4)
							   .collect(Collectors.toList());
							   
System.out.println("Names whose length > 4: " + nameLength);
// Output: Names whose length > 4: [Sagar, Priyanka, Shilpa, Sumeet, Manish]

5. Employees from SALES

List<Employee> employees = Arrays.asList(
        new Employee(101, "Manish", 35000, "IT"),
		new Employee(12, "Priyanka", 12000, "SALES"),
		new Employee(67, "Kirti", 10000, "HR"),
		new Employee(55, "Sagar", 25000, "SALES"),
		new Employee(87, "Shilpa", 45000, "IT"),
		new Employee(31, "Manish", 15000, "HR"),
		new Employee(88, "Atul", 10000, "IT"),
		new Employee(46, "Nilam", 14000, "IT")
);

List<Employee> salesEmployees = employees.stream()
										 .filter(employee -> employee.getDepartment().equals("SALES"))
										 .collect(Collectors.toList());
										 
System.out.println("Sales employees: " + salesEmployees);
/**
Output: Sales employees:[
							Employee{id=12, name='Priyanka', salary=12000, department='SALES'},
							Employee{id=55, name='Sagar', salary=25000, department='SALES'}
						]
*/

6. Employees salary > 15000

List<Employee> employees = Arrays.asList(
        new Employee(101, "Manish", 35000, "IT"),
		new Employee(12, "Priyanka", 12000, "SALES"),
		new Employee(67, "Kirti", 10000, "HR"),
		new Employee(55, "Sagar", 25000, "SALES"),
		new Employee(87, "Shilpa", 45000, "IT"),
		new Employee(31, "Manish", 15000, "HR"),
		new Employee(88, "Atul", 10000, "IT"),
		new Employee(46, "Nilam", 14000, "IT")
);

List<Employee> salary15K = employees.stream()
									.filter(employee -> employee.getSalary() > 15000)
									.collect(Collectors.toList());
									
System.out.println("Employees whose salary > 15K: " + salary15K);
/**
Output: Employees whose salary > 15K: [
										Employee{id=101, name='Manish', salary=35000, department='IT'},
										Employee{id=55, name='Sagar', salary=25000, department='SALES'},
										Employee{id=87, name='Shilpa', salary=45000, department='IT'}
									  ]
*/

7. Employees from IT & salary > 15000

List<Employee> employees = Arrays.asList(
        new Employee(101, "Manish", 35000, "IT"),
		new Employee(12, "Priyanka", 12000, "SALES"),
		new Employee(67, "Kirti", 10000, "HR"),
		new Employee(55, "Sagar", 25000, "SALES"),
		new Employee(87, "Shilpa", 45000, "IT"),
		new Employee(31, "Manish", 15000, "HR"),
		new Employee(88, "Atul", 10000, "IT"),
		new Employee(46, "Nilam", 14000, "IT")
);

List<Employee> empFromITAndSal15k = employees.stream()
                                             .filter(employee -> employee.getDepartment().equals("IT") && employee.getSalary() > 15000)
                                             .collect(Collectors.toList());
				
System.out.println("Employees from IT department & salary > 15K: " + empFromITAndSal15k);
/**
Output: Employees from IT department & salary > 15K: [
														Employee{id=101, name='Manish', salary=35000, department='IT'},
														Employee{id=87, name='Shilpa', salary=45000, department='IT'}
													 ]
*/

References

  1. Java 8 Stream filter method
  2. Java 8 Stream API allMatch(), anyMatch() and noneMatch() method Example
  3. Java 8 – How to sort Set with stream.sorted()

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.