PriorityBlockingQueue spliterator() method in Java

The spliterator() method of PriorityBlockingQueue returns a Spliterator of the same elements as PriorityBlockingQueue. The returned iterator is weakly consistent. 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 spliterator()

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

Below programs illustrate spliterator() method of PriorityBlockingQueue:

Example 1: Program to demonstrate spliterator() method on PriorityBlockingQueue which contains a list of numbers.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of PriorityBlockingQueue
  
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
  
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue<Integer> PrioQueue
            = new PriorityBlockingQueue<Integer>(capacityOfQueue);
  
        // Add numbers to PriorityBlockingQueue
        PrioQueue.put(7855642);
        PrioQueue.put(35658786);
        PrioQueue.put(5278367);
        PrioQueue.put(74381793);
        PrioQueue.put(76487590);
        PrioQueue.put(87625142);
  
        // create Spliterator of PrioQueue
        // using spliterator() method
        Spliterator<Integer> numbers = PrioQueue.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:
5278367
35658786
7855642
74381793
76487590
87625142

Example 2: Program to demonstrate spliterator() method on PriorityBlockingQueue which contains a list of names.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of PriorityBlockingQueue
  
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacityOfQueue = 5;
  
        // create object of PriorityBlockingQueue which contains
        // name of students
        PriorityBlockingQueue<String> names
            = new PriorityBlockingQueue<String>(capacityOfQueue);
  
        // Add names of students of girls college
        names.add("Joyita");
        names.add("Priyanka");
        names.add("Joydeep");
  
        // create Spliterator of PrioQueue
        // using spliterator() method
        Spliterator<String> list = names.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Names:");
  
        // forEachRemaining method  of Spliterator
        list.forEachRemaining((n) -> System.out.println(n));
    }
}

chevron_right


Output:

list of Names:
Joydeep
Priyanka
Joyita

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