I have a list of pojos over which I want to do some grouping. Sort of:
public class Pojo { private final Category category; private final BigDecimal someValue; } public class Category { private final String majorCategory; private final String minorCategory; }
I want a Map<String, Map<String, List<Pojo>>> , where the key is majorCategory , and the value is a Map with the key minorCategory , and the values ββare a List of Pojo objects for the specified minorCategory .
I intend to use Java 8 lambdas to achieve this. I can get the first level of grouping with the following:
Map<String, Pojo> result = list .stream() .collect(groupingBy(p -> p.getCategory().getMajorCategory()));
How do I now group again on minorCategory and get the Map<String, Map<String, List<Pojo>>> I want?
Update
The first answer provided is correct for the example presented at the initial stage, however, since then I updated this question. Ruben's comment in the accepted answer gives the final piece of the puzzle.
source share