我有一个 List<Person>
,其中 Person
如下。
class Person {
String personId;
LocalDate date;
String type;
// getters & setters
}
我正在尝试将此转换为
List<Person>
至Map<String, Map<LocalDate,List<Person>>>
,其中外部map的键是personId
,内部map的键是date
,但我无法弄清如何实现。到目前为止,我尝试了以下内容。也可以接受Java 8的解决方案。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);
}
innerList
在循环内部声明?为什么innerMap
在外部声明?为什么outerMap
也在外部声明? - M. Prokhorov