Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

java.util.Arrays — array operations and Arrays.stream

java.util.Arrays provides static array operations. Arrays.stream creates a sequential stream over an entire array or a selected range, enabling filtering and aggregation.

Version: Java SE 21 API reference; neither the repository runtime nor the production runtime is attested

Syntax

Arrays.stream(array)
Arrays.stream(array, startInclusive, endExclusive)

java.util.Arrays provides static methods for sorting, searching, copying, and comparing arrays. You do not need an instance of this class. This description targets Java SE 21; it does not attest the repository or production runtime.

Arrays.stream(array) creates a sequential stream: T[] produces Stream<T>, while int[], long[], and double[] produce IntStream, LongStream, and DoubleStream, respectively. Other primitive types have no direct overloads.

Arrays.stream(array, startInclusive, endExclusive) includes the range start but excludes its end. It requires 0 <= startInclusive <= endExclusive <= array.length; a violation throws ArrayIndexOutOfBoundsException.

A stream is not a copy of the array. Do not modify the source during processing. filter is lazy; terminal sum() triggers computation and consumes the stream. Create a new stream for another traversal.

Examples

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] values = {9, 2, 4, 6, 8};
        int sum = Arrays.stream(values, 1, 4)
                .filter(n -> n >= 4)
                .sum();
        System.out.println(sum);
    }
}

Result

10

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources