Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. stream.forEach() / reduce() / count()

stream.forEach() / reduce() / count()

As terminal operations on streams, use forEach() to iterate over all elements, reduce() to aggregate (fold) elements, and count() to count elements (Java 8+). Numeric streams also support sum(), average(), and more.

Syntax

// Performs an action on each element (terminal operation).
stream.forEach(element -> action);

// Aggregates elements (identity, (accumulator, element) -> new accumulator).
stream.reduce(identity, (a, b) -> operation);

// Returns the number of elements.
stream.count();

// Returns the sum of a numeric stream.
intStream.sum();

// Returns the average of a numeric stream (OptionalDouble).
intStream.average();

Method List

MethodDescription
forEach(Consumer)A terminal operation that executes a lambda expression for each element. Returns no value.
reduce(T identity, BinaryOperator)Aggregates elements using an identity value and a binary operator. Returns the aggregated result.
count()Returns the number of elements in the stream as a long.
sum()Returns the sum of an IntStream, LongStream, or DoubleStream.
average()Returns the average of an IntStream or similar as an OptionalDouble.

Sample Code

import java.util.Arrays;
import java.util.List;

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

// Use forEach to print all elements.
numbers.stream()
    .forEach(n -> System.out.println(n)); // Prints 1 through 5.

// You can also use a method reference for brevity.
numbers.stream().forEach(System.out::println);

// Use reduce to compute the sum (identity 0, (sum, element) -> sum + element).
int sum = numbers.stream()
    .reduce(0, (a, b) -> a + b);
System.out.println(sum); // Prints 15.

// Use reduce to find the maximum value.
int max = numbers.stream()
    .reduce(Integer.MIN_VALUE, (a, b) -> a > b ? a : b);
System.out.println(max); // Prints 5.

// Use count to check the number of matching elements.
long count = numbers.stream()
    .filter(n -> n % 2 == 0)
    .count();
System.out.println(count); // Prints 2 (the count of even numbers).

// Convert to a numeric stream with mapToInt, then use sum and average.
int total = numbers.stream().mapToInt(Integer::intValue).sum();
System.out.println(total); // Prints 15.

double avg = numbers.stream().mapToInt(Integer::intValue).average().orElse(0);
System.out.println(avg); // Prints 3.0.

Notes

forEach() behaves similarly to an enhanced for loop on a collection, but its key advantage is that it can be chained with intermediate stream operations. You cannot modify local variables inside forEach() — they must be effectively final.

reduce() is a general-purpose aggregation operation suitable for computing sums, products, maximum values, string concatenation, and more. That said, for common operations like sums and maximums, using dedicated methods such as sum() and max() on IntStream improves readability.

To collect a stream into a collection, see collect() / toList().

If you find any errors or copyright issues, please .