gpt4 book ai didi

Java Collectors.groupingBy()---List 是有序的吗?

转载 作者:太空狗 更新时间:2023-10-29 22:39:46 40 4
gpt4 key购买 nike

对于 Collectors.groupingBy()返回 Map<K,List<T>>是否暗示 List<T>是为了评估流吗?

我没有看到对列表排序的明确描述,而并发版本明确说明没有排序。如果它不是以某种方式订购的,我希望它是一个集合,但我看不出它可能是什么其他订购,除了收到的订单。

我希望它能保证每个列表中的最后一个值是该组收到的最后一个值。

最佳答案

groupingBy() 的文档说:

Implementation Requirements:

This produces a result similar to:

groupingBy(classifier, toList());

toList() 的文档说:

Returns:

a Collector which collects all the input elements into a List, in encounter order

因此,要回答您的问题,只要您的流具有已定义的 encounter order ,您一定会得到有序列表。


编辑:正如@Holger 指出的那样,groupingBy() 还必须遵守遇到顺序以保留toList() 的排序约束。此说明中强烈暗示了它确实存在的事实:

Implementation Note:

...If preservation of the order in which elements are presented to the downstream collector is not required, using groupingByConcurrent(Function, Collector) may offer better parallel performance.

关于Java Collectors.groupingBy()---List 是有序的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39172981/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com