Manikandan Kbk
Manikandan Kbk

Reputation: 151

Java Unordered() function

In java 8, when I do,

Type 1

list.stream().parallel().map(/**/).unordered().filter(/**/).collect(/**/);

Type 2

list.stream().parallel().unordered().map(/**/).filter(/**/).collect(/**/);

As both the streams are parallel, I can understand all the objects for each of the operations like filter, map etc.. will be executed parallely but the operations itself will be executed sequentially in the order defined.

Questions

1.In Type1, I do say unordered() after map() operation. So, does the map() operation try to handle 'ordering', because it is before unOrdered()?

2.In Type2, Ordering is not maintained across map, filter ops right? Is my understanding correct?

Upvotes: 2

Views: 551

Answers (2)

Randy Casburn
Randy Casburn

Reputation: 14175

The effect of .unordered() is to only remove constraints on the stream that it must remain ordered. So any intermediate operations in the pipeline are unaffected by an ordering constraint. In the example provided, assuming the internal operations of each are not stateful operations, .unordered() has no effect.

Here are some helpful quotes from the docs:

Stream Operations:

Traversal of the pipeline source does not begin until the terminal operation of the pipeline is executed.

So all of the effects of intermediate operations are consolidated and operate on an optimized representation of the input data. These means that regardless of the order of the intermediate operation, they effect the entirety of the operation of the pipeline the same way. This is true for parallel or sequential streams.

Ordering:

However, if the source has no defined encounter order, then any permutation of the values [2, 4, 6] would be a valid result.

This is related to your question about T1 (maintaining ordering). In the pipelines you have, this quote means there is nothing that will maintain order.

Upvotes: 3

Andreas
Andreas

Reputation: 159165

There are 3 Stream state-modifying methods:

  • sequential()

    Returns an equivalent stream that is sequential. May return itself, either because the stream was already sequential, or because the underlying stream state was modified to be sequential.

  • parallel()

    Returns an equivalent stream that is parallel. May return itself, either because the stream was already parallel, or because the underlying stream state was modified to be parallel.

  • unordered()

    Returns an equivalent stream that is unordered. May return itself, either because the stream was already unordered, or because the underlying stream state was modified to be unordered.

As you can see, all three may modify the underlying stream state, which means that the position in the stream chain of methods don't matter.

Your two examples are the same. So would these be:

list.stream().parallel().map(/**/).filter(/**/).unordered().collect(/**/);

list.stream().map(/**/).filter(/**/).unordered().parallel().collect(/**/);

list.stream().unordered().map(/**/).parallel().filter(/**/).collect(/**/);

list.stream().unordered().parallel().map(/**/).filter(/**/).collect(/**/);

You should click on the unordered link and read the javadoc to learn more about ordering of streams.

Upvotes: 5

Related Questions