Does anyone know what the time complexity of java.util.stream.Stream
is?
As of JDK 8, the main sorting algorithm which is also used in standard stream API implementation for sequential sorting is TimSort. Its worst case is O(n log n)
, but it works incredibly fast (with O(n)
and quite small constant) if data is presorted (in forward or reverse direction) or partially presorted (for example, if you concatenate two sorted lists and sort them again).
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
Arrays.sort()
(O(n log n))Arrays.parallelSort()
(O(n log n))