jav8 分组后 排序的问题

    在使用stream 提供的 Collectors.groupingBy()时,默认返回的结果是HashMap, 也就是无序的。

    但有时也需要按照顺序分组,这是需要使用如下代码:

 

.collect(Collectors.groupingBy(i -> i % 2 == 0, LinkedHashMap::new, Collectors.toList()))

    下面是在stackoverflow查到的相关问题以及答案:(地址 https://stackoverflow.com/questions/45557290/stream-doesnt-preserve-the-order-after-grouping?noredirect=1&lq=1

    问题:

        I have a List name availableSeats I am sorting and grouping by the blockIndex property like below:

        我有一个名字是availableSeats 的列表。我在按照blockIndex 字段排序并分组,代码如下

availableSeats.stream()

.sorted(Comparator.comparing(SeatedTicketAssignment::getBlockIndex))

.collect(Collectors.groupingBy(SeatedTicketAssignment::getBlockIndex))

.forEach((block, blockAssignments) -> {

//Rest of the code

}

        The problem is that the result of grouping by is not sorted by the blockIndex.

        问题是分组的结果并不是按照blockIndex排好序的。

    正确回答是:

        Keep in mind, Collectors#groupingBy(Function) will return a HashMap, which does not guarantee order. If you want the ordering to be in the order that the aggregate identity (your i % 2 == 0 result) shows up, then you can use LinkedHashMap:

       记住, Collectors#groupingBy(Function) 会返回一个HashMap, 而它(HashMap)并不保证顺序。如果你想要保持在上面展示的(你的 i % 2 == 0 的结果)的排序结果,你可以使用 LinkedHashMap:

 

.collect(Collectors.groupingBy(i -> i % 2 == 0, LinkedHashMap::new, Collectors.toList()))

     Would return a LinkedHashMap<Boolean, List<SeatedTicketAssignment>> (since your collector is grouped by a boolean). Additionally, since the list utilized by the collector is an ArrayList, it should preserve the iteration order of the stream relative to the list.

    这会返回一个 LinkedHashMap<Boolean, List<SeatedTicketAssignment>>(因为你的收集器是按照一个boolean 分组的)。另外,返回的列表是ArrayList类型,它会保持你之前的排序结果

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值