Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream ordered/unordered problems

I have the following code:

Set<Integer> l = new TreeSet<>();
l.add(1);
l.add(10);
l.add(3);
l.add(-3);
l.add(-4);

and I want to unorder the collection with:

l.stream().unordered().forEach(System.out::println);

but the forEach returns always the collection ordered!

Then I have also another doubt about the below sentence from here:

For sequential streams, the presence or absence of an encounter order does not affect performance, only determinism. If a stream is ordered, repeated execution of identical stream pipelines on an identical source will produce an identical result; if it is not ordered, repeated execution might produce different results.

In fact if I try this code on an unordered stream the results is always the same and never produce different results:

Arrays.stream( new int[]{8, -1, 3}).forEach(System.out::println);
Arrays.stream( new int[]{8, -1, 3}).forEach(System.out::println);

I really don't understand this API part...

like image 629
xdevel2000 Avatar asked Jan 25 '14 11:01

xdevel2000


People also ask

Does stream maintain order?

While most intermediate operations will maintain the order of the Stream, some will, by their nature, change it. unordered and empty are two more examples of intermediate operations that will ultimately change the ordering of a Stream.

What is the difference between stream () and parallelStream ()?

A sequential stream is executed in a single thread running on one CPU core. The elements in the stream are processed sequentially in a single pass by the stream operations that are executed in the same thread. A parallel stream is executed by different threads, running on multiple CPU cores in a computer.

What are unordered streams?

Description. A stream can be ordered or unordered. An ordered stream keeps the order of its elements. The Streams API can convert an ordered stream, which may represent an ordered data source such as a list or a sorted set, into an unordered stream.

When we should not use parallel stream?

Similarly, don't use parallel if the stream is ordered and has much more elements than you want to process, e.g. This may run much longer because the parallel threads may work on plenty of number ranges instead of the crucial one 0-100, causing this to take very long time.


1 Answers

The unordered() operation doesn't do any actions to explicitly unorder the stream. What it does is that it removes the constraint on the stream that it must remain ordered, thereby allowing subsequent operations to use optimizations that don't have to take ordering into consideration.

You can read about this in the Java 8 docs:

For sequential streams, the presence or absence of an encounter order does not affect performance, only determinism. If a stream is ordered, repeated execution of identical stream pipelines on an identical source will produce an identical result; if it is not ordered, repeated execution might produce different results.
For parallel streams, relaxing the ordering constraint can sometimes enable more efficient execution.

...

In cases where the stream has an encounter order, but the user does not particularly care about that encounter order, explicitly de-ordering the stream with unordered() may improve parallel performance for some stateful or terminal operations.

like image 200
Keppil Avatar answered Oct 21 '22 09:10

Keppil