Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Numeric operations on IntStream

Create and filter streams of primitive `int` values, then distinguish the result types and empty-stream behavior of counting, summing, and averaging.

Version: Java SE 21

Syntax

import java.util.OptionalDouble;
import java.util.function.IntPredicate;
import java.util.stream.IntStream;

// IntStream method signatures
static IntStream of(int t)
static IntStream of(int... values)
IntStream filter(IntPredicate predicate)
long count()
int sum()
OptionalDouble average()

In Java SE 21, java.util.stream.IntStream processes primitive int values rather than Integer objects. The static factory IntStream.of has two overloads: of(int) creates a one-element sequential stream, while of(int...) creates a sequential stream in the order the values are supplied. The second overload also accepts an int[] or no arguments, which gives an empty stream.

IntStream.filter(IntPredicate) is a lazy intermediate operation. It returns a stream containing only the elements for which the predicate returns true. The predicate must be non-interfering and stateless: it must not modify the stream source or depend on state that can change during processing. In n -> n % 2 == 0, % computes the remainder after division by 2, and == 0 checks whether that remainder is zero.

The terminal operations have different result types: IntStream.count() returns the number of elements as a long, while IntStream.sum() returns the total as an int. Both return zero on an empty stream. The sum can overflow int without throwing an exception; assigning the result to a long does not prevent overflow. IntStream.average() returns the arithmetic mean in an OptionalDouble, which is empty when the stream has no elements. Use orElse(...) to choose an explicit fallback.

A terminal operation consumes the pipeline. For another independent calculation on the same data, create a fresh stream from the source. Reusing a stream is not allowed and can throw IllegalStateException if detected.

Examples

import java.util.OptionalDouble;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        int[] values = {2, 3, 7, 10};

        long count = IntStream.of(values).count();
        int sum = IntStream.of(values).sum();
        OptionalDouble average = IntStream.of(values).average();
        int evenSum = IntStream.of(values).filter(n -> n % 2 == 0).sum();
        OptionalDouble negativeAverage = IntStream.of(values)
                .filter(n -> n < 0)
                .average();

        System.out.println("count = " + count);
        System.out.println("sum = " + sum);
        System.out.println("average = " + average.orElse(Double.NaN));
        System.out.println("even sum = " + evenSum);
        System.out.println("negative average = " + negativeAverage.orElse(Double.NaN));
    }
}

Result

count = 4
sum = 22
average = 5.5
even sum = 12
negative average = NaN

Each calculation starts with a fresh `IntStream.of(values)`. The even-number filter keeps `2` and `10`. The negative-number filter keeps nothing, so `NaN` comes from the explicit fallback, not from `average()`.

Related lessons

  • Java · Lesson 46 · Intermediate · Version: 2

Sources