mem*_*und 3 java java-8 java-stream
我想用java8创建一个列表映射。
class Person {
String name;
int age;
//etc
}
List<Person> persons;
Map<String, Person> personsByName = persons.stream().collect(
Collectors.toMap(Person::getName, Functions.identify());
Run Code Online (Sandbox Code Playgroud)
结果: The type Person does not define getName(T) that is applicable here
为什么?有什么问题Person::getName?
如果你有一个getName()方法Person,这应该没问题:
Map<String, Person> personsByName = persons.stream().collect(
Collectors.toMap(Person::getName, Function.identity()));
Run Code Online (Sandbox Code Playgroud)
请注意,我也Functions.identify()用Function.identity(). 我认为这是你的错字。
| 归档时间: |
|
| 查看次数: |
1121 次 |
| 最近记录: |