CKTIS / Java
IntStream — filtering and aggregating int values
IntStream processes primitive int values. It supports filtering data, testing conditions, and calculating element counts, sums, and averages.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
IntStream.of(int... values)
IntStream filter(IntPredicate predicate)
boolean allMatch(IntPredicate predicate)
long count()
int sum()
OptionalDouble average()java.util.stream.IntStream is a stream of primitive int values rather than Integer objects. It describes data processing, not storage like a collection. IntStream.of(int) creates a single-element stream, while IntStream.of(int... values) creates a sequential, ordered stream of the supplied values; it also accepts int[].
IntStream.filter(IntPredicate) retains matching elements. It is a lazy intermediate operation: calling it alone does not run the filtering. The predicate should be stateless and must not modify the source.
IntStream.allMatch(IntPredicate) returns boolean and may short-circuit; it returns true for an empty stream. IntStream.count() returns long, while IntStream.sum() returns int; both return 0 for an empty stream. IntStream.average() returns an OptionalDouble, empty when there are no elements.
These four operations are terminal. Do not reuse a stream after a terminal operation: create a new one from the source. The example does this for each calculation. orElse(0.0) explicitly chooses a fallback for an empty average.
Examples
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int[] values = {1, 2, 3, 4};
boolean positive = IntStream.of(values).allMatch(n -> n > 0);
long evenCount = IntStream.of(values).filter(n -> n % 2 == 0).count();
int sum = IntStream.of(values).sum();
double average = IntStream.of(values).average().orElse(0.0);
System.out.println("positive=" + positive);
System.out.println("evenCount=" + evenCount);
System.out.println("sum=" + sum);
System.out.println("average=" + average);
}
}Result
positive=true evenCount=2 sum=10 average=2.5
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.