In my previous question - How to filter the age while grouping in map with list I was able to find the name to age groups using List
. Now I am
You can use removeAll
to obtain the complimentary list.
List userBelowThreshold = new ArrayList<>(users); // initiated with 'users'
userBelowThreshold.removeAll(userAboveThreshold);
Note: This would require overridden equals
and hashCode
implementation for User
.
On the other hand, if you further want to iterate over the complete users
list just once, you can use Collectors.partitioningBy
as:
Map> userAgeMap = users.stream()
.collect(Collectors.partitioningBy(user -> user.getAge() > 21, Collectors.toList()));
List userAboveThreshold = userAgeMap.get(Boolean.TRUE);
List userBelowThreshold = userAgeMap.get(Boolean.FALSE);