【发布时间】:2020-10-10 12:10:43
【问题描述】:
我有一个Student 对象,如下所示
class Student{
String name,email,country;
//getters setters
}
我需要将元素收集为TreeMap<String,List<String>>,其中键是学生的country,值是email 的列表
Map<String, List<Student>> countryStudents = students.stream()
.collect(Collectors.groupingBy(Student::getCountry));
Map<String,List<String>> map = new HashMap<>();
countryStudents .entrySet().forEach(entry -> map.put(entry.getKey(),entry.getValue().stream().map(student -> student .getEmail()).collect(Collectors.toList())));
我想知道是否有任何有效的方法来做到这一点,而不是在 2 次迭代中完成。
【问题讨论】:
标签: java optimization java-8