V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
rqxiao
V2EX  ›  程序员

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

  •  
  •   rqxiao · 2019-09-23 16:14:06 +08:00 · 3996 次点击
    这是一个创建于 1649 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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-09-23 19:01:04 +08:00
    必须一步到位么?
    https://stackoverflow.com/questions/29610465/sort-the-values-in-a-map-of-type-mapstring-liststring 这个两步可解决
    第二步:map.values().stream().forEach(l->l.sort(Compartor)) 大概这样
    zydxn
        2
    zydxn  
       2019-09-23 19:39:26 +08:00
    list.stream()
    .sorted(Comparator.comparing(IntroductionVideoPageDTO::getSort))
    .collect(Collectors.groupingBy(IntroductionVideoPageDTO::getDataId));
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2482 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 15:45 · PVG 23:45 · LAX 08:45 · JFK 11:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.