![]() |
VOOZH | about |
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.
Splitting the list: Geeks For Geeks
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:
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.
It processes the next element if present and advances the Spliterator
Syntax of tryAdvance():
public boolean tryAdvance(Consumer<? super T> action)
Example 2: The below Java program demonstrate how to use the trySplit() to divide a spliterator into two parts and process each part separately.
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.
List of Movies: Movie Name: Delhi 6 Movie Name: 3 Idiots Movie Name: Stree Movie Name: Airlift