我需要过滤一个HashMap
Map<String, Point> points = new HashMap<String, Point>();
对于它的一些值并有一个方法
public List<String> getEqualPointList(Point point) {
return this.points.entrySet().stream().filter(p -> p.getValue().isEqual(point)).collect(Collectors.toList(p -> p.getKey()));
}
该方法将在过滤Map后返回一个包含所有键(匹配值)的List。
如何处理collect()?我收到一条错误消息
Multiple markers at this line
- The method toList() in the type Collectors is not applicable for the arguments
((<no type> p) -> {})
- Type mismatch: cannot convert from Collection<Map.Entry<String,Point>> to
List<String>
最佳答案
toList
不接受任何参数。您可以使用 map
将 Entry
流转换为 key 流。
public List<String> getEqualPointList(Point point) {
return this.points
.entrySet()
.stream()
.filter(p -> p.getValue().isEqual(point))
.map(e -> e.getKey())
.collect(Collectors.toList());
}
关于java - 过滤时如何从 map 中恢复列表(使用流),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28263518/