![]() |
VOOZH | about |
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}
To copy elements from one Priority Queue to another in Java, we can use the addAll Method. Following is the syntax to use the addAll method in Java:
destinationPq.addAll(sourcePq);
The following program illustrates how we can copy elements from one priority queue to another in Java:
Original Priority Queue: [1, 2, 3, 4, 5] Copied Priority Queue: [1, 2, 3, 4, 5]
Time Complexity: O(N), where N is the length of the priority queue.
Auxiliary Space: O(N)