java 8 parallelStream() with sorted()
You need to use forEachOrdered, not forEach. As per the forEach doc: For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the …