I want to create a Map of a List with java8.
class Person {
String name;
int age;
//etc
}
List<Person> persons;
Map<String, Person> personsByName = persons.stream().collect(
Collectors.toMap(Person::getName, Functions.identify());
Result: The type Person does not define getName(T) that is applicable here
Why? What is wrong with Person::getName
?
If you have a getName()
method in Person
, this should be fine:
Map<String, Person> personsByName = persons.stream().collect(
Collectors.toMap(Person::getName, Function.identity()));
Note that I've also changed Functions.identify()
(which doesn't exist) with Function.identity()
. I would assume it was a typo of yours.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With