场景
有一个student类,name是姓名,score是分数,现在要统计每位学生的总成绩
方法一
studentList.stream().collect(Collectors.toMap(Student::getName, Student::getScore, Integer::sum));
方法二
List<student> studentList = new ArrayList<>();
studentList.stream()
.collect(Collectors.groupingBy(Student::getName, Collectors.collectingAndThen(
Collectors.summarizingDouble(Student::getScore), DoubleSummaryStatistics::getSum)));
方法三
List<student> studentList = new ArrayList<>();
studentList.stream()
.collect(Collectors.groupingBy(Student::getName, Collectors.collectingAndThen(
Collectors.mapping(Student::getScore, Collectors.reducing(Integer::sum)),
Optional::get)));
</student></student>