Java SE API

Stream.reduce()

Combines values; identity must be neutral and accumulator associative, stateless and noninterfering. Java SE API; example uses Java 21-compatible syntax.

Formal Syntax & Structure

java syntaxjava
T reduce(T identity, BinaryOperator<T> accumulator)

Parameters & Attributes

NameTypeDescription
identityTNeutral value
accumulatorBinaryOperator<T>Associative combination

Return Value

T

Combines values; identity must be neutral and accumulator associative, stateless and noninterfering.

Working Example

Example Snippetjava
import java.util.*;import java.util.stream.*;public class Main {    public static void main(String[] args) {        System.out.println(Stream.of(2, 3, 4).reduce(1, (a, b) -> a * b));        System.out.println(Stream.<Integer>empty().reduce(1, (a, b) -> a * b));        System.out.println(IntStream.empty().average().orElse(-1));    }}
Java Reference