ParallelStream vs stream.parallel

I was interested to know the difference between Collections.parallelStream()and Collections.stream().parallel(). According to Javadocs, it parallelStream()tries to return a parallel stream, while it stream().parallel()returns a parallel stream. After some testing of my own, I did not find any differences. Where is the difference in these two methods? Is one implementation more time-efficient than the other? Thanks.

+6
source share
2 answers

Even if they act the same at the moment, there is a difference - at least in their documentation, as you indicated correctly; which may be used in the future, as far as I can tell.

parallelStream Collection :

default Stream<E> parallelStream() {
    return StreamSupport.stream(spliterator(), true);
}

, ( Collections).

, , , non-parallel Stream. , , .

parallelStream - Stream; parallel :

  Collections.some()
       .parallelStream() // actually sequential
       .parallel() // force it to be parallel

, .

, - , parallelStream parallel, - .

, , , ; , , .

+4

Collections.parallelStream() Collections.stream().parallel(). , , ForkJoinPool ( ).

+3

Source: https://habr.com/ru/post/1017025/


All Articles