PriorityBlockingQueue iterator() method in Java Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The iterator() method of PriorityBlockingQueue class Returns an iterator over the elements in this queue. The elements returned from this method do not follow any order. The returned iterator is weakly consistent. Syntax: public Iterator iterator() Parameter: This method does not take any parameter. Returns: This method returns an iterator having same elements as present in PriorityBlockingQueue. Below program illustrate iterator() method of PriorityBlockingQueue. Example 1: Java // Java Program Demonstrate iterator() // 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 elements to PriorityBlockingQueue PrioQueue.add(945645); PrioQueue.add(6468516); PrioQueue.add(7564165); PrioQueue.add(45616); // print PrioQueue System.out.println("PrioQueue: " + PrioQueue); // Call iterator() method of PriorityBlockingQueue Iterator iteratorVals = PrioQueue.iterator(); // Print elements of iterator // created from PriorityBlockingQueue System.out.println("The iterator values" + " of PriorityBlockingQueue are:"); while (iteratorVals.hasNext()) { System.out.println(iteratorVals.next()); } } } Output: PrioQueue: [45616, 945645, 7564165, 6468516] The iterator values of PriorityBlockingQueue are: 45616 945645 7564165 6468516 Example 2: To illustrate iterator() method of PriorityBlockingQueue which contains list of names. Java // Java Program Demonstrate iterator() // 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<String> names = new PriorityBlockingQueue<String>(capacityOfQueue); // Add names of students of girls college names.add("Geeks"); names.add("forGeeks"); names.add("A"); names.add("Computer"); names.add("Portal"); // Call iterator() method of PriorityBlockingQueue Iterator iteratorVals = names.iterator(); // Print elements of iterator // created from PriorityBlockingQueue System.out.println("The Names are:"); while (iteratorVals.hasNext()) { System.out.println(iteratorVals.next()); } } } Output: The Names are: A Computer Geeks forGeeks Portal Related Article : Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#iterator-- Comment More infoAdvertise with us Next Article PriorityBlockingQueue clear() method in Java A AmanSingh2210 Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-Iterator Java-PriorityBlockingQueue +3 More Practice Tags : JavaJava-CollectionsMisc Similar Reads PriorityQueue iterator() Method in Java The Java.util.PriorityQueue.iterator() method is used to return an iterator of the same elements as the Priority Queue. The elements are returned in random order from what present in the Queue. Syntax: Iterator iterate_value = Priority_Queue.iterator(); Parameters: The function does not take any par 2 min read PriorityBlockingQueue comparator() method in Java The comparator() method of PriorityBlockingQueue returns the comparator that can be used to order the elements in a PriorityBlockingQueue. The method returns null value if the queue follows the natural ordering pattern of the elements. Syntax: public Comparator<? super E> comparator() Returns: 2 min read PriorityBlockingQueue add() Method in Java The add(E e) method of PriorityBlockingQueue inserts the element passed as a parameter to the method at the tail of this PriorityBlockingQueue. This method returns true if the adding of the element is successful. Else it returns false. Syntax: public boolean add(E e) Parameter: This method takes a m 2 min read PriorityBlockingQueue drainTo() method in Java The drainTo(Collection col) method of PriorityBlockingQueue removes all available elements from this LinkedBlocking Queue and adds them to the given collection passed as a parameter. drainTo(Collection<? super E> col) The drainTo(Collection<? super E> col) method of PriorityBlockingQueue 5 min read PriorityBlockingQueue clear() method in Java The clear() method of PriorityBlockingQueue removes all the elements from this queue. Therefore this method can be applied when it is required to clear the PriorityBlockingQueue. Syntax: public void clear() Parameter: This method takes no parameters. Returns: This method returns nothing. Exception: 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 Like