VOOZH about

URL: https://www.geeksforgeeks.org/java/linkedlist-spliterator-method-in-java/

⇱ LinkedList spliterator() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.


Output
Splitting 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.


Output
First 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. 


Output
List of Movies:
Movie Name: Delhi 6
Movie Name: 3 Idiots
Movie Name: Stree
Movie Name: Airlift
Comment