Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Streams: get values grouped by inner map key

I have Map<A, Map<B, C>> and I want to get Map<B, List<C>> from it using Java Streams.

I try to do it as follows:

public <A, B, C> Map<B, List<C>> groupsByInnerKey(Map<A, Map<B, C>> input) {
    return input.values()
            .stream()
            .flatMap(it -> it.entrySet().stream())
            .collect(Collectors.groupingBy(Map.Entry::getKey));
}

What I expect:

  • flatMap gives a Stream of Map.Entry<B, C>
  • collect(Collectors.groupingBy(...)) takes function which is applied to Map.Entry<B, C> and returns B, thus it collects values of C into List<C>.

But it doesn't compile, literally:

Non-static method cannot be referenced from a static context

at Map.Entry::getKey in the last line.

Can someone explain what is wrong or what is the right way to achieve what I want?

like image 597
hotkey Avatar asked Jan 11 '16 11:01

hotkey


1 Answers

Your Stream is composed of Map.Entry objects but want you want to collect is actually the value of the entry, not the entry itself. With your current code, you would be obtaining a Map<B, List<Map.Entry<B, C>>>.

As such, you are just missing a call to Collectors.mapping. This collector will map the Stream element with the given mapper function and collect that result into the downstream container. In this case, the mapper is Map.Entry::getValue (so returning the value from the map entry) and the downstream collector collects into a List.

public <A, B, C> Map<B, List<C>> groupsByInnerKey(Map<A, Map<B, C>> input) {
    return input.values()
            .stream()
            .flatMap(it -> it.entrySet().stream())
            .collect(Collectors.groupingBy(
                 Map.Entry::getKey,
                 Collectors.mapping(Map.Entry::getValue, Collectors.toList())
            ));
}
like image 59
Tunaki Avatar answered Sep 20 '22 16:09

Tunaki