how to add multiple filter condition in java stream filter chain

Solutions on MaxInterview for how to add multiple filter condition in java stream filter chain by the best coders in the world

showing results for - "how to add multiple filter condition in java stream filter chain"
Cheyenne
13 Feb 2019
1import java.util.ArrayList;
2import java.util.List;
3import java.util.function.Predicate;
4import java.util.stream.Collectors;
5 
6public class Main 
7{
8    public static void main(String[] args) 
9    {
10        List<Employee> employeeList = getEmployeesFromDataSource();
11         
12        //filter 1
13        Predicate<Employee> isEmployeeActive = e -> e.getStatus() == EmployeeStatus.ACTIVE;
14 
15        //filter2
16        Predicate<Employee> isAccountActive = e -> e.getAccount().getStatus() == AccountStatus.ACTIVE;
17         
18        //Active employees
19        String result = employeeList.stream()
20                .filter(isEmployeeActive)
21                .map(e -> e.getId().toString())
22                .collect(Collectors.joining(",", "[", "]"));
23         
24        System.out.println("Active employees = " + result);
25         
26        //Active employees with active accounts
27        result = employeeList.stream()
28                .filter(isEmployeeActive.and(isAccountActive))
29                .map(e -> e.getId().toString())
30                .collect(Collectors.joining(",", "[", "]"));
31         
32        System.out.println("Active employees with active accounts = " + result);
33         
34        //Active employees with inactive accounts
35        result = employeeList.stream()
36                .filter(isEmployeeActive.and(isAccountActive.negate()))
37                .map(e -> e.getId().toString())
38                .collect(Collectors.joining(",", "[", "]"));
39         
40        System.out.println("Active employees with inactive accounts = " + result);
41         
42        //Inactive employees with inactive accounts
43        result = employeeList.stream()
44                .filter(isEmployeeActive.negate().and(isAccountActive.negate()))
45                .map(e -> e.getId().toString())
46                .collect(Collectors.joining(",", "[", "]"));
47         
48        System.out.println("Inactive employees with inactive accounts = " + result);
49    }
50     
51    private static List<Employee> getEmployeesFromDataSource() {
52        List<Employee> employeeList = new ArrayList<>();
53         
54        employeeList.add(new Employee(1L, "A", "AA", EmployeeStatus.ACTIVE, 
55                new Account(1001L, "Saving - 1001", "Saving", AccountStatus.ACTIVE)));
56        employeeList.add(new Employee(2L, "B", "BB", EmployeeStatus.ACTIVE, 
57                new Account(1002L, "Checking - 1002", "Checking", AccountStatus.ACTIVE)));
58        employeeList.add(new Employee(3L, "C", "CC", EmployeeStatus.ACTIVE, 
59                new Account(1003L, "Deposit - 1003", "Deposit", AccountStatus.ACTIVE)));
60        employeeList.add(new Employee(4L, "D", "DD", EmployeeStatus.ACTIVE, 
61                new Account(1004L, "Saving - 1004", "Saving", AccountStatus.INACTIVE)));
62        employeeList.add(new Employee(5L, "E", "EE", EmployeeStatus.ACTIVE, 
63                new Account(1005L, "Checking - 1005", "Checking", AccountStatus.INACTIVE)));
64        employeeList.add(new Employee(6L, "F", "FF", EmployeeStatus.ACTIVE, 
65                new Account(1006L, "Deposit - 1006", "Deposit", AccountStatus.BLOCKED)));
66         
67        return employeeList;
68    }
69}
70
queries leading to this page
java filter two streamsfilter multiple times on one streamwe can give 2 filters in a stream javahow to have two filters in java 8filter with multiple conditions in java 8apply more filters to stram javajava stream with multiple filtershow to write multiple condition in filter method in java 8multiple filter in java streamjava stream multiple filters or onejava stream filter multiplemultiple filters in java 8 streamuse multiple filter function in javajava 8 stream filter chaining or adding multiple conditionsfilter set on multiple conditions javajava 8 stream filter chaining vs adding multiple conditionsjava stream filter multiple conditionsjava stream filter on multiple conditionshow to use multiple filter in stream in java 8multiple filters in stream java 8 in one linestream filter with multiple conditionsmultiple filters in java 8java multiple streams with multiple filtersmultiple filters java stramhow to write multiple condition in filter method in java8add multiple conditions in filter in java 8filter on multiple values of array in java lamdafilter stream multiple functionsjava stream multiple filters is emptyhow to use multiple conditions in filter in java 8multi filter or single filter stream javamultiple filters in stream java 8stream api use multiple filterjava 8 stream multiple filtersstream with two filterstream filter with multiple condition javajava stream multiple filter 28or operation 29stream multiple filterstream in java chaining filterjava 8 stream filter with multiple conditionsfilter stream java 8 by multiple conditionshow to multiple filter in one filter javajava stream multiple filtersjava stream multiple filters between filtersfilter chaining in java 8java stream multiple filterstream filter multiple conditionsmultiple filter in stream java 8stream multiple filters filter multiple filter statement javaadd two filters for list of objectspass multiple filter in streamjava stream filter more than oneobjects filter multiple conditionsmultiple conditions inside filter in java streamshow to add multiple filter condition in java stream filter chain