The 30 second answer
I use partitioningBy with a predicate for evenness. The result is always a map with both true and false keys, even when one side is empty. That is the main difference from groupingBy, which only creates keys that actually appear.
Map<Boolean, List<Integer>> p = nums.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));The returned map always contains both true and false keys, even if one list is empty.
Sources:
Sources (0)
See inline citations in the answer above.