Java Stream with Multiple Filters Example


The Java Stream API that was introduced in Java 8 is the most powerful API when it comes to processing objects in a collection.

One of the most used methods from this API is the filter(Predicate predicate)

You can definitely make use of the filter method multiple times while processing the stream. Let's see some examples.


Example 1:

    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class MultipleFilterStreamExample {
    
        public static void main(String[] args) {
    
            List<String> namesList = new ArrayList<>();
            namesList.add("Mike");
            namesList.add("Alan");
            namesList.add("Aon");
            namesList.add("Alen");
            namesList.add("Bob");
            namesList.add("Andrew");
            namesList.add("Kate");
    
            List<String> filteredNameList = namesList.stream()
                    .filter(name -> name.startsWith("A"))
                    .filter(name -> name.endsWith("n"))
                    .collect(Collectors.toList());
    
            System.out.println(filteredNameList);
    
        }
    }

    In the above example we have made use of a chain of filter method to narrow the list to all names that start with "A" or end with "n"

    [Alan, Alen, Aon]


Example 2:

    package org.code2care.threading;
    
    public class Employee {
    
        private int empId;
        private String empName;
        private int empAge;
        private int empSal;
    
        public Employee(int empId, String empName, int empAge, int empSal) {
            this.empId = empId;
            this.empName = empName;
            this.empAge = empAge;
            this.empSal = empSal;
        }
    
        public int getEmpId() {
            return empId;
        }
    
        public void setEmpId(int empId) {
            this.empId = empId;
        }
    
        public String getEmpName() {
            return empName;
        }
    
        public void setEmpName(String empName) {
            this.empName = empName;
        }
    
        public int getEmpAge() {
            return empAge;
        }
    
        public void setEmpAge(int empAge) {
            this.empAge = empAge;
        }
    
        public int getEmpSal() {
            return empSal;
        }
    
        public void setEmpSal(int empSal) {
            this.empSal = empSal;
        }
    
        @Override
        public String toString() {
            return "Employee{" +
                    "empId=" + empId +
                    ", empName='" + empName + '\'' +
                    ", empAge=" + empAge +
                    ", empSal=" + empSal +
                    '}';
        }
    }
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class StreamFilterChainingExample {
    
        public static void main(String[] args) {
    
           Employee employee1 = new Employee(1,"Alex",22,54000);
           Employee employee2 = new Employee(2,"Alen",25,30000);
           Employee employee3 = new Employee(3,"Mike",26,38000);
    
           List<Employee> employeeList = new ArrayList<>();
           employeeList.add(employee1);
           employeeList.add(employee2);
           employeeList.add(employee3);
    
            List<Employee> employeeFilteredList = employeeList.stream()
                   .filter(employee -> employee.getEmpSal() > 40000)
                   .filter(employee -> employee.getEmpName().startsWith("A"))
                   .filter(employee -> employee.getEmpAge() > 20)
                           .collect(Collectors.toList());
    
            System.out.println(employeeFilteredList);
    
        }
    }

    [Employee{empId=1, empName='Alex', empAge=22, empSal=54000}]

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap