Suppose there are 3 classes:
class Level1 {
int idLevel1;
List<Level2> level2list;
}
class Level2 {
int idLevel2;
List<Level3> level3list;
}
class Level3 {
int idLevel3;
String name;
}
Suppose there is a List of Level1 objects called initial state
List<Level1> initialList = new ArrayList<>();
I want to create a map from initialList where:
- Key: is idLevel1
- Value: is list of all idLevel3 , corresponding to idLevel1
I am able to achieve this using for loops, but I want to achieve this in a more elegant way using Java 8 features (streams and the functions). I tried using Collectors.toMap() also tried grouping but I am unable to get the desired map.
flatMap, it seems.Elegantdoes not necessarily meanbest.level2Listin theLevel2class definition really belevel3List?level3List