Quantcast
Channel: Understanding parallelStream and collector order - Stack Overflow
Viewing all articles
Browse latest Browse all 6

Answer by tquadrat for Understanding parallelStream and collector order

$
0
0

I have renamed some of your variables to illustrate a little bit better what is going on.

List<Integer> result = List.of( 1, 2 )    .parallelStream()     .collect(        ArrayList::new,        (thisList,value) -> thisList.add( value ),        (thisList,theOtherList) -> thisList.addAll( theOtherList )    );System.out.println( result );

The method Stream::collect returns thisList.

When you change the third parameter of the call to collect() like this:

…(thisList,theOtherList) -> theOtherList.addAll( thisList )…

you add the contents of thisList to theOtherList and thisList remains unchanged, meaning the contents of theOtherList is not added to it.

That the result is just 1 is coincidential.


Viewing all articles
Browse latest Browse all 6

Trending Articles