TL; DR
Yes, the order is guaranteed.
API documentation Stream.collect ()
The starting point is to look at what determines whether the reduction is simultaneous or not. Stream.collect() description says the following:
If the stream is parallel and Collector is concurrent , and either the stream is unordered or the collector is unordered , then parallel reduction will be performed (see Collector for more information on parallel reduction.)
The first condition is fulfilled: the flow is parallel. What about the second and third: is Collector simultaneous and disordered?
API Documentation Collectors.toList ()
toList() documentation reads:
Returns a Collector that accumulates input elements in a new List . There are no guarantees regarding the type, variability, serializability or security of List streams; if more control is required on the returned List , use toCollection(Supplier) .
Returns:
a collector that collects all input elements into a list in the order of meeting
An operation that works in search order works with elements in their original order. This cancels concurrency.
Implementation code
Checking the implementation of Collectors.java confirms that toList() contains no CONCURRENT or UNORDERED .
public static <T> Collector<T, ?, List<T>> toList() { return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add, (left, right) -> { left.addAll(right); return left; }, CH_ID); }
Note that the collector has a feature set CH_ID , which has only one feature IDENTITY_FINISH . CONCURRENT and UNORDERED do not exist, so the reduction cannot be parallel.
Noncompetitive reduction means that if the flow is parallel, the collection can continue in parallel, but it will be divided into several intermediate results with flow restriction, which are then combined. This ensures that the combined result is in the search order.
See also: Why concurrent thread builds sequentially in Java 8