Java8 listPerson转MapString ListString其中ListString里边包含Map的key
假设Person类中有一个属性为name,可以使用Java8的Stream API将List
代码示例:
Map<String, List<String>> map = personList.stream()
.collect(Collectors.groupingBy(Person::getName,
Collectors.mapping(p -> p.getKey(), Collectors.toList())));
解释:
首先使用Stream的groupingBy()方法将List
然后使用mapping()方法将每个Person对象映射为其name属性值,并使用toList()方法将其转换为List
最终得到Map<String, List
原文地址: https://www.cveoy.top/t/topic/b03j 著作权归作者所有。请勿转载和采集!