16

I'm trying to understand why there is a different when I change from y.addAll(x) to x.addAll(y) in code snippet below:

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

I know, when I use parallelStream, there is more than one thread run at a time.

collect has three parameters; the first two parameters I understand. With the third parameter, I know x, y are substreams and they are ArrayLists, but I don't understand why the results are different in each case. I expected them to be the same.

  • (x, y) -> y.addAll(x) // output: [1]

  • (x, y) -> x.addAll(y) // output: [1, 2]

1
  • FYI, you can substitute List::add for (x, y) -> x.add(y). Same with addAll.
    – shmosel
    Commented Aug 5 at 6:27

2 Answers 2

23

Why one is correct and the other isn't

From the Javadocs of Stream#collect (specifically the last parameter, emphasis mine):

combiner - an associative, non-interfering, stateless function that accepts two partial result containers and merges them, which must be compatible with the accumulator function. The combiner function must fold the elements from the second result container into the first result container.

Similarly, a.addAll(b) adds all elements from b to a but not the other way round. It takes information from the parameter and modifies the receiver.

So, the contract of that method specifies that you have to merge the second argument of the lambda into the first.

If you do (x, y) -> x.addAll(y), it will add all elements of y into x adhering to the contract. However, with (x, y) -> y.addAll(x) you are adding it to the second element resulting in the the elements of y not being added to x which are then missing in the result.

What happens

This is done that way because parallel streams split processing into chunks where different threads process different chunks. After the processing, it needs to merge the elements together which is done using the combiner (the last lambda expression which is the one you talked about). This combiner needs to be able to combine two elements together and the first argument is then used for further processing while the second is discarded.

Let's say we have the numbers 1 and 2 as in your example and assume one thread processes a chunk containing 1 and the other thread processes a chunk containing 2. When collecting, each thread starts by creating a new ArrayList following the ArrayList::new in your code. The threads then add the elements of their corresponding chunks to the list resulting in two lists with one element each (1 for the first thread and 2 for the other). When both threads are finished, the combiner is called to merge/combine the results. With x.addAll(y), it adds the second list to the first which is then returned yielding the correct result. However, with y.addAll(x), it adds the elements of the first list to the second list but Java assumes you want the first list (as that's what you are supposed to modify) so collect returns the first list which doesn't contain the elements processed by the second thread.

8
  • 2
    @happy because collect() will return the first parameter, that is, x -- adding to y will not change the result (it could have been programmed to return the result of the last parameter, but that would not work, for example, with addAll() since it returns boolean)
    – user85421
    Commented Aug 3 at 18:10
  • 2
    @happy The way the stream is implemented, it keeps x (after any elements have been added to it) and discards y. By adding all elements of x to y, the stream will keep the original x without any of the y elements in it.
    – M. Justin
    Commented Aug 3 at 18:11
  • 1
    I added more information on that to my answer. I hope that clears it up. The contract states you should accumulate it into the first argument so it just ignores the second one.
    – dan1st
    Commented Aug 3 at 18:12
  • 1
    Note that this is different from the Collector definition, where you can do whatever you want with the parameters, so long as you return their combination.
    – shmosel
    Commented Aug 5 at 16:32
  • 1
    @shmosel you mean: the different between combiner in collect() of Stream and combiner in Collector.class
    – happy
    Commented Aug 6 at 6:01
9

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.