Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

What is a Spliterator and what do tryAdvance, trySplit, estimateSize and characteristics do? Why does a good trySplit matter?

A Spliterator is the object a stream uses to walk through its source and, for parallel streams, to cut the source into parts. tryAdvance hands over one element if there is one, forEachRemaining hands over the rest, trySplit gives part of the elements to a new Spliterator so another thread can process them, estimateSize says how many elements remain, and characteristics such as SIZED, ORDERED, SORTED and IMMUTABLE tell the stream which shortcuts are safe. A good trySplit matters because parallel speed depends entirely on splitting the work into cheap, balanced pieces.

Why this question is asked

Most developers never write a Spliterator, but every stream they use runs on one. Knowing how it works explains why ArrayList parallelises well and LinkedList does not, why count() sometimes skips our lambdas, and how to make a custom data source stream-friendly. Interviewers ask about it at the senior level because it connects the stream API to the machinery underneath. In this answer, we will recap what a Spliterator is, try each of its methods on a real list, look at characteristics and the optimisations they enable, write a small Spliterator of our own, and see what a poor trySplit does to a parallel stream.

1A Quick Recap: What a Spliterator Is

The name joins two words: split and iterator. Like an Iterator, a Spliterator walks through elements one at a time. Unlike an Iterator, it can also split itself, handing a part of its remaining elements to a new Spliterator. That second ability is what lets a parallel stream divide its source between threads.

Every collection has a spliterator() method, and every stream is built on one. When we call list.stream(), the stream wraps list.spliterator(); the operations we add, such as filter and map, are then fed elements by that Spliterator when a terminal operation runs.

2Trying Each Method on an ArrayList

We can call the methods directly to see what they do.

Java
List<String> list = new ArrayList<>(List.of("a", "b", "c", "d", "e", "f"));
Spliterator<String> s = list.spliterator();
System.out.println(s.estimateSize()); // 6
s.tryAdvance(x -> System.out.println(x)); // a (returns true)
Spliterator<String> prefix = s.trySplit(); // takes the first half of what is left
System.out.println(prefix.estimateSize()); // 2 ("b", "c")
System.out.println(s.estimateSize()); // 3 ("d", "e", "f")
prefix.forEachRemaining(System.out::println); // b, c
System.out.println(prefix.tryAdvance(x -> {})); // false: nothing left
System.out.println(s.hasCharacteristics(Spliterator.SIZED)); // true
System.out.println(s.hasCharacteristics(Spliterator.ORDERED)); // true

tryAdvance(action) is the basic step. If an element remains, it passes it to the action and returns true; otherwise it returns false. That boolean is how a stream knows the source is exhausted, and because it handles one element at a time, the stream can check between elements whether a short-circuiting operation like findFirst wants to stop.

forEachRemaining(action) processes everything left in one call. Its default implementation simply calls tryAdvance in a loop, but sources override it with a tight loop, which is faster when the stream knows it needs every element.

trySplit() is the interesting one. ArrayList's version computes the middle of the remaining index range, returns a new Spliterator covering the first half, and keeps the second half for itself. Notice that the returned part is a prefix: for an ORDERED source, trySplit must give away the elements that come first, so the stream can stitch results back together in the right order. When a Spliterator is too small to split, or cannot split at all, trySplit returns null.

3EstimateSize and Characteristics

estimateSize() returns how many elements remain, exactly if the source knows, or an estimate if not, with Long.MAX_VALUE meaning unknown. The parallel framework uses it to decide when a piece is small enough to stop splitting: roughly the total size divided by four times the pool's parallelism.

characteristics() returns a set of flags packed into an int. Each flag is a promise about the source, and each promise unlocks an optimisation.

Characteristics and what they enable
FlagPromiseWhat the stream can do with it
SIZEDestimateSize is exacttoArray allocates once; count() can skip traversal
SUBSIZEDevery split is SIZED tooparallel collect writes into exact slots
ORDEREDelements have an encounter orderkeeps order in results; forbids reordering
SORTEDelements are already sortedsorted() with the same order can do nothing
DISTINCTno two elements are equaldistinct() can do nothing
NONNULLno element is nullskips null checks
IMMUTABLE / CONCURRENTsource cannot change, or is safe to changeno fail-fast checks needed
Characteristics are promises; a false promise gives wrong results, not just slow ones.

4Writing a Small Spliterator

Here is a Spliterator over part of an array, written the way ArrayList's is. It shows what each method must do.

