Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

Java Arrays.stream()

Reference for Arrays.stream(): turn arrays into object or primitive streams, stream a slice of an array, and avoid the boxing and Arrays.asList traps.

Arrays.stream() creates a stream whose source is an array. For an object array you get a Stream<T>; for int[], long[] and double[] you get an IntStream, LongStream or DoubleStream. The primitive streams avoid boxing and come with handy terminal operations such as sum(), average() and max().

An overload takes a start (inclusive) and end (exclusive) index, which streams only a slice. The stream reads the array lazily when a terminal operation runs; it does not copy it.

Java
int total = Arrays.stream(new int[]{4, 8, 15}).sum();          // 27
List<String> up = Arrays.stream(new String[]{"a", "b"})
                        .map(String::toUpperCase).toList();

Try it

Java Primitive and object streams

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Primitive arrays and generics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
static IntStream      stream(int[] array)
static IntStream      stream(int[] array, int startInclusive, int endExclusive)
static LongStream     stream(long[] array)
static DoubleStream   stream(double[] array)
static <T> Stream<T>  stream(T[] array)
static <T> Stream<T>  stream(T[] array, int startInclusive, int endExclusive)
Name Description
array The source array. Must not be null.
startInclusive First index to include.
endExclusive Index after the last element to include.

Returns: a sequential stream over the elements, in index order. There is no overload for char[], byte[], short[], float[] or boolean[].

Throws: NullPointerException for a null array, ArrayIndexOutOfBoundsException if the range is invalid.

Usage notes

  • Streams do not store data. The array is the storage; changing it before the terminal operation changes what the stream sees, so do not mutate the source during the pipeline.
  • Stream.of(int[]) is a trap. With a primitive array it produces a Stream<int[]> with one element. Use Arrays.stream() (or IntStream.of()).
  • Prefer primitive streams for numbers. IntStream avoids creating Integer objects; call boxed() only when you need a collector or comparator.
  • Back to an array: toArray() on a primitive stream gives int[]; on an object stream use toArray(String[]::new) so you get a typed array.
  • Parallel: call .parallel() for large arrays with CPU-heavy, stateless work. The array is split efficiently because it is a sized, indexable source.
  • Arrays.stream(list.toArray()) is never needed; call list.stream().
  • char[]: to stream characters use String.valueOf(chars).chars() or loop.
  • Fixed size: Arrays.asList(array).stream() works, but is just a longer way of writing Arrays.stream(array).