毛小川
03/01/2022, 9:08 AM.forEach{it->}
guarantee the original sequence during execution?gildor
03/01/2022, 9:10 AMgildor
03/01/2022, 9:10 AM毛小川
03/01/2022, 9:14 AMstream().forEach()
of java
毛小川
03/01/2022, 9:18 AMgildor
03/01/2022, 9:20 AMgildor
03/01/2022, 9:20 AMThis seems to be different from theI believe it’s not different except if you use parallel streamofstream().forEach()
java
gildor
03/01/2022, 9:21 AMgildor
03/01/2022, 9:22 AMVampire
03/01/2022, 10:53 AMstream().forEach()
in Java.
If the Stream is ordered and sequential, forEach
will preserve the order. If the stream is unordered or parallel, order might not be preserved.
Same for forEach
in Kotlin. If the collection you run it on preserves order, forEach
preserves order too, if no, then not.gildor
03/01/2022, 1:12 PMVampire
03/01/2022, 1:25 PMforEach
does have no guarantee even for ordered sequential stream, but there the order usually is preserved.
Forgot about forEachOrdered
which adds this guarantee for ordered streams, no matter whether sequential or parallel.