我正在尝试使用Stream API过滤HashMap中的条目,但卡在最后一个方法调用Collectors.toMap
上。因此,我不知道如何实现toMap方法。
public void filterStudents(Map<Integer, Student> studentsMap){
HashMap<Integer, Student> filteredStudentsMap = studentsMap.entrySet().stream().
filter(s -> s.getValue().getAddress().equalsIgnoreCase("delhi")).
collect(Collectors.toMap(k , v));
}
public class Student {
private int id;
private String firstName;
private String lastName;
private String address;
...
}
有什么建议吗?
toMap(s -> s.getKey(), s -> s.getValue())
,可以像@Eran的答案中那样转换为方法引用。即使它们有点长,我仍然建议这样做。 - Novaterata