How to Iterate over the Elements of a PriorityQueue in Java? Last Updated : 06 Aug, 2025 Comments Improve Suggest changes Like Article Like Report In Java, a Priority Queue is a Data structure that allows the users to store data based on their priority so that the elements with the highest priority can be accessed in constant time. In this article, we will learn how to iterate over the elements of a PriorityQueue in Java. Example Input: PriorityQueue pq ={1,2,3,4,5}Output: Elements of pq: 1 2 3 4 5 Iterate Over the Elements of a PriorityQueueTo iterate over the elements of a PriorityQueue in Java, we can create an iterator and then traverse and access each element present in the PriorityQueue with the help of the hasNext function. Let us understand this with the help of the below program: Java Program to Iterate Over the Elements of a PriorityQueueThe following program demonstrates how we can iterate over the elements of a PriorityQueue in Java: Java // Java Program to Iterate over the // Elements of a PriorityQueue import java.util.PriorityQueue; import java.util.Iterator; // Driver Class public class PriorityQueueIteration { // main function public static void main(String[] args) { // Create a PriorityQueue of Integer PriorityQueue<Integer> pq = new PriorityQueue<>(); // Add some elements to the PriorityQueue pq.add(1); pq.add(2); pq.add(3); pq.add(4); pq.add(5); // Create an iterator to iterate over // the elements of the PriorityQueue Iterator<Integer> it = pq.iterator(); // Iterate over the elements using the iterator System.out.println("Elements of the PriorityQueue pq:"); while (it.hasNext()) { System.out.print(it.next() + " "); } } } OutputElements of the PriorityQueue pq: 1 2 3 4 5 Complexity of the above Program:Time Complexity: O(N), where N is the size of the PriorityQueueAuxiliary Space: O(1). Explanation of the above Program:We declared a PriorityQueue pq of interger type and added some elements in it.We initialized an iterator it for the PriorityQueue to iterate over it's elements.With the help of hasNext function we moved the iterator it over the elements of the PriorityQueue pq and printed them. Create Quiz Comment G gaurav472 Follow 0 Improve G gaurav472 Follow 0 Improve Article Tags : Java Java Programs java-priority-queue Java Examples Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like