Java Arrays.stream() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

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
CallWhat it does
Arrays.stream(int[] a)IntStream; also LongStream and DoubleStream.
Arrays.stream(T[] a)Stream<T> for object arrays.
Arrays.stream(a, from, to)Only indexes from to to - 1.
Arrays.stream(a).boxed().collect(Collectors.toList())int[] to List<Integer>.

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

ParameterDescription
arrayThe source array. Must not be null.
startInclusive, endExclusive (optional)The range of indexes to stream.

Return Value

Return ValueDescription
StreamIntStream, LongStream, DoubleStream, or Stream<T>, sequential and ordered.

Compare with Similar Methods

Related conversions:

MethodHow it differs
Arrays.asList(a)A List view of an object array; no stream needed.
Arrays.spliterator(a)The lower-level traversal object behind the stream.
Arrays.parallelPrefix(a, op)Running totals stored back into the array.
Stream.of(a)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());
    }
}

How It Works

  1. IntStream from int[]. Arrays.stream(marks) returns an IntStream, which has sum(), max(), and average() built in.
  2. A new stream each time. Every line calls Arrays.stream again, because a stream is used up by its terminal operation.
  3. 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.
  4. 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.

Java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArraysStreamFilter {
    public static void main(String[] args) {
        int[] nums = {3, 8, 5, 12, 7, 6};
        int[] evensSquared = Arrays.stream(nums).filter(n -> n % 2 == 0).map(n -> n * n).toArray();
        System.out.println(Arrays.toString(evensSquared));

        String[] names = {"Ivy", "Oscar", "Eve", "Liam"};
        List<String> short3 = Arrays.stream(names).filter(s -> s.length() == 3).collect(Collectors.toList());
        System.out.println(short3);
    }
}

How It Works

  1. Keep the evens. filter(n -> n % 2 == 0) lets 8, 12, and 6 through, in their original order.
  2. Square them. map(n -> n * n) turns them into 64, 144, and 36, and toArray() collects an int[].
  3. Object stream. names is a String[], so Arrays.stream returns a Stream<String>.
  4. Collect to a List. Ivy and Eve have three letters; Collectors.toList() gathers them into a list that prints as [Ivy, Eve].

Example 3 — Stream a Range, Box to a List, and Join Text

Sum part of an array, turn an int[] into a List<Integer>, and join strings with a separator.

Java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArraysStreamRange {
    public static void main(String[] args) {
        int[] readings = {5, 9, 2, 7, 4, 8};
        System.out.println("sum of [1, 4): " + Arrays.stream(readings, 1, 4).sum());

        List<Integer> boxed = Arrays.stream(readings).boxed().collect(Collectors.toList());
        System.out.println("as List<Integer>: " + boxed);

        String joined = Arrays.stream(new String[] {"a", "b", "c"}).collect(Collectors.joining("-"));
        System.out.println(joined);
    }
}

How It Works

  1. Range stream. Arrays.stream(readings, 1, 4) covers indexes 1, 2, and 3, so the sum is 9 + 2 + 7 = 18.
  2. From int to Integer. boxed() wraps each int in an Integer, which is what a List<Integer> needs.
  3. The asList alternative fails here. Arrays.asList(readings) would give a one-element List<int[]>; streaming and boxing gives six separate numbers.
  4. 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.

Next: Arrays.toString()

Print a one-dimensional array as readable text.

toString() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful