Java Programs›Java 8 Streams›Find Sum and Average Using Streams

Find Sum and Average Using Streams in Java

beginner·  Java 8 Streams  ·  Streams

Problem

An IntStream is a stream of primitive ints; unlike a Stream of Integer objects it offers ready-made numeric operations such as sum, average, min and max.

Given a list of exam scores, compute their sum and their average.

Input
[72, 88, 95, 61, 84]
Output
Sum: 400, Average: 80.0

Java Program

Java
import java.util.Arrays; import java.util.List; public class SumAndAverageStream { public static void main(String[] args) { List<Integer> scores = Arrays.asList(72, 88, 95, 61, 84); int sum = scores.stream() .mapToInt(Integer::intValue) // unbox to an IntStream, which has sum() and average() .sum(); double average = scores.stream() .mapToInt(Integer::intValue) .average() // OptionalDouble: empty if the list is empty .orElse(0.0); System.out.println("Sum: " + sum); System.out.println("Average: " + average); } }

Output

Sum: 400 Average: 80.0

Core Logic

Converting the Stream of Integer into an IntStream unlocks numeric terminal operations, so totals need no manual loop or accumulator.

How It Works
  1. 1mapToInt(Integer::intValue) unboxes each Integer into a primitive int.
  2. 2sum() adds the values and returns an int.
  3. 3average() returns an OptionalDouble, because an empty list has no average.
  4. 4orElse(0.0) supplies a fallback for the empty case.
72 + 88 + 95 + 61 + 84 = 400, and 400 / 5 = 80.0.
💡

Key Point: A stream can only be consumed once, which is why the code calls scores.stream() twice — reusing the first stream would throw IllegalStateException.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Each pipeline makes one pass with a running total; this version makes two passes, still linear.

Key Concepts

mapToInt()IntStream.sum()average()OptionalDouble

Approach 2: Using IntSummaryStatistics

Java
import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; public class SumAndAverageStatistics { public static void main(String[] args) { List<Integer> scores = Arrays.asList(72, 88, 95, 61, 84); IntSummaryStatistics stats = scores.stream() .mapToInt(Integer::intValue) .summaryStatistics(); // one pass collects count, sum, min, max and average System.out.println("Count: " + stats.getCount()); System.out.println("Sum: " + stats.getSum()); System.out.println("Min: " + stats.getMin()); System.out.println("Max: " + stats.getMax()); System.out.println("Average: " + stats.getAverage()); } }

Output

Count: 5 Sum: 400 Min: 61 Max: 95 Average: 80.0

Core Logic

When you need several statistics, summaryStatistics() computes all of them in a single pass.

How It Works
  1. 1summaryStatistics() is the terminal operation on the IntStream.
  2. 2The returned object exposes getCount, getSum, getMin, getMax and getAverage.
  3. 3For an empty stream it returns sensible defaults (count 0, average 0.0) instead of an Optional.
One pass over the five scores yields count 5, sum 400, min 61, max 95 and average 80.0.
💡

Key Point: This is the efficient choice for dashboards and reports, where you'd otherwise stream the same list four or five times.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: A single pass updates five running values.

Key Concepts

summaryStatistics()IntSummaryStatistics

Interview Questions

Why does average() return an OptionalDouble?

An empty stream has no average, so the result may be absent. orElse(0.0) or getAsDouble() gets the value out.

How do you sum a field of objects?

list.stream().mapToInt(Item::getQuantity).sum(), or Collectors.summingInt(Item::getQuantity) inside a collector.

Learn the Concept

Related Programs