In Java, Arrays.stream(array) returns a sequential stream whose elements are the array's elements in index order. An int[] gives an IntStream, a long[] a LongStream, a double[] a DoubleStream, and an object array a Stream<T>.
Streams let you describe what to compute (filter, map, sum, collect) instead of writing the loop yourself. The primitive streams add number-friendly methods such as sum(), average(), and max(), and boxed() turns them into a Stream<Integer> when you need a List.
A static method of java.util.Arrays (Java 8+): call it on the class, as in Arrays.stream(...); there is no Arrays object to create. It was added in Java 8 together with the Stream API.
Syntax
Stream the whole array or a range, then chain stream operations.
Java
int[] a = {4, 1, 3};
int total = Arrays.stream(a).sum(); // 8
int[] doubled = Arrays.stream(a).map(x -> x * 2).toArray(); // [8, 2, 6]
String[] w = {"x", "yy"};
long longWords = Arrays.stream(w).filter(s -> s.length() > 1).count(); // 1
Only int[], long[], double[], and object arrays are supported, each with a range form. There is no overload for char[], byte[], short[], float[], or boolean[]; for a char[], new String(chars).chars() gives an IntStream of character codes.
Library declarations
Java
// java.util.Arrays (since Java 8)
public static IntStream stream(int[] array)
public static LongStream stream(long[] array)
public static DoubleStream stream(double[] array)
public static <T> Stream<T> stream(T[] array)
// each also has (array, int startInclusive, int endExclusive)
Parameters
Parameter
Description
array
The source array. Must not be null.
startInclusive, endExclusive (optional)
The range of indexes to stream.
Return Value
Return Value
Description
Stream
IntStream, LongStream, DoubleStream, or Stream<T>, sequential and ordered.
Same as Arrays.stream for object arrays; avoid it for int[], where it gives a Stream<int[]>.
Java Examples
Example 1 — Sum, Maximum, and Average of Marks
Compute three statistics from an int[] without writing a loop.
Java
import java.util.Arrays;
public class ArraysStreamDemo {
public static void main(String[] args) {
int[] marks = {72, 88, 95, 61, 84};
System.out.println("sum: " + Arrays.stream(marks).sum());
System.out.println("max: " + Arrays.stream(marks).max().getAsInt());
System.out.println("average: " + Arrays.stream(marks).average().getAsDouble());
}
}
Output
sum: 400
max: 95
average: 80.0
How It Works
IntStream from int[].Arrays.stream(marks) returns an IntStream, which has sum(), max(), and average() built in.
A new stream each time. Every line calls Arrays.stream again, because a stream is used up by its terminal operation.
Optional results.max() returns an OptionalInt and average() an OptionalDouble, since an empty array has neither; the array is not empty, so getAsInt() and getAsDouble() are safe.
The numbers. 72 + 88 + 95 + 61 + 84 = 400, the largest mark is 95, and 400 / 5 = 80.0.
Example 2 — Filter, Transform, and Collect
Square the even numbers into a new int[], and collect the three-letter names into a List.
sum of [1, 4): 18
as List<Integer>: [5, 9, 2, 7, 4, 8]
a-b-c
How It Works
Range stream.Arrays.stream(readings, 1, 4) covers indexes 1, 2, and 3, so the sum is 9 + 2 + 7 = 18.
From int to Integer.boxed() wraps each int in an Integer, which is what a List<Integer> needs.
The asList alternative fails here.Arrays.asList(readings) would give a one-element List<int[]>; streaming and boxing gives six separate numbers.
Join with a separator.Collectors.joining("-") puts a dash between the strings, producing a-b-c.
Frequently Asked Questions
An IntStream, which has number methods like sum(), average(), and max(). long[] gives LongStream, double[] gives DoubleStream, and object arrays give Stream<T>.
Arrays.stream(array).boxed().collect(Collectors.toList()). Arrays.asList does not work for int[].
Yes. Arrays.stream(array, from, to) streams indexes from to to - 1.
No, there is no char[] overload. Use new String(chars).chars() for an IntStream of character codes.
No. A stream can have only one terminal operation; call Arrays.stream(array) again for another calculation.