Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
LinkedList toArray() Method in Java
Next article icon

LinkedList spliterator() Method in Java

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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);     } } 

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.

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);     } } 

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. 

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));     } } 

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


Next Article
LinkedList toArray() Method in Java

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-Collections
  • java-LinkedList
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • LinkedList indexOf() Method in Java
    In Java, the indexOf() method of the LinkedList class is used to find the index of the first occurrence of the specified element in the list. Syntax of LinkedList indexOf() Method public int indexOf(Object o); Parameter: This method takes an object "o" as an argument. Return type: It returns the ind
    2 min read
  • LinkedList lastIndexOf() Method in Java
    In Java, the lastIndexOf() method of LinkedList is used to find the last occurrence of the specified element in the list. If the element is present it will return the last occurrence of the element, otherwise, it will return -1. Syntax of LinkedList lastIndexOf() Method public int lastIndexOf(Object
    2 min read
  • LinkedList listIterator() Method in Java
    In Java, the listIterator() method of the LinkedList class returns a ListIterator that allows us to iterate over the elements of the list. Example: [GFGTABS] Java // Java Program to Demonstrate the // use of listIterator() in LinkedList import java.util.LinkedList; import java.util.ListIterator; pub
    3 min read
  • Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
    In Java, the LinkedList class is present in java.util package, and it also has different methods that do the work of flexible addition of elements and help addition both at the front and back of the list. In this article, we cover the LinkedList offer(), offerFirst(), and offerLast() methods. These
    5 min read
  • Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
    Linked list class offers the functionality to "look into" the first and last elements of the list and hence can be useful in cases where only the retrieval is required and not necessarily the deletion is required. Three functionalities are present and all are discussed in this article. 1. peek() : T
    4 min read
  • Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
    Java's Linked list class offers a function that allows a "Queue Based" working called poll(). This function not only returns deletes the first element, but also displays them while being deleted and hence can have a lot of usage in daily life problems and competitive programming as well. There are 3
    4 min read
  • LinkedList pop() Method in Java
    In Java, the pop() method of the LinkedList class is used to remove and return the top element from the stack represented by the LinkedList. The method simply pops out an element present at the top of the stack. This method is similar to the removeFirst method in LinkedList. Example 1: Here, we use
    2 min read
  • LinkedList push() Method in Java
    In Java, the push() method of the LinkedList class is used to push an element at the starting(top) of the stack represented by LinkedList. This is similar to the addFirst() method of LinkedList. This method simply inserts the element at the first position or top of the LinkedList. Syntax of LinkedLi
    1 min read
  • LinkedList removeFirst() Method in Java
    In Java, the removeFirst() method of the LinkedList class is used to remove and return the first element of the list. Example 1: Here, we use the removeFirst() method to remove the first element (head) of the LinkedList of Integers. [GFGTABS] Java // Java Program to demonstrate the // use of removeF
    2 min read
  • LinkedList removeFirstOccurrence() Method in Java
    In Java, the removeFirstOccurrence() method of the LinkedList class is used to remove the first occurrence of the specified element from the list. If there is no occurrence of the specified element the list remains unchanged. Example 1: Here, we use the removeFirstOccurence() method to remove the fi
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences