What is the best way to divide a collection into 2 different collections?

Map<Boolean, List<Integer>> partitioned = 
    set.stream().collect(Collectors.partitioningBy(x -> x%2 == 0));

The elements in partitioned.get(true) are even; the elements in partitioned.get(false) are odd.

Unlike doing this using groupingBy, it is guaranteed that both true and false lists will be present in the map even if they are empty. (Not documented in Java 8, but it was true; Java 9’s doc now states it explicitly).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)