PriorityQueue spliterator() method in Java

The spliterator() method of PriorityQueue returns a Spliterator the same elements as PriorityQueue.The returned Spliterator is late-binding and fail-fast Spliterator. A late-binding Spliterator binds to the source of elements means PriorityQueue 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 PriorityQueue.



Below programs illustrate spliterator() method of PriorityQueue:

Example 1: To demonstrate spliterator() method on PriorityQueue.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of PriorityQueue
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an PriorityQueue
        PriorityQueue<String> queue = new PriorityQueue<String>();
  
        // Add String to queue
        queue.add("Kolkata");
        queue.add("Patna");
        queue.add("Delhi");
        queue.add("Jammu");
  
        // using spliterator() method
        Spliterator<String> spt = queue.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Strings:");
  
        // forEachRemaining method of Spliterator
        spt.forEachRemaining((n) -> System.out.println(n));
    }
}

chevron_right


Output:

list of Strings:
Delhi
Jammu
Kolkata
Patna

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of PriorityQueue
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an PriorityQueue which going to
        // contain list of names
        PriorityQueue<String> queue = new PriorityQueue<String>();
  
        // Add String object to queue
        queue.add("Aman");
        queue.add("Amar");
        queue.add("Sanjeet");
        queue.add("Josh");
        queue.add("Ron");
        queue.add("Kevin");
  
        // using spliterator() method
        Spliterator<String> spt = queue.spliterator();
  
        // print result from Spliterator
        System.out.println("list of String Object:");
  
        // forEachRemaining method of Spliterator
        spt.forEachRemaining((n) -> print(n));
    }
  
    public static void print(String s)
    {
        System.out.println("Student Name: " + s);
    }
}

chevron_right


Output:

list of String Object:
Student Name: Aman
Student Name: Amar
Student Name: Kevin
Student Name: Josh
Student Name: Ron
Student Name: Sanjeet

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/PriorityQueue.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.