I am trying to understand this Java Stream method
<R> R collect(Supplier<R> supplier,
BiConsumer<R, ? super T> accumulator,
BiConsumer<R, R> combiner);
The following code should give as a result 55 put instead 0 is printed out. Can someone explain to me what is wrong and how to change it so that 55 is printed?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Create a supplier that returns a new mutable result container, in this case an Integer accumulator.
Supplier<Integer> supplier = () -> 0;
// Create an accumulator that adds the square of the current element to the result container.
BiConsumer<Integer, Integer> accumulator = (result, element) -> result += element * element;
// Create a combiner that adds two result containers together.
BiConsumer<Integer, Integer> combiner = (result1, result2) -> result1 += result2;
// Collect the results of the reduction operation into a single Integer value.
Integer sumOfSquares = numbers.stream()
.collect(supplier, accumulator, combiner);
System.out.println(sumOfSquares); // 55
You are working on ints/Integer which are not mutable. Thus both, the accumulator and the combiner don't alter the result. Thus is stays 0.
You can avoid this problem by using a mutable object like AtomicInteger to do the operations and use a finisher to convert it back to Integer: