In this answer I attempted to create a static utility method to make a List into a Map:
public static Map toMa
With this bit:
Map peopleById1 = students.stream()
.collect(Collectors.toMap(Student::getId, Function.identity()));
Notice that you do not provide arguments to Function.identity(). The compiler is free to infer it as Function. to resolve the difference imposed by the return value assignment.
This should be good enough for your purpose:
public static Map toMapBy(
List extends T> list, // <- note
Function super T, ? extends K> mapper
) {
...
}
Now the elements of the List can be a subtype of the Map values. Or you can define a third parameter like @Alex has suggested.