过滤时如何从地图中恢复列表(使用流)
how to revert a List from a Map when filtering (using streams)
我需要过滤一个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()));
}
该方法应 return 过滤 Map 后包含所有键(匹配值)的列表。
如何处理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
的流转换为键的流。
public List<String> getEqualPointList(Point point) {
return this.points
.entrySet()
.stream()
.filter(p -> p.getValue().isEqual(point))
.map(e -> e.getKey())
.collect(Collectors.toList());
}
我需要过滤一个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()));
}
该方法应 return 过滤 Map 后包含所有键(匹配值)的列表。
如何处理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
的流转换为键的流。
public List<String> getEqualPointList(Point point) {
return this.points
.entrySet()
.stream()
.filter(p -> p.getValue().isEqual(point))
.map(e -> e.getKey())
.collect(Collectors.toList());
}