How to Iterate over the Elements of a PriorityQueue in Java? Last Updated : 27 Apr, 2024 Summarize Comments Improve Suggest changes Share 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. Comment More infoAdvertise with us Next Article How to Iterate over the Elements of a PriorityQueue in Java? G gaurav472 Follow Improve Article Tags : Java Java Programs java-priority-queue Java Examples Practice Tags : Java Similar Reads How to Handle Null Elements in a PriorityQueue in Java? In Java, a Priority Queue is an abstract data type similar to a regular queue or stack, but with a key difference like elements are dequeued based on their priority. Regarding the null value property, the PriorityQueue class does not allow null elements. Attempting to add a null element will result 3 min read How to Iterate over a Queue in Java? Queue is a concept of Linear Data Structure that follows the concept of FIFO(First In First Out). Queues are majorly used to maintain the order of the elements to which they are added. In this article, we will learn to perform Queue Iteration in Java. Queue Iteration Program in JavaSyntax with Examp 1 min read How to Copy Elements from One PriorityQueue to Another in Java? In Java, a priority queue is a data structure that allows the users to store data based on their priority. In this article, we will learn how to copy elements from one priority queue to another in Java. Example Input: PriorityQueue original ={1,2,3,4,5}Output: PriorityQueue Copy ={1,2,3,4,5}Copy ele 2 min read How to Remove the Top Element From a PriorityQueue in Java? A priority queue is a specialized type of queue, in which elements are given with priorities, and those with more priorities are served before elements with lower priorities. It's equivalent to a regular queue (first-in, first-out) but prioritizes urgency rather than arrival order. Remove the Top El 2 min read How to Customize the Ordering of Elements in a PriorityQueue Using a Comparator in Java? A PriorityQueue is a data structure that allows elements to be processed based on their priority. By default, it orders elements according to their natural ordering (e.g., numeric values in ascending order). However, sometimes we need a different sorting order based on specific criteria. In this art 4 min read Like