HashSet spliterator() method in Java

The spliterator() method of HashSet returns a Spliterator with the same elements as HashSet. The returned Spliterator is late-binding and fail-fast Spliterator. A late-binding Spliterator binds to the source of elements means HashSet at the point of first traversal, first split, or first query for estimated size, rather than at the time the Spliterator is created. It can be used with Streams in Java 8. Also it can traverse elements individually and in bulk too. Spliterator is better way to traverse over element because it provides more control on elements.

Syntax:

public Spliterator<E> spliterator()

Returns: This method returns a Spliterator over the elements in HashSet.



Below programs illustrate spliterator() method of HashSet:

Example 1: To demonstrate spliterator() method on HashSet which contains a set of Numbers.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of HashSet
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an HashSet which going to
        // contains a list of Numbers
        HashSet<Integer> Numbers = new HashSet<Integer>();
  
        // Add Number to list
        Numbers.add(23);
        Numbers.add(32);
        Numbers.add(45);
        Numbers.add(63);
  
        // using spliterator() method
        Spliterator<Integer> numbers = Numbers.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Numbers:");
  
        // forEachRemaining method of Spliterator
        numbers.forEachRemaining((n) -> System.out.println(n));
    }
}

chevron_right


Output:

list of Numbers:
32
23
45
63

Example 2: To demonstrate spliterator() method on HashSet which contains set of Students Names.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of HashSet
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an HashSet which going to
        // contains a list of string values
        HashSet<String> students = new HashSet<String>();
  
        // Add Strings to list
        students.add("Ram");
        students.add("Mohan");
        students.add("Sohan");
        students.add("Rabi");
  
        // using spliterator() method
        Spliterator<String> names = students.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Students:");
  
        // forEachRemaining method of Spliterator
        names.forEachRemaining(
            (n) -> System.out.println("Student Name: " + n));
    }
}

chevron_right


Output:

list of Students:
Student Name: Rabi
Student Name: Mohan
Student Name: Sohan
Student Name: Ram

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html#spliterator–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.