average() on a numeric stream always wraps its result in an OptionalDouble because an empty stream has no average, so callers must check isPresent or supply a default instead of reading a raw double.
double[] prices = {19.99, 5.50, 12.25};
OptionalDouble avg = Arrays.stream(prices).average();
System.out.println("Average present? " + avg.isPresent());
System.out.printf(Locale.ROOT, "Average: %.2f%n", avg.getAsDouble());
OptionalDouble none = DoubleStream.empty().average();
System.out.println("Average of empty stream: " + none);
Average present? true
Average: 12.58
Average of empty stream: OptionalDouble.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