LinkedList spliterator() Method in Java
Last Updated :
11 Jul, 2025
In Java, the spliterator() method of the LinkedList class returns a Spliterator that helps iterate over an element of the linked list. A Spliterator is a special type of iterator used to process elements in parallel processing of elements when used with conjunction in parallel streams.
Example 1: The below Java program demonstrates the basic traversal of a LinkedList using the spliterator() method.
Java
// Java Program to demonstrate
// spliterator() method of LinkedList
import java.util.*;
public class Geeks {
public static void main(String[] args)
{
// Create a LinkedList
LinkedList<String> l = new LinkedList<>();
l.add("Geeks");
l.add("For");
l.add("Geeks");
// Get the Spliterator for the LinkedList
Spliterator<String> it = l.spliterator();
// Use the Spliterator to
// traverse the LinkedList
System.out.println("Splitting the list:");
it.forEachRemaining(System.out::println);
}
}
OutputSplitting the list:
Geeks
For
Geeks
Syntax of LinkedList spliterator() Method
public Spliterator<E> spliterator()
Return Type: This method returns a Spliterator of type E, which can be used to traverse or split the elements of the linked list.
The spliterator interface has two main methods which are listed below:
1. trySplit()
It splits the elements into two groups one group returned a new spliterator while the original spliterator keep the other group.
Syntax of trySplit():
public Spliterator<T> trySplit()
Return Type: A new Spliterator covering some portion of the elements, or null if splitting is not possible.
2. tryAdvance()
It processes the next element if present and advances the Spliterator
Syntax of tryAdvance():
public boolean tryAdvance(Consumer<? super T> action)
- Parameter: A functional interface that defines the action to perform on the current element.
- Return Type: Returns true if there was an element to process, otherwise false.
Example 2: The below Java program demonstrate how to use the trySplit() to divide a spliterator into two parts and process each part separately.
Java
// Java Program to demonstrate the use of trySplit()
import java.util.*;
public class Geeks {
public static void main(String[] args) {
// Create a list
List<String> l = Arrays.asList("A", "B", "C", "D", "E");
// Get the Spliterator
Spliterator<String> s = l.spliterator();
// Split the Spliterator
Spliterator<String> n = s.trySplit();
// Process the first part
if (n != null) {
System.out.println("First Spliterator:");
n.forEachRemaining(System.out::println);
}
// Process the second part
System.out.println("Second Spliterator:");
s.forEachRemaining(System.out::println);
}
}
OutputFirst Spliterator:
A
B
Second Spliterator:
C
D
E
Example 3: The below Java Program demonstrate the spliterator() method on a LinkedList of type String which contains a list of Movie Names.
Java
// Java Program Demonstrate spliterator()
// which contains list of movie name
import java.util.*;
public class Geeks {
public static void main(String[] args) {
// create a list of Movie
// names of string values
LinkedList<String> l = new LinkedList<String>();
// Add Strings to list
// each string represents city name
l.add("Delhi 6");
l.add("3 Idiots");
l.add("Stree");
l.add("Airlift");
// using spliterator() method
Spliterator<String> s = l.spliterator();
// print result from Spliterator
System.out.println("List of Movies:");
// forEachRemaining method of Spliterator
s.forEachRemaining(
(n) -> System.out.println("Movie Name: " + n));
}
}
OutputList of Movies:
Movie Name: Delhi 6
Movie Name: 3 Idiots
Movie Name: Stree
Movie Name: Airlift
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java