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 aStream
ofMap.Entry<B, C>
collect(Collectors.groupingBy(...))
takes function which is applied toMap.Entry<B, C>
and returnsB
, thus it collects values ofC
intoList<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?