Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Arrays.stream(T[]): create a stream from an array

Use an array of reference-type elements as the source of a sequential stream, filter its elements, and collect the result into an array of the right type.

Version: Java SE 21

Syntax

public static <T> Stream<T> stream(T[] array)

Arrays.stream(T[]) is a static method in java.util.Arrays, available since Java 8. It uses an array of reference-type elements as the source of a sequential java.util.stream.Stream<T> in index order. For example, a String[] yields a Stream<String>. The method does not copy the source array; passing a null array reference throws NullPointerException.

Intermediate operations such as filter are lazy: processing starts when a terminal operation such as toArray() is called. Do not modify the source array while it is being processed. Do not reuse the same stream after a terminal operation. To process the array again, create a new stream.

The range overload, Arrays.stream(array, startInclusive, endExclusive), includes the start index but excludes the end index. It requires 0 <= startInclusive <= endExclusive <= array.length; invalid bounds throw ArrayIndexOutOfBoundsException. The overloads for int[], long[], and double[] return IntStream, LongStream, and DoubleStream, respectively. There is no direct overload for other one-dimensional primitive arrays such as boolean[].

The no-argument toArray() method on a Stream<T> has a declared return type of Object[], but Java SE 21 does not specify the exact runtime class of the returned array. Do not cast that result to String[]; if you need a String[], use toArray(String[]::new) on a stream of strings.

In reflective code, you can pass the resulting Object[] to Constructor.newInstance(Object...). Its elements become successive arguments to the selected constructor. Their number, order, and types must match its parameters, allowing for permitted conversions. A mismatch in argument count or type throws IllegalArgumentException.

Examples

import java.util.Arrays;

public class Main {
    public record Pair(String first, String second) {}

    public static void main(String[] args) throws ReflectiveOperationException {
        String[] input = {"Ada", " ", "Lin"};
        Object[] arguments = Arrays.stream(input)
                .filter(name -> !name.isBlank())
                .toArray();

        Pair pair = Pair.class.getConstructor(String.class, String.class)
                .newInstance(arguments);

        System.out.println(Arrays.toString(arguments));
        System.out.println(pair.first() + " / " + pair.second());
    }
}

Result

[Ada, Lin]
Ada / Lin

`filter` discards the string containing only one space. `toArray()` puts `Ada` and `Lin` into an `Object[]` in that order, and `newInstance` passes them as two arguments to the `Pair` constructor.

Related lessons

  • Java · Lesson 59 · Advanced · Version: 2
  • Java · Lesson 63 · Basic · Version: 2

Sources