I have a list of employees. They have isActive boolean field. I would like to divide employees into two lists: activeEmployees>
If you are open to using a third-party library, this will work using Collectors2.partition from Eclipse Collections.
PartitionMutableList partition =
employees.stream().collect(
Collectors2.partition(Employee::isActive, PartitionFastList::new));
List activeEmployees = partition.getSelected();
List formerEmployees = partition.getRejected();
You can also simplify things using ListIterate.
PartitionMutableList partition =
ListIterate.partition(employees, Employee::isActive);
List activeEmployees = partition.getSelected();
List formerEmployees = partition.getRejected();
PartitionMutableList is a type that extends from PartitionIterable. Every subtype of PartitionIterable has a collection for positive results getSelected() and negative results getRejected().
Note: I am a committer for Eclipse Collections.