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
Call
What 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
Parameter
Description
array
The array to traverse. Must not be null.
startInclusive, endExclusive (optional)
The range of indexes to cover.
Return Value
Return Value
Description
Spliterator
Spliterator.OfInt, OfLong, OfDouble, or Spliterator<T>, depending on the array type.
ordered: true
first half: a b c d
second half: e f g h
How It Works
Splitting hands off a prefix. For an ORDERED spliterator, trySplit must return the front part; the original keeps the rest.
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).
Characteristics.hasCharacteristics(Spliterator.ORDERED) confirms that the elements keep their index order.
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]);
}
}
Output
covers 3 values
sum of indexes 1-3: 66.5
How It Works
Range form.spliterator(temps, 1, 4) covers indexes 1, 2, and 3, so the size is 3.
OfDouble. A double[] gives a Spliterator.OfDouble, and the typed lambda (double t) -> ... picks the DoubleConsumer overload.
A one-slot array as a counter. Lambdas cannot reassign local variables, so the sum is kept in total[0].
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.