Java Arrays.spliterator() Method

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

Definition

In Java, Arrays.spliterator(array) returns a Spliterator: an object that can walk through the elements one at a time (tryAdvance), process all remaining elements (forEachRemaining), or split itself into two parts (trySplit) so that the halves can be processed separately.

Spliterators are the engine behind streams; Arrays.stream is built on one. You rarely need one directly, but it shows how a stream divides an array for parallel work. int[], long[], and double[] give the boxing-free Spliterator.OfInt, OfLong, and OfDouble.

A static method of java.util.Arrays (Java 8+): call it on the class, as in Arrays.spliterator(...); there is no Arrays object to create. It was added in Java 8.

Syntax

Get a spliterator for the whole array or a range.

Java
int[] a = {1, 2, 3};
Spliterator.OfInt s = Arrays.spliterator(a);
s.tryAdvance((int x) -> System.out.println(x));     // prints 1
String[] w = {"a", "b", "c", "d"};
Spliterator<String> part = Arrays.spliterator(w, 1, 3); // covers b, c
CallWhat it does
Arrays.spliterator(int[] a)Returns Spliterator.OfInt; also OfLong and OfDouble.
Arrays.spliterator(T[] a)Returns Spliterator<T>.
Arrays.spliterator(a, from, to)Covers indexes from to to - 1 only.
s.trySplit()Hands off a prefix of the remaining elements as a new spliterator.

The returned spliterator reports the characteristics SIZED, SUBSIZED, ORDERED, and IMMUTABLE, so estimateSize() is exact and elements come in index order.

Library declarations
Java
// java.util.Arrays (since Java 8)
public static Spliterator.OfInt spliterator(int[] array)
public static Spliterator.OfLong spliterator(long[] array)
public static Spliterator.OfDouble spliterator(double[] array)
public static <T> Spliterator<T> spliterator(T[] array)
// each also has (array, int startInclusive, int endExclusive)

Parameters

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

Return Value

Return ValueDescription
SpliteratorSpliterator.OfInt, OfLong, OfDouble, or Spliterator<T>, depending on the array type.

Compare with Similar Methods

Other ways to walk an array:

MethodHow it differs
Arrays.stream(a)A stream built on this spliterator; easier for most tasks.
for (int x : a)The simplest way to visit every element.
list.spliterator()The same idea for an ArrayList.
Arrays.asList(a).iterator()An Iterator over an object array.

Java Examples

Example 1 — tryAdvance, forEachRemaining, and estimateSize

Take the first element by itself, then process the rest, and watch the size estimate.

Java
import java.util.Arrays;
import java.util.Spliterator;

public class ArraysSpliteratorDemo {
    public static void main(String[] args) {
        int[] nums = {10, 20, 30, 40};
        Spliterator.OfInt it = Arrays.spliterator(nums);

        System.out.println("size: " + it.estimateSize());
        it.tryAdvance((int n) -> System.out.println("first: " + n));
        it.forEachRemaining((int n) -> System.out.println("rest: " + n));
        System.out.println("left: " + it.estimateSize());
    }
}

How It Works

  1. Primitive spliterator. For an int[] the method returns Spliterator.OfInt, so the values are passed as plain int.
  2. Exact size. Array spliterators are SIZED, so estimateSize() reports exactly 4 before anything is consumed.
  3. One step. tryAdvance hands over just the next element, 10, and returns true because there was one.
  4. The rest in one call. forEachRemaining visits 20, 30, and 40 in order; after that nothing is left and the estimate drops to 0.

Example 2 — Split an Array for Two Workers

Call trySplit once and process each half separately.

Java
import java.util.Arrays;
import java.util.Spliterator;

public class ArraysSpliteratorSplit {
    public static void main(String[] args) {
        String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h"};
        Spliterator<String> second = Arrays.spliterator(letters);
        Spliterator<String> first = second.trySplit();

        System.out.println("ordered: " + second.hasCharacteristics(Spliterator.ORDERED));
        System.out.print("first half:");
        first.forEachRemaining(s -> System.out.print(" " + s));
        System.out.println();
        System.out.print("second half:");
        second.forEachRemaining(s -> System.out.print(" " + s));
        System.out.println();
    }
}

How It Works

  1. Splitting hands off a prefix. For an ORDERED spliterator, trySplit must return the front part; the original keeps the rest.
  2. Down the middle. The JDK's array spliterator splits at the midpoint, so with eight letters each part gets four (an implementation detail, but a stable one for arrays).
  3. Characteristics. hasCharacteristics(Spliterator.ORDERED) confirms that the elements keep their index order.
  4. Independent halves. Each part can be processed on its own, which is exactly what a parallel stream does on different threads.

Example 3 — A Spliterator over Part of a double Array

Cover only indexes 1 to 3 of a double[] and add those values.

Java
import java.util.Arrays;
import java.util.Spliterator;

public class ArraysSpliteratorRange {
    public static void main(String[] args) {
        double[] temps = {18.5, 21.0, 23.5, 22.0, 19.5};
        Spliterator.OfDouble mid = Arrays.spliterator(temps, 1, 4);
        System.out.println("covers " + mid.estimateSize() + " values");

        double[] total = {0};
        mid.forEachRemaining((double t) -> total[0] += t);
        System.out.println("sum of indexes 1-3: " + total[0]);
    }
}

How It Works

  1. Range form. spliterator(temps, 1, 4) covers indexes 1, 2, and 3, so the size is 3.
  2. OfDouble. A double[] gives a Spliterator.OfDouble, and the typed lambda (double t) -> ... picks the DoubleConsumer overload.
  3. A one-slot array as a counter. Lambdas cannot reassign local variables, so the sum is kept in total[0].
  4. Exact sum. 21.0 + 23.5 + 22.0 = 66.5; all three values are exact in binary, so no rounding appears.

Frequently Asked Questions

A Spliterator.OfInt, which passes elements as int values without boxing. long[] gives OfLong and double[] gives OfDouble.
tryAdvance processes one element and returns whether there was one. forEachRemaining processes every element that is left.
It returns a new spliterator for a prefix of the remaining elements and keeps the rest. For arrays the JDK splits at the middle.
Both tryAdvance(IntConsumer) and tryAdvance(Consumer<? super Integer>) accept that lambda. Write (int x) -> ... to choose the primitive version.
Rarely. Arrays.stream or a for loop is simpler; spliterators matter when you write your own stream sources or parallel algorithms.

Next: Arrays.stream()

Process an array with the Stream API: sum, filter, map, collect.

stream() 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.

7 people found this page helpful