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 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01