I have been curious about the difference between Collections.parallelStream()
and Collections.stream().parallel()
. According to the Javadocs, parallelStream()
tries to return a parallel stream, whereas stream().parallel()
returns a parallel stream. Through some testing of my own, I have found no differences. Where does the difference in these two methods lie? Is one implementation more time efficient than another? Thanks.
Short answer: no difference. Long answer: noooooooo difference. They're just aliases for each other.
The performance of both streams degrades fast when the number of values increases. However, the parallel stream performs worse than the sequential stream in all cases.
As CPUs with multiple cores are now extremely common, Java parallel stream is considered an excellent addition in Java 8 as it allows developers to utilize multiple cores present in their processor for tasks execution.
Even if they act the same at the moment, there is a difference - at least in their documentation, as you correctly pointed out; that might be exploited in the future as far as I can tell.
At the moment the parallelStream
method is defined in the Collection
interface as:
default Stream<E> parallelStream() { return StreamSupport.stream(spliterator(), true); }
Being a default method it could be overridden in implementations (and that's what Collections
inner classes actually do).
That hints that even if the default method returns a parallel Stream, there could be Collections that override this method to return a non-parallel Stream
. That is the reason the documentation is probably the way it is.
At the same time even if parallelStream
returns a sequential stream - it is still a Stream
, and then you could easily call parallel
on it:
Collections.some() .parallelStream() // actually sequential .parallel() // force it to be parallel
At least for me, this looks weird.
It seems that the documentation should somehow state that after calling parallelStream
there should be no reason to call parallel
again to force that - since it might be useless or even bad for the processing.
EDIT
For anyone reading this - please read the comments by Holger also; it covers cases beyond what I said in this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With