Java
final class SliceSpliterator<T> implements Spliterator<T> {
private final T[] items;
private int from; // next element to hand out
private final int to; // one past the last element
SliceSpliterator(T[] items, int from, int to) { this.items = items; this.from = from; this.to = to; }
public boolean tryAdvance(Consumer<? super T> action) {
if (from >= to) return false;
action.accept(items[from++]);
return true;
}
public Spliterator<T> trySplit() {
int mid = (from + to) >>> 1;
if (mid <= from) return null; // one element left: do not split
Spliterator<T> prefix = new SliceSpliterator<>(items, from, mid);
from = mid; // we keep the second half
return prefix;
}
public long estimateSize() { return to - from; }
public int characteristics() { return ORDERED | SIZED | SUBSIZED | NONNULL | IMMUTABLE; }
}
String[] data = {"x", "y", "z", "w"};
long n = StreamSupport.stream(new SliceSpliterator<>(data, 0, 4), true).count(); // 4

Notice that trySplit costs almost nothing: one addition, one shift and one new object. It also splits evenly, so after three rounds of splitting, eight threads each hold an eighth of the array. That is exactly what makes arrays and ArrayList the best sources for parallel streams. NONNULL and IMMUTABLE here are promises about our use of the array; we would leave them out if callers might put nulls in it or change it while streaming.

5Why a Good trySplit Matters

A parallel stream gets all its parallelism from trySplit. If trySplit returns null, the whole source stays in one Spliterator and runs on one thread, with the overhead of the parallel machinery added on top. If it splits unevenly, for example by giving away one element at a time, the work tree becomes long and thin: one thread holds almost everything while the others finish their tiny pieces and sit idle.

Sources that cannot jump to their middle do something in between. Spliterators.spliteratorUnknownSize(iterator, ...), used for iterator-based sources, and LinkedList both split by copying a batch of elements into an array, starting with about 1,024 elements and growing the batch each time. That works, but the splitting itself is sequential work done by one thread, and with an unknown size the framework cannot balance the pieces. So when we write a Spliterator for a source that could support it, such as an indexed structure, a proper index-based trySplit is the difference between a parallel stream that scales and one that is merely slower.

6Follow-up questions

What is the difference between SIZED and SUBSIZED?

SIZED means this Spliterator knows its exact size. SUBSIZED means every Spliterator produced by trySplit, and every one produced from those, is also SIZED. ArrayList is both. A HashSet's Spliterator is SIZED at the top but its splits are only estimates, because the entries are not evenly spread across bucket ranges, so it is not SUBSIZED.

Why does count() sometimes not run my map or peek lambdas?

Since Java 9, if the source is SIZED and no operation in the pipeline can change the number of elements, count() returns estimateSize() directly without traversing anything. A map or peek does not change the count, so their lambdas never run. A filter does, so with a filter in the pipeline the elements are traversed.

What does late-binding mean for a Spliterator?

A late-binding Spliterator looks at its source only when it is first traversed, split or asked for its size, not when it is created. So changes made to an ArrayList between calling stream() and the terminal operation are seen. After that point, ArrayList's Spliterator is fail-fast and throws ConcurrentModificationException if the list changes structurally during traversal.

7Conclusion

In this answer, we saw that a Spliterator both traverses and partitions a stream's source. tryAdvance hands over one element and reports whether one existed, forEachRemaining processes the rest in a tight loop, trySplit gives a prefix of the remaining elements to a new Spliterator for another thread, and estimateSize with characteristics such as SIZED, ORDERED, SORTED and IMMUTABLE let the stream skip work safely. A cheap, balanced trySplit is what makes a source scale in parallel; a missing or lopsided one leaves most of the work on a single thread.

What to say in the interviewAbout 90 seconds aloud

Open with

A Spliterator is what a stream uses to traverse its source, and for parallel streams, to partition it. tryAdvance takes an action, and if an element remains it passes that element to the action and returns true, otherwise false; the stream uses it element by element so it can stop early for things like findFirst. forEachRemaining processes everything left, usually in a tight loop. trySplit hands part of the remaining elements to a new Spliterator, and for an ordered source that part must be the prefix, so results can be combined in order; it returns null if it cannot or should not split. estimateSize says how many elements are left, exactly when the source is SIZED, or Long.MAX_VALUE if unknown, and the framework uses it to decide when to stop splitting. characteristics are flags like SIZED, SUBSIZED, ORDERED, SORTED, DISTINCT, NONNULL and IMMUTABLE, and they enable shortcuts: count can skip traversal for a sized source, sorted can be a no-op on a sorted one.

Then, if they want more

A good trySplit matters because it is the only source of parallelism. ArrayList splits by index into exact halves for free, so eight threads quickly get equal work. A Spliterator that never splits, or splits off tiny pieces, or a LinkedList that has to copy batches, leaves most of the work on one thread.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a Spliterator and what do tryAdvance, trySplit, estimateSize and characteristics...": https://scrivio-interview-library.netlify.app/java/streams/b46fddd931/