List<Object> to Map<String, Map<String,List<Object>>>

Ram :

I have List<Person> where Person is as below.

class Person {

    String personId;
    LocalDate date;
    String type;

    // getters & setters

}

I'm trying to convert this to List<Person> to Map<String, Map<LocalDate,List<Person>>> where outer map's key is personId and inner map's key is date and I couldn't figure out how to achieve this. Thus far have tried something like below. Open to Java 8 solutions as well.

Map<String,Map<LocalDate,List<Person>>> outerMap = new HashMap<>();
Map<LocalDate,List<Person>> innerMap = new HashMap<>();

for(Person p : list) {
    List<Person> innerList = new ArrayList<>();
    innerList.add(p);
    innerMap.put(p.getDate(), innerList);
    outerMap.put(p.getPersonId(), innerMap);
}
Eugene :
list.stream()
    .collect(Collectors.groupingBy(
        Person::getPersonId,
        Collectors.groupingBy(
            Person::getDate
)));

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=433524&siteId=1