Java 将字符串集合转换为以输入作为键映射器的映射
下面是一个例子:Java 将字符串集合转换为以输入作为键映射器的映射,java,java-8,java-stream,collectors,Java,Java 8,Java Stream,Collectors,下面是一个例子: Map<String, Student> getStudentsById(Collection<String> ids) { return ids.stream() .collect(Collectors.toMap(<id-here>, id -> new Student(id)) } Map getStudentsById(集合ID){ 返回id.stream() .collect(Collectors.to
Map<String, Student> getStudentsById(Collection<String> ids) {
return ids.stream()
.collect(Collectors.toMap(<id-here>, id -> new Student(id))
}
Map getStudentsById(集合ID){
返回id.stream()
.collect(Collectors.toMap(,id->newstudent(id))
}
我不知道如何使用
收集器。toMap
,因此键
是流元素(这里是ID的例子),而值
是从键
构造的某个对象,您正在将字符串
和学生
传递给收集器。toMap()
,当您应该传递函数时,这是否回答了您的问题?@SebastiánLópez不,不是。这个问题是关于使用与键映射器相同的输入参数。
Map<String, Student> idToStudent = ids.stream()
.collect(Collectors.toMap(Function.identity(), id -> new Student(id)));