Java Collectors.groupingBy()---is List ordered?(Java Collectors.groupingBy()---List 是有序的吗?)
问题描述
对于返回 Map<K,List<T>> 的
Collectors.groupingBy()
是否暗示 List<T>
code> 是为了评估流吗?
For the Collectors.groupingBy()
that returns Map<K,List<T>>
is it implied that the List<T>
is in order that the stream is evaluated?
我没有看到列表排序的明确描述,而并发版本明确声明没有排序.如果它不是以某种方式订购的,我希望它是一个集合,我看不出它可能是什么其他订购,除了收到的订单.
I see no explicit description of the ordering of the list, whereas the concurrent version explicitly states no ordering. If it weren't ordered somehow, I'd expect it to be a Collection though, and I don't see what other ordering it could possibly be, other than order received.
我希望可以保证每个列表中的最后一个值是该组收到的最后一个值.
I'm hoping it's guaranteed that the last value in each list is the last value received for that group.
推荐答案
groupingBy()
说:
实施要求:
这会产生类似的结果:
groupingBy(classifier, toList());
<代码>toList() 说:
返回:
一个Collector
,它将所有输入元素按遇到顺序收集到一个List
中
a Collector
which collects all the input elements into a List
, in encounter order
所以,要回答您的问题,只要您的流具有定义的 遇到订单,保证得到有序列表.
So, to answer your question, as long as your stream has a defined encounter order, you're guaranteed to get ordered lists.
正如@Holger 指出的那样,groupingBy()
还必须尊重遇到顺序以保留 toList()
的排序约束.本说明强烈暗示了它确实如此的事实:
As @Holger points out, groupingBy()
would also have to respect encounter order to preserve toList()
's ordering constraint. The fact that it does is strongly implied in this note:
实施说明:
...如果不需要保留元素呈现给下游收集器的顺序,则使用 groupingByConcurrent(Function, Collector)
可能会提供更好的并行性能.
...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 是有序的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java Collectors.groupingBy()---List 是有序的吗?


- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- 转换 ldap 日期 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01