PriorityBlockingQueue spliterator() method in Java Last Updated : 26 Nov, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Java // 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)); } } 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. Java // 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)); } } Output: list of Names: Joydeep Priyanka Joyita Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#spliterator-- Comment More infoAdvertise with us Next Article PriorityBlockingQueue spliterator() method in Java A AmanSingh2210 Follow Improve Article Tags : Java Java-Collections Java - util package java-basics Java-Functions Java-PriorityBlockingQueue +2 More Practice Tags : JavaJava-Collections Similar Reads 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 2 min read PriorityBlockingQueue size() method in Java The size() method of PriorityBlockingQueue is used to find the present size of the queue. It returns the number of elements in the collection. If the collection contains more than Integer.MAX_VALUE elements, then this method returns Integer.MAX_VALUE. Syntax: public int size() Return Value: This met 2 min read PriorityBlockingQueue put() method in Java The put(E e) method of PriorityBlockingQueue is used to add an element into this queue. This method inserts the specified element into this priority queue. Since the queue is unbounded, this method will be never be blocked.Syntax: public void put(E e) Parameter: This method accepts a mandatory param 2 min read PriorityBlockingQueue take() method in Java The take() method of PriorityBlockingQueue returns head of the queue after removing it. If queue is empty, then this method will wait until an element becomes available. Syntax: public E take() throws InterruptedException Returns: This method returns value at the head of this PriorityBlockingQueue. 2 min read PriorityBlockingQueue toArray() method in Java toArray() The toArray method of PriorityBlockingQueue is used to create an array having the same elements as that of this PriorityBlockingQueue, in proper sequence. Actually, this method copies all the element from the PriorityBlockingQueue to a new array. This method behaves as a bridge between arr 4 min read Like