In Java, spliterator() returns a Spliterator over the elements of an ArrayDeque, from head to tail. A spliterator is a "splittable iterator": it can hand out elements one at a time (tryAdvance), all at once (forEachRemaining), or split itself into two parts (trySplit) so that parallel streams can share the work.
You rarely call it directly: deque.stream() uses it for you. The spliterator is late-binding (it looks at the deque when you first use it, not when you create it) and fail-fast. It reports the characteristics SIZED, SUBSIZED, ORDERED, and NONNULL.
An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety. spliterator() was added in Java 8.
Syntax
Get the spliterator, then pull elements from it.
Java
Spliterator<String> sp = deque.spliterator();
sp.tryAdvance(s -> System.out.println(s)); // one element
sp.forEachRemaining(System.out::println); // the rest
Call
What it does
sp.tryAdvance(action)
Processes one element; returns false if none is left.
sp.forEachRemaining(action)
Processes all remaining elements.
sp.trySplit()
Hands part of the elements to a new spliterator, or returns null.
sp.estimateSize()
Number of elements left (exact here, since it is SIZED).
How a spliterator divides its elements in trySplit is an implementation detail. Current JDKs split an ArrayDeque roughly in half, giving the first half to the new spliterator.
Library declarations
Java
// java.util.ArrayDeque<E>
public Spliterator<E> spliterator() // since 1.8
// java.util.Spliterator<T>
boolean tryAdvance(Consumer<? super T> action)
void forEachRemaining(Consumer<? super T> action)
Spliterator<T> trySplit()
long estimateSize()
boolean hasCharacteristics(int characteristics)
Parameters
Parameter
Description
None
spliterator() takes no parameters.
Return Value
Return Value
Description
Spliterator<E>
A spliterator over the deque's elements, head to tail.
ORDERED. Elements always come out head to tail, so the order is meaningful.
SIZED and NONNULL. The deque knows its exact size, and it can never contain null.
Not SORTED. The values 3, 1, 2 stay in insertion order; a deque never sorts its elements.
Not DISTINCT. A deque allows duplicates, so it cannot promise unique elements.
Example 3 — Split the Work with trySplit
Split a six-element spliterator into two parts and walk each part separately.
Java
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Spliterator;
public class ArrayDequeSpliteratorSplit {
public static void main(String[] args) {
Deque<Integer> d = new ArrayDeque<>();
for (int i = 1; i <= 6; i++) {
d.add(i * 10);
}
Spliterator<Integer> second = d.spliterator();
Spliterator<Integer> first = second.trySplit();
if (first != null) {
System.out.print("first part:");
first.forEachRemaining(n -> System.out.print(" " + n));
System.out.println();
}
System.out.print("second part:");
second.forEachRemaining(n -> System.out.print(" " + n));
System.out.println();
}
}
Output
first part: 10 20 30
second part: 40 50 60
How It Works
Six values. The loop adds 10, 20, 30, 40, 50, and 60 at the tail.
Split.trySplit() returns a new spliterator for the front part, and the original keeps the rest. On current JDKs a six-element deque splits three and three.
null check.trySplit() may return null when splitting is not worth it, so the code checks before using first.
Parallel idea. Each part could be processed by a different thread; parallel streams do exactly this splitting for you.
Frequently Asked Questions
It returns a Spliterator over the elements, from head to tail, which streams use to read and split the data.
SIZED, SUBSIZED, ORDERED, and NONNULL.
The spliterator binds to the deque contents when it is first used, not when spliterator() is called.
No. deque.stream() and deque.parallelStream() call it for you.