Home Java LinkedList Methods spliterator() Java LinkedList spliterator() Method Definition In Java, spliterator() returns a Spliterator, an object that can traverse the elements of the LinkedList and also split off part of them for parallel work. It is the engine behind list.stream().
Beginners rarely need it directly. The useful methods are tryAdvance(action) to visit one element, forEachRemaining(action) to visit the rest, estimateSize() for the number of elements left, and characteristics() for facts such as "ordered" and "sized".
An instance method of java.util.LinkedList (since Java 8): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Collection, List and Iterable, and LinkedList implements it. spliterator() was added in Java 8.
Syntax Get the spliterator, then ask it to visit elements.
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B", "C"));
Spliterator<String> sp = list.spliterator();
sp.tryAdvance(s -> System.out.println(s)); // A
sp.forEachRemaining(s -> System.out.println(s)); // B, C Call What it does sp.tryAdvance(action)Runs the action for one element. Returns false when none is left. sp.forEachRemaining(action)Runs the action for all remaining elements. sp.estimateSize()The number of elements left to visit. sp.characteristics()Bit flags such as ORDERED and SIZED.
For a LinkedList, the spliterator reports ORDERED and SIZED. It is late-binding : it only reads the list when the traversal starts.
Library declarations // java.util.LinkedList
public Spliterator<E> spliterator() Parameters Parameter Description None spliterator() takes no parameters.
Return Value Return Value Description Spliterator<E>A spliterator over the elements, in order.
Important
This method needs Java 8 or later. A LinkedList is expensive to split, so it does not suit parallel streams as well as an ArrayList does. The spliterator is fail-fast: changing the list after traversal has started leads to a ConcurrentModificationException. For everyday work, use list.stream() or a for-each loop.
Compare with Similar Methods Ways to traverse a list:
Java Examples 1. One by one 2. Size and characteristics 3. The link to streams Example 1 — Visit Elements with tryAdvance and forEachRemaining Take two elements one at a time, then visit the rest in bulk.
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Spliterator;
public class LinkedListSpliteratorDemo {
public static void main(String[] args) {
LinkedList<String> letters = new LinkedList<>(Arrays.asList("a", "b", "c", "d", "e"));
Spliterator<String> sp = letters.spliterator();
sp.tryAdvance(s -> System.out.println("first: " + s));
sp.tryAdvance(s -> System.out.println("second: " + s));
sp.forEachRemaining(s -> System.out.println("rest: " + s));
boolean more = sp.tryAdvance(s -> System.out.println("never printed"));
System.out.println("more left: " + more);
}
} first: a
second: b
rest: c
rest: d
rest: e
more left: false How It Works A spliterator over five letters. It has not read anything yet.tryAdvance takes one. The first two calls visit "a" and "b" and each returns true.forEachRemaining takes the rest. It visits c, d and e in order.Then nothing is left. The last tryAdvance finds no element, so it returns false and never runs its action.Example 2 — Ask a Spliterator About Its Size and Traits Print the estimated size and check which characteristics a LinkedList spliterator reports.
import java.util.LinkedList;
import java.util.Spliterator;
public class LinkedListSpliteratorFacts {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
for (int i = 1; i <= 6; i++) {
list.add(i);
}
Spliterator<Integer> sp = list.spliterator();
System.out.println("estimateSize: " + sp.estimateSize());
System.out.println("exact size: " + sp.getExactSizeIfKnown());
System.out.println("ORDERED: " + sp.hasCharacteristics(Spliterator.ORDERED));
System.out.println("SIZED: " + sp.hasCharacteristics(Spliterator.SIZED));
System.out.println("DISTINCT: " + sp.hasCharacteristics(Spliterator.DISTINCT));
sp.tryAdvance(x -> { });
System.out.println("after one: " + sp.estimateSize());
}
} estimateSize: 6
exact size: 6
ORDERED: true
SIZED: true
DISTINCT: false
after one: 5 How It Works Six elements. The list holds 1 to 6.Size information. A LinkedList knows its size, so both the estimate and the exact size are 6.Characteristics. The list is ORDERED (it has a fixed sequence) and SIZED, but not DISTINCT, because a list may hold duplicates.The estimate shrinks. After visiting one element, 5 are left.Example 3 — Turn a Spliterator into a Stream Build a stream from the spliterator with StreamSupport and compare it with list.stream().
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Spliterator;
import java.util.stream.StreamSupport;
public class LinkedListSpliteratorStream {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(3, 8, 5, 12));
Spliterator<Integer> sp = list.spliterator();
long big = StreamSupport.stream(sp, false).filter(n -> n > 4).count();
System.out.println("numbers above 4: " + big);
long sameWay = list.stream().filter(n -> n > 4).count();
System.out.println("with list.stream(): " + sameWay);
}
} numbers above 4: 3
with list.stream(): 3 How It Works A stream needs a spliterator. StreamSupport.stream(sp, false) wraps it in a sequential stream; the false means "not parallel".Filter and count. The numbers 8, 5 and 12 are above 4, so the count is 3.list.stream() does the same. It creates the spliterator for you, so the result is again 3.That is the purpose. Collections hand their spliterator to the stream library, and you almost never write the lines above yourself.Frequently Asked Questions What does LinkedList spliterator() do? It returns a Spliterator, an object that traverses the elements one by one or in bulk and can split work into parts. Streams use it internally.
Which Java version added spliterator()? Java 8, together with the Stream API.
When should I use spliterator() directly? Rarely. Use a for-each loop, an iterator, or list.stream() for everyday work. Use spliterator() only when you write low-level stream or parallel code.
What characteristics does a LinkedList spliterator report? ORDERED and SIZED. It does not report DISTINCT, SORTED or IMMUTABLE.
Is a LinkedList good for parallel streams? Not really. A LinkedList has to be walked to split it, so an ArrayList splits much more cheaply.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion