Having list of key - values:
public class KeyValue {
private Long key;
private Long value;
public KeyValue(long key, long value) {
thi
This is exactly what the groupingBy collector allows you to do:
Map<Long, List<Long>> result = values.stream()
.collect(Collectors.groupingBy(KeyValue::getKey,
Collectors.mapping(KeyValue::getValue, Collectors.toList())));
Then the mapping collector converts the KeyValue objects into their respective values.