Java 8 Lambda (grouping and shrinking in one step)

Say that I have objects from the list of pairs,

List<Pair<A,B>> listOfPairs = //some list of pairs ; 

I would like to group this list into Map<A,Set<B>>.

I can currently do this in two steps. The first groups of steps along A and return Map<A,Set<Pair<A,B>>as follows:

   Map<A,Set<Pair<A,B>> intermediateStep =   listOfPairs.stream().collect(Collectors.groupingBy((Pair::getLeft), Collectors.toSet()));

Then I pass the input map set above and collect them into the desired final result, mainly by matching each Pair object with its value B and collecting them into a set:

 Map<A, Set<B>> finalResult= intermediateStep.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().stream().map(Pair::getRight).collect(Collectors.toSet())));

Is there a better way to achieve the desired result, perhaps in one step? In other words, the intermediate step mentioned above by groups is through A, but the right side of the grouping returns the entire Pair object. I would like to group by A and point A into a set of related Bs in one step.

( , , , , , , .)

!

+6
2

, mapping groupingBy :

Map<A, Set<B>> collect = 
       listOfPairs.stream()
                  .collect(Collectors.groupingBy(Pair::getLeft, 
                                Collectors.mapping(Pair::getRight,
                                       Collectors.toSet())));
+11

, Map.computeIfAbsent:

Map<A, Set<B>> finalResult = new HashMap<>();
listOfPairs.forEach(pair -> finalResult.computeIfAbsent(
        pair.getLeft(), 
        k -> new HashSet<>())
    .add(pair.getRight()));

, LinkedHashMap LinkedHashSet HashMap HashSet .

+1

All Articles