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’m new with paralell stream java. I know, when I use parallelStream(), there are more than one thread run at a time, collect() method has three parameter, the first two parameter I can understand.
So, with the third parameter, I know x, y are substream and they are ArrayList, but I can’t understand why the result variable are different each case. I think they are same result.
-
(x, y) -> y.addAll(x) // output: [1]
-
(x, y) -> x.addAll(y) // output: [1, 2]
Please be patient with me. Thanks