rqxiao
V2EX  ›  程序员

stream 有什么办法对分组后的每组元素内部进行排序吗

By rqxiao at 2019 年 9 月 23 日 · 5029 次点击

Map<String, List<introductionvideopagedto>> map = videoPageDtoList.stream().collect(Collectors.groupingBy(dto -> dto.getDataId()));</introductionvideopagedto>

"3": [ { "id": "96ab01d10d66bb0fe41d3fc65b7eeca4", "title": "视频标题", "videoUrl": "11111111111", "dataId": "3", "sort": 2, "createTime": 1564041929000 }, { "id": "ef510c786580637e831ea532e925a312", "title": "课程标题课程标题", "videoUrl": "222222222222", "dataId": "3", "sort": 3, "createTime": 1568791200000 }, { "id": "efa01867ea0d6cf4cccc0020203c6928", "title": "课程标题", "videoUrl": "333333333333333333333333333333333333333", "dataId": "3", "sort": 1, "createTime": 1568790906000 } ]

groupingBy 之后的这个 map 中 key stream 好像是默认已经排序了? 接着还想在组内对 sort 进行排序

2 条回复  •  2019-09-23 19:39:26 +08:00
ayonel
   1
ayonel  
   2019 年 9 月 23 日
必须一步到位么?
https://stackoverflow.com/questions/29610465/sort-the-values-in-a-map-of-type-mapstring-liststring 这个两步可解决
第二步:map.values().stream().forEach(l->l.sort(Compartor)) 大概这样
Tsukihime
   2
Tsukihime  
   2019 年 9 月 23 日
list.stream()
.sorted(Comparator.comparing(IntroductionVideoPageDTO::getSort))
.collect(Collectors.groupingBy(IntroductionVideoPageDTO::getDataId));
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
© 2026 V2EX · 51ms · 3.9.8.5