Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串集合转换为以输入作为键映射器的映射_Java_Java 8_Java Stream_Collectors - Fatal编程技术网

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)));