按班级分组
Map<String, List<Student>> collect = students.stream().collect(
Collectors.groupingBy(Student::getClassNumber)
);
嵌套分组
Map<String, Map<Integer, List<Student>>> collect = students.stream().collect(
Collectors.groupingBy(Student::getClassNumber, Collectors.groupingBy(Student::getAge))
);
按照班级分组得到每个班级的同学姓名
Map<String, List<String>> collect = students.stream().collect(
Collectors.groupingBy(
Student::getClassNumber, Collectors.mapping(Student::getName, Collectors.toList())
)
);
按班级分组求每个同学的总成绩
Map<String, Map<String, Integer>> collect = students.stream().collect(
Collectors.groupingBy(
Student::getClassNumber,
Collectors.toMap(Student::getName, student -> student.getMathScores() + student.getChainessScores())
)
);