Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
Java Collection spliterator() with Examples
Next article icon

Java Collection spliterator() with Examples

Last Updated : 27 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The spliterator() in Java Collection creates a spliterator over the elements in the collection. In simple words, it's an iterator that allows you to traverse the elements individually but is specially designed to work in parallel processing scenarios. The difference between Iterator and Spliterator is iterator does not support parallel programming but spliterator supports parallel programming.

Syntax

public Spliterator<E> spliterator();

Here E denotes the type of element stored inside the collection.

Return Value

It returns a spliterator across elements in the collection.

Example 1

In this example, we have used some of the methods of spliterator that we need to understand before we proceed to the example.

  1. spliterator(): It returns a spliterator across the element of the calling collection.
  2. tryAdvance(): It returns a boolean value. It returns true, if there are more elements exist else it returns false.
Java
import java.util.List; import java.util.ArrayList; import java.util.Spliterator; import java.io.*;  class GFG {     public static void main (String[] args) {                          List<String> list1 = new ArrayList<String>();             list1.add("Pen");             list1.add("Paper");             list1.add("Rubber");             list1.add("Pencil");              Spliterator<String> spliterator = list1.spliterator();              System.out.println("The list contains:");             while(spliterator.tryAdvance((element)->System.out.print(element+" ")));       } } 

Output:

The list contains:
Pen Paper Rubber Pencil

Example 2

In this example, we have used spliterator() in Hashset Collection.The same way as above example it traverses through the set and prints the element.

Java
import java.util.HashSet; import java.util.Spliterator; import java.io.*;  class GFG {     public static void main (String[] args) {                  HashSet<Integer> list1 = new HashSet<Integer>();         list1.add(1);         list1.add(2);         list1.add(3);         list1.add(4);         list1.add(5);         list1.add(6);          Spliterator<Integer> spliterator1 = list1.spliterator();          System.out.println("The collection contains :");         while(spliterator1.tryAdvance((element)->System.out.print(element+" ")));      } } 

Ouput:

The collection contains :
1 2 3 4 5 6

Next Article
Java Collection spliterator() with Examples

S

saswatdas121
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java-Collections-Class
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    Collectors toSet() in Java with Examples
    Collectors toSet() returns a Collector that accumulates the input elements into a new Set. There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned. This is an unordered Collector i.e, the collection operation does not commit to preserving the encounter
    2 min read
    AbstractCollection in Java with Examples
    The AbstractCollection class in Java is a part of the Java Collection Framework and implements the Collection interface. It is used to implement an unmodifiable collection, for which one needs to only extend this AbstractCollection Class and implement only the iterator and the size methods. Class Hi
    3 min read
    Collectors toList() method in Java with Examples
    The toList() method of Collectors Class is a static (class) method. It returns a Collector Interface that gathers the input data onto a new list. This method never guarantees type, mutability, serializability, or thread-safety of the returned list but for more control toCollection(Supplier) method c
    2 min read
    AbsractCollection iterator() Method in Java with Examples
    The iterator() method of Java AbstractCollection is used to return an iterator of the same elements as that of the Collection. The elements are returned in random order from what was present in the Collection. Syntax: Iterator iterate_value = AbstractCollection.iterator(); Parameters: The function d
    2 min read
    Java Swing | JSeparator with examples
    JSeparator is a part of Java Swing framework. It is used to create a dividing line between two components. More specifically, it is mainly used to create dividing lines between menu items in a JMenu. In JMenu or JPopupMenu addSeparartor function can also be used to create a separator. Constructor of
    4 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