forEach vs forEachOrdered in Java 8 Stream
Asked Answered
A

4

113

I understand that these methods differ the order of execution but in all my test I cannot achieve different order execution.

Example:

System.out.println("forEach Demo");
Stream.of("AAA","BBB","CCC").forEach(s->System.out.println("Output:"+s));
System.out.println("forEachOrdered Demo");
Stream.of("AAA","BBB","CCC").forEachOrdered(s->System.out.println("Output:"+s));

Output:

forEach Demo
Output:AAA
Output:BBB
Output:CCC
forEachOrdered Demo
Output:AAA
Output:BBB
Output:CCC

Please provide examples when 2 methods will produce different outputs.

Acord answered 26/9, 2015 at 13:12 Comment(3)
Try maybe with parallel streams.Pegpega
@Pegpega is it only possible option?Acord
Unspecified order does not imply “guaranteed to be different order”. It just means unspecified, which always implies the possibility to match the encounter order. There is no built-in shuffle function.Indisputable
F
114
Stream.of("AAA","BBB","CCC").parallel().forEach(s->System.out.println("Output:"+s));
Stream.of("AAA","BBB","CCC").parallel().forEachOrdered(s->System.out.println("Output:"+s));

The second line will always output

Output:AAA
Output:BBB
Output:CCC

whereas the first one is not guaranted since the order is not kept. forEachOrdered will processes the elements of the stream in the order specified by its source, regardless of whether the stream is sequential or parallel.

Quoting from forEach Javadoc:

The behavior of this operation is explicitly nondeterministic. 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.

When the forEachOrdered Javadoc states (emphasis mine):

Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.

Formica answered 26/9, 2015 at 13:13 Comment(5)
Yes, you are right. Is it possible only for parallelStreams ?Acord
Even if it only would apply to parallel streams right now - and I'm not saying it does - it still might break in the future if some intermediate steps get optimized to take advantage of unordered streams, e.g. a sort could use an unstable algorithm if the stream is unordered.Viniferous
So it makes no sense to use forEachOrdered with parallel ?Heedful
@BhushanPatil Yes, that's correct. #47337325Lapboard
Using forEachOrdered will process element by order then using parallel streams will loose benefits of parallelism. Please suggest.Mayers
B
38

Although forEach shorter and looks prettier, I'd suggest to use forEachOrdered in every place where order matters to explicitly specify this. For sequential streams the forEach seems to respect the order and even stream API internal code uses forEach (for stream which is known to be sequential) where it's semantically necessary to use forEachOrdered! Nevertheless you may later decide to change your stream to parallel and your code will be broken. Also when you use forEachOrdered the reader of your code sees the message: "the order matters here". Thus it documents your code better.

Note also that for parallel streams the forEach not only executed in non-determenistic order, but you can also have it executed simultaneously in different threads for different elements (which is not possible with forEachOrdered).

Finally both forEach/forEachOrdered are rarely useful. In most of the cases you actually need to produce some result, not just side-effect, thus operations like reduce or collect should be more suitable. Expressing reducing-by-nature operation via forEach is usually considered as a bad style.

Bondon answered 26/9, 2015 at 13:23 Comment(7)
"Finally both forEach/forEachOrdered are rarely useful". I could not agree more. It seems these methods are over-used.Formica
Why is it semantically necessary to use forEachOrdered in that code?Somber
@RealSkeptic, it's user-specified stream (passed into flatMap). It can be ordered, so it must be placed into the resulting stream in the same order.Bondon
Why? Where in the contract of flatMap did you see that it says that it will be placed in the output in the same order?Somber
@RealSkeptic, you are the real skeptic! Stream.of("a", "b", "c").flatMap(s -> Stream.of("1", "2", "3").map(s::concat)).spliterator().hasCharacteristics(Spliterator.ORDERED) returns true, thus the order for the resulting stream must be determined. If you feel that JDK documentation should explicitly say about this, feel free to submit a bug.Bondon
“forEach rarely useful”… As for filling a collection, I can see your point – but isn’t list.addAll(stream.collect(toList)) taking twice the memory of stream.forEach(list::add) ? But what about stream.forEach(this::doSomethingTotallyUnrelatedToFillingAList) ?Mackintosh
I strongly disagree that forEach is rarely used. When you need to process large collections of data, you obviously will get the task done in parallel. It's quite useful to submit a lambda function there. And when I do need the results ordered, I use it with an IntStream; the processing method receives the input collection, the output will collection, along with the int from then stream, which is the index for both collections. This way, no thread safe data collection has to be used, which let's your code run faster, and due to the sync collections, order is still preserved.Straggle
T
22

forEach() method performs an action for each element of this stream. For parallel stream, this operation does not guarantee to maintain order of the stream.

forEachOrdered() method performs an action for each element of this stream, guaranteeing that each element is processed in encounter order for streams that have a defined encounter order.

take the example below:

    String str = "sushil mittal";
    System.out.println("****forEach without using parallel****");
    str.chars().forEach(s -> System.out.print((char) s));
    System.out.println("\n****forEach with using parallel****");

    str.chars().parallel().forEach(s -> System.out.print((char) s));
    System.out.println("\n****forEachOrdered with using parallel****");

    str.chars().parallel().forEachOrdered(s -> System.out.print((char) s));

Output:

****forEach without using parallel****

sushil mittal

****forEach with using parallel****

mihul issltat

****forEachOrdered with using parallel****

sushil mittal
Tolley answered 26/6, 2018 at 6:59 Comment(0)
A
0

We may lose the benefits of parallelism if we use forEachOrdered() with parallel Streams.

As we know, In parallel programming element will print parallelly if we use forEach() method. so the order will not be fixed. But In the use of forEachOrdered() fixed order in parallel Streams.

Stream.of("AAA","BBB","CCC").forEachOrdered(s->System.out.println("Output:"+s)); Output:AAA Output:BBB Output:CCC

Aribold answered 17/1, 2021 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.