gpt4 book ai didi

java - JAVA 8中如何同时使用分组和排序

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:02:43 26 4
gpt4 key购买 nike

<分区>

我正在尝试对列表进行排序,然后对元素进行分组。问题是,当我对元素进行分组时,排序顺序会发生变化。 下面是我的一段代码

listOfRooms.stream().sorted(Comparator.comparing(DXARoom::getStayAverageRate))
.collect(Collectors.groupingBy(DXARoom::getRoomInfoLabel));

以上代码的输出是这样的

{
"Presidential suite": [
{
"roomInfoLabel": "Presidential suite",
"stayAverageRate": "1696.0",
},
{
"roomInfoLabel": "Presidential suite",
"stayAverageRate": "1729.0",
},
"Premier king room": [
{
"roomInfoLabel": "Premier king room",
"stayAverageRate": "370.0",
},
{
"roomInfoLabel": "Premier king room",
"stayAverageRate": "377.0",
},
}

基本上,我的要求是先显示最便宜的房型。所以输出应该首先按 Premier king room 分组,因为它有最便宜的房间 370 英镑。

预期输出 ->

   {
"Premier king room": [
{
"roomInfoLabel": "Premier king room",
"stayAverageRate": "370.0",
},
{
"roomInfoLabel": "Premier king room",
"stayAverageRate": "377.0",
},
"Presidential suite": [
{
"roomInfoLabel": "Presidential suite",
"stayAverageRate": "1696.0",
},
{
"roomInfoLabel": "Presidential suite",
"stayAverageRate": "1729.0",
}

}

能否请您建议我任何替代方法或上述实现有什么问题,欢迎任何新想法。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com