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.
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
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.
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
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 aStream<int[]>with one element. UseArrays.stream()(orIntStream.of()).- Prefer primitive streams for numbers.
IntStreamavoids creatingIntegerobjects; callboxed()only when you need a collector or comparator. - Back to an array:
toArray()on a primitive stream givesint[]; on an object stream usetoArray(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; calllist.stream().char[]: to stream characters useString.valueOf(chars).chars()or loop.- Fixed size:
Arrays.asList(array).stream()works, but is just a longer way of writingArrays.stream(array).