The one-argument reduce has no identity value to fall back on for an empty stream, so it wraps its result in an Optional instead of returning the raw type directly.
List<Integer> nums = List.of(3, 7, 2, 9, 4);
Optional<Integer> max = nums.stream().reduce(Integer::max);
System.out.println("Max via reduce: " + max.get());
Optional<Integer> maxEmpty = List.<Integer>of().stream().reduce(Integer::max);
System.out.println("Reduce on empty stream: " + maxEmpty);
Max via reduce: 9
Reduce on empty stream: Optional.empty
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27