【发布时间】:2020-06-06 21:21:59
【问题描述】:
我有许多城市的房屋清单。我正在尝试使用过滤器来生成每个城市最昂贵的房子的列表。我无法使用常规循环。
//This returns unique City Names
List unique = obList.stream()
.map(x -> x.getCity())
.distinct()
.sorted()
.collect(Collectors.toList());
//This returns the house with the highest price in that city
RAddress obReturn = obList.stream()
.filter(x -> x.getCity().equalsIgnoreCase(sName))
.sorted((x,y) -> (int)(y.getPrice() - x.getPrice()))
.findFirst()
.get();
我知道以某种方式结合这些对于这个问题是必要的,但我一生都无法弄清楚如何......
感谢任何和所有的帮助。
【问题讨论】:
标签: java lambda filter java-stream