Quick revision
The short answer
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.
What to remember
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.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
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.
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.
| Flag | Promise | What the stream can do with it |
|---|---|---|
| SIZED | estimateSize is exact | toArray allocates once; count() can skip traversal |
| SUBSIZED | every split is SIZED too | parallel collect writes into exact slots |
| ORDERED | elements have an encounter order | keeps order in results; forbids reordering |
| SORTED | elements are already sorted | sorted() with the same order can do nothing |
| DISTINCT | no two elements are equal | distinct() can do nothing |
| NONNULL | no element is null | skips null checks |
| IMMUTABLE / CONCURRENT | source cannot change, or is safe to change | no fail-fast checks needed |
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.
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.
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.
- Official documentation
- java.util.stream, package documentation, java.util.function, package documentation
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/