我需要过滤一个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()));
}
过滤地图后,该方法应返回包含所有键(匹配值)的列表。
如何处理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
将的Stream转换Entry
为键的Stream。
public List<String> getEqualPointList(Point point) {
return this.points
.entrySet()
.stream()
.filter(p -> p.getValue().isEqual(point))
.map(e -> e.getKey())
.collect(Collectors.toList());
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句