Lambda 使用java流对具有相同id/名称的对象列表进行分组

Lambda 使用java流对具有相同id/名称的对象列表进行分组,lambda,java-8,java-stream,collectors,Lambda,Java 8,Java Stream,Collectors,我有一个学生类列表的对象列表。学生班级有另一个班级成绩的日期和对象的排序图 见下文: class Student{ String name; SortedMap<LocalDate, Score> semScore = new TreeMap()<>; } 如何聚合学生列表,以按学生姓名将所有已排序的地图合并到单个地图组中。 例如 Score得分=新得分(90,“A”); SortedMap sMap=新树映射(); sMap.put(LocalDat

我有一个学生类列表的对象列表。学生班级有另一个班级成绩的日期和对象的排序图 见下文:

class Student{
    String name;
    SortedMap<LocalDate, Score> semScore = new TreeMap()<>; 
}
如何聚合学生列表,以按学生姓名将所有已排序的地图合并到单个地图组中。 例如

Score得分=新得分(90,“A”);
SortedMap sMap=新树映射();
sMap.put(LocalDate.now(),score);
学生s=新生(“鲍勃”,sMap);
因此,对于一个名字和一张分数地图的学生,列表中有多个记录。
我需要将所有分数聚合到同一个学生对象中,如按名称分组。
我们如何使用java流实现它?
谢谢

由于必须合并它们,
收集器#toMap
包含一个合并功能:

Map<String, Student> mergedStudents = list.stream().collect(Collectors.toMap(Student::getName, Function.identity(), (s1, s2) -> {
  SortedMap<LocalDate, Score> semScore = new TreeMap<>(s1.getSemScore());
  semScore.putAll(s2.getSemScore());
  return new Student(s1.getName(), semScore);
}));
Map mergedStudents=list.stream().collect(Collectors.toMap(Student::getName,Function.identity(),(s1,s2)->{
SortedMap semScore=新树映射(s1.getSemScore());
semScore.putAll(s2.getSemScore());
返回新学生(s1.getName(),semScore);
}));

是否可以修改现有的并在合并功能中返回?是。只需将s2.semScore添加到s1并返回s1。
Score score = new Score(90, "A");
SortedMap<LocalDate, Score> sMap = new TreeMap<>();
sMap.put(LocalDate.now(), score);

Student s = new Student("Bob", sMap);

So multiple records are in the list for a Student with a given name with one map of score.
I need to aggregate all the scores into the same student object like group by name.

How can we do it using java streams?

Thanks
Map<String, Student> mergedStudents = list.stream().collect(Collectors.toMap(Student::getName, Function.identity(), (s1, s2) -> {
  SortedMap<LocalDate, Score> semScore = new TreeMap<>(s1.getSemScore());
  semScore.putAll(s2.getSemScore());
  return new Student(s1.getName(), semScore);
}));