Skip to main content
PreparationZone Logo
PrepZone

reduce & Primitive Streams

Identities for parallel reduce, mapToInt, and the cost of boxing.

Pass checkpoint below

reduce

Java
public class Example {
    public static void main(String[] args) {
        int total = numbers.stream().reduce(0, Integer::sum);
            Optional<Integer> product = numbers.stream().reduce((a, b) -> a * b);
            int sum = numbers.stream().mapToInt(Integer::intValue).sum();
    }
}

The identity must be a true identity for the operation — wrong identities break parallel streams.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 1 section

  • Primitive streams

Test yourself

Answer these before moving on — recall is what makes it stick.