What I would like to do is group elements of a List in order to create a map, based on specific fields. The desired output is the following: Map<String,Map<String,Map<String,MasterObject>>>. The order of the keys is date, type, color.
My code is as follows
public class MasterObject{
private String date;
private List<SubObject> subObject;
}
public class SubObject{
private String type;
private String color;
}
What I have tried and used is Collectors.groupingBy which works great if your fields are members of the same Object (ex. date), but haven't managed to make it work for containing objects (ex. subObject). My input is a List<MasterObject>. It could be done the hard way by using Map's put and get but maybe there is a much cleaner way to make it work with Java streams.
What I have tried thus far is the following:
Map<String, Map<List<String>, List<MasterObject>>> collect = faList.stream().collect(
Collectors.groupingBy(f -> f.getDate(),
Collectors.groupingBy(f -> f.getSubObject().stream().map(z -> z.getType()).collect(Collectors.toList()))));
In my sample above, I haven't managed to achieve to group elements by type, instead my key is a List<String>.Also my list should have a group by color as well.
MasterObjectcontains multiple subobjects with different types and colors?Mapto have different entries grouped by first type and second color for the same date