【发布时间】:2018-03-22 13:23:23
【问题描述】:
我有一个List counties,其中仅包含唯一的县名,还有一个List txcArray,其中包含该城市的城市名称、县名称和人口。
我需要仅使用带有 lambda 表达式的 Java 8 和 Streams 从 txcArray 获取每个县的最大城市名称。
这是我目前的代码:
List<String> largest_city_name =
counties.stream()
.map(a -> txcArray.stream()
.filter(b -> b.getCounty().equals(a))
.mapToInt(c -> c.getPopulation())
.max())
.collect( Collectors.toList());
我正在尝试在 .max() 之后添加另一个 .map 语句以获取人口最多的 City 的名称,但我的新 lambda 表达式在 txcArray 流中不存在,它仅将其识别为 @987654328 @ 类型和 texasCitiesClass 类型。这就是我想要做的。
List<String> largest_city_name =
counties.stream()
.map(a -> txcArray.stream()
.filter( b -> b.getCounty().equals(a))
.mapToInt(c->c.getPopulation())
.max()
.map(d->d.getName()))
.collect( Collectors.toList());
谁能告诉我我做错了什么?
【问题讨论】:
标签: java lambda collections java-8 java-stream