Ya no busques más por todo internet porque estás al lugar adecuado, tenemos la respuesta que buscas y sin liarte.
Ejemplo: cómo agregar una condición de filtro múltiple en la cadena de filtro de flujo de Java
import java.util.ArrayList;import java.util.List;import java.util.function.Predicate;import java.util.stream.Collectors;publicclassMainpublicstaticvoidmain(String[] args)List<Employee> employeeList =getEmployeesFromDataSource();//filter 1Predicate<Employee> isEmployeeActive = e -> e.getStatus()==EmployeeStatus.ACTIVE;//filter2Predicate<Employee> isAccountActive = e -> e.getAccount().getStatus()==AccountStatus.ACTIVE;//Active employeesString result = employeeList.stream().filter(isEmployeeActive).map(e -> e.getId().toString()).collect(Collectors.joining(",","[","]"));System.out.println("Active employees = "+ result);//Active employees with active accounts
result = employeeList.stream().filter(isEmployeeActive.and(isAccountActive)).map(e -> e.getId().toString()).collect(Collectors.joining(",","[","]"));System.out.println("Active employees with active accounts = "+ result);//Active employees with inactive accounts
result = employeeList.stream().filter(isEmployeeActive.and(isAccountActive.negate())).map(e -> e.getId().toString()).collect(Collectors.joining(",","[","]"));System.out.println("Active employees with inactive accounts = "+ result);//Inactive employees with inactive accounts
result = employeeList.stream().filter(isEmployeeActive.negate().and(isAccountActive.negate())).map(e -> e.getId().toString()).collect(Collectors.joining(",","[","]"));System.out.println("Inactive employees with inactive accounts = "+ result);privatestaticList<Employee>getEmployeesFromDataSource()List<Employee> employeeList =newArrayList<>();
employeeList.add(newEmployee(1L,"A","AA",EmployeeStatus.ACTIVE,newAccount(1001L,"Saving - 1001","Saving",AccountStatus.ACTIVE)));
employeeList.add(newEmployee(2L,"B","BB",EmployeeStatus.ACTIVE,newAccount(1002L,"Checking - 1002","Checking",AccountStatus.ACTIVE)));
employeeList.add(newEmployee(3L,"C","CC",EmployeeStatus.ACTIVE,newAccount(1003L,"Deposit - 1003","Deposit",AccountStatus.ACTIVE)));
employeeList.add(newEmployee(4L,"D","DD",EmployeeStatus.ACTIVE,newAccount(1004L,"Saving - 1004","Saving",AccountStatus.INACTIVE)));
employeeList.add(newEmployee(5L,"E","EE",EmployeeStatus.ACTIVE,newAccount(1005L,"Checking - 1005","Checking",AccountStatus.INACTIVE)));
employeeList.add(newEmployee(6L,"F","FF",EmployeeStatus.ACTIVE,newAccount(1006L,"Deposit - 1006","Deposit",AccountStatus.BLOCKED)));return employeeList;
Puntuaciones y reseñas
Si sostienes algún reparo y forma de progresar nuestro escrito eres capaz de dejar un paráfrasis y con gusto lo analizaremos.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)