Java: List转Map

java标志
image-3362

1
2
3
4
5
6
7
// 定义实体类.
record Student(String id,String name,int age){
}

// 方法.
List<Student> students= new ArrayList<>(1);
Map<String, Student> studentIdToStudent = students.stream().collect(Collectors.toMap(Student::id,  Function.identity()));