Java ArrayDeque spliterator() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

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
CallWhat 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

ParameterDescription
Nonespliterator() takes no parameters.

Return Value

Return ValueDescription
Spliterator<E>A spliterator over the deque's elements, head to tail.

Compare with Similar Methods

spliterator and related tools:

MethodHow it differs
iterator()Simpler: one element at a time, no splitting.
deque.stream()Builds a stream on top of spliterator(); the usual way to use it.
forEach(action)Like forEachRemaining on a fresh spliterator.
toArray()Copies elements out instead of walking them.

Java Examples

Example 1 — tryAdvance and forEachRemaining

Take one element, then the rest, and watch estimateSize() drop to zero.

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

public class ArrayDequeSpliteratorDemo {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>(Arrays.asList("a", "b", "c", "d"));
        Spliterator<String> sp = d.spliterator();

        System.out.println("estimate: " + sp.estimateSize());
        sp.tryAdvance(s -> System.out.println("first: " + s));
        sp.forEachRemaining(s -> System.out.println("rest: " + s));
        System.out.println("estimate now: " + sp.estimateSize());
    }
}

How It Works

  1. Exact size. The spliterator is SIZED, so estimateSize() reports exactly 4.
  2. One step. tryAdvance runs its lambda for the head, a, and returns true (not printed).
  3. The rest in one go. forEachRemaining handles b, c, and d in order.
  4. Used up. Nothing is left, so the final estimate is 0. A new spliterator would be needed to walk the deque again.

Example 2 — Check the Spliterator's Characteristics

Ask which flags an ArrayDeque spliterator reports.

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

public class ArrayDequeSpliteratorFlags {
    public static void main(String[] args) {
        Spliterator<Integer> sp = new ArrayDeque<>(Arrays.asList(3, 1, 2)).spliterator();

        System.out.println("ORDERED:  " + sp.hasCharacteristics(Spliterator.ORDERED));
        System.out.println("SIZED:    " + sp.hasCharacteristics(Spliterator.SIZED));
        System.out.println("NONNULL:  " + sp.hasCharacteristics(Spliterator.NONNULL));
        System.out.println("SORTED:   " + sp.hasCharacteristics(Spliterator.SORTED));
        System.out.println("DISTINCT: " + sp.hasCharacteristics(Spliterator.DISTINCT));
    }
}

How It Works

  1. ORDERED. Elements always come out head to tail, so the order is meaningful.
  2. SIZED and NONNULL. The deque knows its exact size, and it can never contain null.
  3. Not SORTED. The values 3, 1, 2 stay in insertion order; a deque never sorts its elements.
  4. 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();
    }
}

How It Works

  1. Six values. The loop adds 10, 20, 30, 40, 50, and 60 at the tail.
  2. 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.
  3. null check. trySplit() may return null when splitting is not worth it, so the code checks before using first.
  4. 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.
Java 8, together with streams and lambdas.

Next: toArray()

Copy the elements into an array, head to tail.

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

14 people found this page helpful