在使用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类型,它会保持你之前的排序结果