Java ArrayList spliterator() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java 8 and later, spliterator() returns a Spliterator<E>: a "splittable iterator" over the elements of an ArrayList. It can visit elements one at a time with tryAdvance(), visit all remaining ones with forEachRemaining(), and hand part of its work to a new spliterator with trySplit().

Most programs never call it directly: list.stream() and parallel streams use it behind the scenes. Calling it yourself is useful for learning how streams divide work, or for writing your own stream sources.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe. spliterator() was added in Java 8, together with streams.

Syntax

Get a spliterator, then advance, split, or drain it.

Java
Spliterator<String> sp = list.spliterator();
sp.tryAdvance(s -> System.out.println(s));       // first element
Spliterator<String> part = sp.trySplit();        // split off part of what is left
sp.forEachRemaining(System.out::println);        // the rest of this part
CallWhat it does
sp.tryAdvance(action)Processes one element; returns false when none remain.
sp.forEachRemaining(action)Processes every remaining element.
sp.trySplit()Hands part of the elements to a new spliterator, or returns null.
sp.estimateSize()Number of elements left (exact for an ArrayList).

ArrayList's spliterator is late-binding: it reads the list's size when you first use it, not when you create it. It is also fail-fast: structural changes made during traversal cause ConcurrentModificationException.

Library declarations
Java
// java.util.ArrayList<E>
public Spliterator<E> spliterator()

// java.util.Spliterator<T>
boolean tryAdvance(Consumer<? super T> action)
void forEachRemaining(Consumer<? super T> action)
Spliterator<T> trySplit()
long estimateSize()
int characteristics()

Parameters

ParameterDescription
Nonespliterator() takes no parameters.

Return Value

Return ValueDescription
Spliterator<E>A new spliterator that reports the SIZED, SUBSIZED, and ORDERED characteristics.

Compare with Similar Methods

Other ways to traverse the list:

MethodHow it differs
iterator()One element at a time; cannot split.
forEach(action)Visits everything in one call.
stream()Builds a stream on top of this spliterator; the usual way to use it.
subList(a, b)A view of a range, if you just need part of the list.

Java Examples

Example 1 — tryAdvance() and forEachRemaining()

Process one task, check how many are left, then process the rest.

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

public class ArrayListSpliteratorDemo {
    public static void main(String[] args) {
        List<String> tasks = new ArrayList<>(Arrays.asList("plan", "code", "test", "ship"));
        Spliterator<String> sp = tasks.spliterator();

        System.out.println("size estimate: " + sp.estimateSize());
        sp.tryAdvance(t -> System.out.println("first: " + t));
        System.out.println("left: " + sp.estimateSize());
        sp.forEachRemaining(t -> System.out.println("then: " + t));
        System.out.println("more? " + sp.tryAdvance(t -> System.out.println(t)));
    }
}

How It Works

  1. Exact size. For an ArrayList, estimateSize() is the real number of elements still to visit: 4.
  2. One element. tryAdvance runs the lambda for "plan" only and moves forward.
  3. The rest. forEachRemaining processes code, test, and ship in order.
  4. Exhausted. Nothing is left, so a final tryAdvance does not call its lambda and returns false.

Example 2 — Split the Work in Two

Split a six-element spliterator and drain each part separately.

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

public class ArrayListSpliteratorSplit {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));
        Spliterator<Integer> second = nums.spliterator();
        Spliterator<Integer> first = second.trySplit();

        System.out.println("first part size: " + first.estimateSize());
        System.out.println("second part size: " + second.estimateSize());

        StringBuilder a = new StringBuilder();
        StringBuilder b = new StringBuilder();
        first.forEachRemaining(n -> a.append(n).append(' '));
        second.forEachRemaining(n -> b.append(n).append(' '));
        System.out.println("first: " + a.toString().trim());
        System.out.println("second: " + b.toString().trim());
    }
}

How It Works

  1. Split in place. trySplit() gives part of the remaining elements to a new spliterator and keeps the rest for itself.
  2. Halves. ArrayList splits at the middle of the remaining range, so each part holds 3 of the 6 numbers.
  3. Prefix goes out. The returned spliterator covers the first half (1 to 3) and the original keeps 4 to 6, so encounter order is preserved.
  4. How parallel streams work. A parallel stream splits like this again and again and hands the pieces to different threads.

Example 3 — Characteristics and Late Binding

Create the spliterator on an empty list, add elements, then inspect it.

Java
import java.util.ArrayList;
import java.util.List;
import java.util.Spliterator;

public class ArrayListSpliteratorTraits {
    public static void main(String[] args) {
        List<String> words = new ArrayList<>();
        Spliterator<String> sp = words.spliterator();

        words.add("late");
        words.add("binding");

        System.out.println("ORDERED: " + sp.hasCharacteristics(Spliterator.ORDERED));
        System.out.println("SIZED: " + sp.hasCharacteristics(Spliterator.SIZED));
        System.out.println("SORTED: " + sp.hasCharacteristics(Spliterator.SORTED));
        System.out.println("sees " + sp.estimateSize() + " elements added after creation");
    }
}

How It Works

  1. Created early. The spliterator is made while the list is still empty.
  2. Elements added later. Because ArrayList's spliterator is late-binding, it does not fix its range until it is first used, so the two adds are allowed.
  3. Trait flags. hasCharacteristics shows that it promises encounter order (ORDERED) and an exact size (SIZED), but not sorted order.
  4. First use. estimateSize() binds the spliterator to the list as it is now, so it reports 2.

Frequently Asked Questions

A splittable iterator, added in Java 8. It can walk elements one by one or in bulk, and split itself so that parallel streams can share the work.
Usually not. list.stream() and list.parallelStream() call it for you.
The spliterator reads the list size when it is first used, not when it is created, so changes made in between are seen.
SIZED, SUBSIZED, and ORDERED.
No. Once it has visited its elements it is exhausted. Call spliterator() again for a new pass.

Next: subList()

Get a live view of a range of indexes.

subList() 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