I used reduce with stream and parallelStream on the same array with same lambda expression, and I expected the same result but the output is different. However, the results are different and I don't know why.
The code:
System.out.println("Reduce me...");
Integer[] arrx = {1,2,3,4};
// Reducing the Array
Arrays
.stream(arrx)
.reduce((s1, s2) -> (int)Math.pow(s1,2) + (int)Math.pow(s2,2))
.ifPresent(System.out::println);
Arrays.asList(arrx)
.parallelStream()
.reduce((s1, s2) -> (int)Math.pow(s1,2) + (int)Math.pow(s2,2))
.ifPresent(System.out::println);
The output:
1172
650