Неожиданный результат при вызове метода сбора в потоке JavaJAVA

Программисты JAVA общаются здесь
Anonymous
Неожиданный результат при вызове метода сбора в потоке Java

Сообщение Anonymous »

Я пытаюсь понять этот метод Java Stream
R collect(Supplier supplier,
BiConsumer accumulator,
BiConsumer combiner);

Следующий код должен дать в результате 55 вместо 0.
Может кто-нибудь объяснить мне, что не так и как изменить это, чтобы было напечатано 55?
List 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 supplier = () -> 0;

// Create an accumulator that adds the square of the current element to the result container.
BiConsumer accumulator = (result, element) -> result += element * element;

// Create a combiner that adds two result containers together.
BiConsumer 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


Подробнее здесь: https://stackoverflow.com/questions/781 ... ava-stream

Вернуться в «JAVA»