Fal*_*ise 7 java java-8 java-stream collectors
有一个Student类,它们有name, surname, age字段和getter.
给定一个Student对象流.
如何调用一个collect方法,使得它将返回Map键所在的位置age,Student并且值TreeSet包含surname具有此类的学生age.
我想用Collectors.toMap(),但卡住了.
我以为我可以这样做并将第三个参数传递给toMap方法:
stream().collect(Collectors.toMap(Student::getAge, Student::getSurname, new TreeSet<String>()))`.
Run Code Online (Sandbox Code Playgroud)
students.stream()
.collect(Collectors.groupingBy(
Student::getAge,
Collectors.mapping(
Student::getSurname,
Collectors.toCollection(TreeSet::new))
))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
144 次 |
| 最近记录: |