forEachOrdered() applies an action from left to right according to the stream’s encounter order, including when the input stream is parallel. It is useful when an accumulator or side effect depends on processing order, but it usually removes the benefit of parallelization. section 9.5
Ordered accumulation
A hash-code accumulator can process mapped values with forEachOrdered(op), ensuring that each update follows the stream’s encounter order. The same calculation may be affected by parallel execution when an order-sensitive accumulator is applied differently. section 9.5
Use it deliberately:
- Choose forEachOrdered() when left-to-right processing is required for a parallel or sequential input. section 9.5
- Expect little parallel benefit for most streams; heavy upstream operations may still make parallel execution worthwhile. section 9.5
