Encounter order preservation in java stream

后端 未结 2 1923
眼角桃花
眼角桃花 2020-12-17 05:55

I have gone through related questions like How to ensure order of processing in java8 streams?, still ordering of output element is not completely clear to me. Therefore ple

2条回答
  •  無奈伤痛
    2020-12-17 06:35

    As soon as you selected unordered then the end result can come through in essentially a random order. Note though that there is no requirement that it do, so in fact it is likely you will still see some ordering in the output.

    forEachOrdered preserves the encounter order of the 'stream' so if you didn't have .unordered() then it would ensure you see the elements in encounter order. If the stream is already unordered though then it is pointless and you might as well use forEach.

    In other words forEachOrdered preserves the encounter order in an already ordered stream. It does not do any sorting or other ordering though so if the stream is already unordered then anything can happen.

提交回复
热议问题