I'm trying to transform data of this format:
Map<Foo, List<Bar>> mapFooToBars; //start Map
Map<Integer, List<Foo>> mapListSizeToFoos //destination Map
List<Bar>
Map<Foo, List<Bar>> fooBarMap = whatever();
fooBarMap.entrySet().stream()
.collect(Collectors.groupingBy(entry -> entry.getValue().size()));
//expected Map<Integer,Entry<Foo, List<Bar>> which
//could be transformed into Map<Integer,Foo> pretty easily
You are on the right track. You should use groupingBy
but with mapping(Map.Entry::getKey, toList())
as the second parameter. This will collect the keys into a list and those lists will be the values of the resulting map.
fooBarMap.entrySet().stream()
.collect(groupingBy(
e -> e.getValue().size(),
mapping(Map.Entry::getKey, toList())
));
above assumes static imports of various collectors