Given a class
public class MyClass {
public int langId;
public int sectionId;
}
If I have a map that maps a LangId to all the instances of MyClass that have that LangId:
Map> mapLangIdToListOfMyClass = new HashMap<>();
Using Java 8 streams, would there be a simple way to consume the previous Map and create a new Map that maps SectionId to all the instances of MyClass that has that SectionId:
Map> mapSectionIdToListOfMyClass = new HashMap<>();
解决方案
I think you can do
Map> mapSectionIdToListOfMyClass = mapLangIdToListOfMyClass
.values()
.stream()
.flatMap(List::stream)
.collect(Collectors.groupingBy(j -> j.sectionId));