With my little understanding of streams, I might be doing something wrong.
My sample class:
@Getter
@Setter
@AllArgsConstructor
class SampleAction {
private String grouping;
private String category;
private String actionType;
private String details;
private String moreDetails;
private String etc;
}
Sample data:
List<SampleAction> items = new ArrayList<SampleAction>() {{
add(new SampleAction("Group1", "Cat1", "Type1", "details1", "", "etc"));
add(new SampleAction("Group2", "Cat1", "Type1", "details2", "", "etc"));
add(new SampleAction("Group1", "Cat1", "Type2", "details3", "", "etc"));
add(new SampleAction("Group1", "Cat2", "Type1", "details4", "", "etc"));
add(new SampleAction("Group2", "Cat2", "Type2", "details5", "", "etc"));
add(new SampleAction("Group2", "Cat2", "Type2", "details6", "", "etc"));
add(new SampleAction("Group2", "Cat3", "Type1", "details7", "", "etc"));
}};
My grouping code (group on grouping, category and action type):
items.stream()
.collect(Collectors.groupingBy(SampleAction::getGrouping,
Collectors.groupingBy(SampleAction::getCategory,
Collectors.groupingBy(SampleAction::getActionType))));
This gives me the grouped output, but the return type is Map<String, Map<String, Map<String, List<SampleAction>>>>
This looks a little scary. What is the best way to manage this? Do I really need this complex type for the output?