将字符串集合转换为以输入作为键映射器的映射

Converting String Collection to a Map with input as the key mapper

这是一个例子:

Map<String, Student> getStudentsById(Collection<String> ids) {

  return ids.stream()
       .collect(Collectors.toMap(<id-here>, id -> new Student(id))

}

我不确定如何使用 Collectors.toMap,因此 key 是流元素(这里是 ID),而 value 是从key.

您将 StringStudent 传递给 Collectors.toMap(),而您应该传递 Function<? super String,? extends String>Function<? super String,? extends Student>

应该是:

Map<String, Student> idToStudent = ids.stream()
     .collect(Collectors.toMap(Function.identity(), id -> new Student(id)));