Big-O complexity of java.util.stream.Streamlt;Tgt;.sorted()(java.util.stream.Streamlt;Tgt;.sorted() 的大 O 复杂度)
问题描述
有谁知道java.util.stream.Stream<T>.sorted()
的时间复杂度是多少?
Does anyone know what the time complexity of java.util.stream.Stream<T>.sorted()
is?
推荐答案
嗯,sorted()
本身就是 O(1),因为它是一个不消耗流的中间操作,但只是简单地向管道添加一个操作.
Well, sorted()
in itself is O(1), since it's an intermediate operation that doesn't consume the stream, but simply adds an operation to the pipeline.
一旦终端操作消耗了流,就会发生排序
Once the stream is consumed by a terminal operation, the sort happens and either
- 它什么都不做 (O(1)),因为流知道元素已经排序(例如,因为它们来自 SortedSet)
- 或者流不是并行的,它委托给
Arrays.sort()
(O(n log n)) - 或者流是并行的,它委托给
Arrays.parallelSort()
(O(n log n))
- it doesn't do anything (O(1)) because the stream knows that the elements are already sorted (because they come from a SortedSet, for example)
- or the stream is not parallel, and it delegates to
Arrays.sort()
(O(n log n)) - or the stream is parallel, and it delegates to
Arrays.parallelSort()
(O(n log n))
这篇关于java.util.stream.Stream<T>.sorted() 的大 O 复杂度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java.util.stream.Stream<T>.sorted() 的大 O 复杂度
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01