我有一个List counties
,其中只包含唯一的县名,还有一个List txcArray
,其中包含城市名称、县名和该城市的人口。
我需要使用Java 8的lambda表达式和Stream
,从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流中,它只将其识别为int
类型和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());
有人能告诉我我做错了什么吗?
Map
而不是无序的城市名称集合可能更好... - Holger