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

parallelStream with collect method()

$
0
0

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]


Viewing all articles
Browse latest Browse all 6

Trending Articles