Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I collect a parallel stream to an arbitrarily large array but not a sequential stream?

From answering this question, I ran into a peculiar feature. The following code works as I assumed it would (the first two values within the existing array would be overridden):

Integer[] newArray = Stream.of(7, 8)
                           .parallel()
                           .toArray(i -> new Integer[] {1, 2, 3, 4, 5, 6});

System.out.println(Arrays.toString(newArray));

Output:

[7, 8, 3, 4, 5, 6]

However, attempting this with a sequential stream throws an IllegalStateException:

Integer[] newArray = Stream.of(7, 8)
                           .toArray(i -> new Integer[] {1, 2, 3, 4, 5, 6});

System.out.println(Arrays.toString(newArray));

Output:

Exception in thread "main" java.lang.IllegalStateException: Begin size 2 is not equal to fixed size 6
    at java.base/java.util.stream.Nodes$FixedNodeBuilder.begin(Nodes.java:1222)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:483)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:550)
    at java.base/java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
    at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:517)
    at test/test.Test.main(Test.java:30)

I'm curious as to why the sequential stream does not overwrite elements of the array as the parallel stream does. I searched around a bit and was not able to find documentation regarding this, but I assume it exists somewhere.

like image 251
Jacob G. Avatar asked Apr 10 '18 18:04

Jacob G.


People also ask

What is the difference between parallel stream and sequential stream?

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 can't be done with parallel streams?

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.

What is the disadvantage of parallel stream in Java 8?

Parallel Streams can actually slow you down It breaks them into subproblems which then run on separate threads for processing, these can go to different cores and then get combined when they're done. This all happens under the hood using the fork/join framework.

Are streams sequential?

Sequential Streams. By default, any stream operation in Java is processed sequentially, unless explicitly specified as parallel.


1 Answers

The generator function is required to produce "a new array of the desired type and the provided length." If you don't comply with the spec, behavior is undefined.

like image 89
shmosel Avatar answered Nov 15 '22 15:11

shmosel