我想一個列表排序,然後組的元素。問題是,當我對元素進行分組時,排序順序就是折騰。 下面是我的一段代碼如何使用分組和排序一起JAVA 8
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",
},
}
基本上,我的要求是首先顯示最便宜的房間類型。 因此,輸出應首先由總理國王室分組,因爲它具有最便宜的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",
}
}
能否請你建議我任何其他方法或什麼是錯與上面的實現,任何新的想法,歡迎。
https://開頭計算器.com/a/28765069/2711488 – Holger