
Introduction
While stacks have a last-in-first-out (LIFO mechanism) and queues have a first-in-first-out mechanism (FIFO), priority queues are somewhat different. Just by the name itself, a priority queue is just a queue — with the added extension of associating a “priority” to each item in the queue. Just like queues, priority queues also have a FIFO mechanism. But what does “priority” mean in this case? What does it mean to assign a “priority” to an item in the queue?
Generally, the element’s value is considered for assigning the priority, i.e., its magnitude. For example, the element whose magnitude is highest is considered to be the element with the highest priority. However, in other cases, we can also mark an element with the lowest value as the element with the highest priority. Priorities can also be set by our needs.

Thus we can classify them into two categories.
- Min Priority Queue: Minimum element has the highest priority.
- Max Priority Queue: Maximum element has the highest priority.
Now, this sounds very interesting but you must be thinking about how to implement it, the answer is a heap data structure. Heaps? What are they?
A heap is a data structure based on trees where the root of the tree contains the element whose priority is the highest or the lowest. A heap is a complete binary tree, Another new word? Well, in a complete binary tree every node has two child nodes except the leaf nodes. This means all the leaf nodes should be at the same level and all other internal nodes should contain two child nodes each.

In the first image, all levels are filled and have two children except the leaf nodes, whereas if we look at the second image, some nodes don't have two children (node with value 3).
Also Read, Prefix Sum Array
Declaration of Priority Queue using Java
Now let’s talk about the declaration of priority queue using Java. We can use different constructors to declare our priority queue.
1. PriorityQueue(): Creates a PriorityQueue with the initial default capacity (11) that orders its elements according to their natural ordering.
PriorityQueue<E> pq = new PriorityQueue<E>(); |
2. PriorityQueue(Collection<E> c): Creates a PriorityQueue containing the elements in the specified collection.
PriorityQueue<E> pq = new PriorityQueue<E>(Collection<E> c); |
3. PriorityQueue(int initialCapacity): Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.
PriorityQueue<E> pq = new PriorityQueue<E>(int initialCapacity); |
4. PriorityQueue(int initialCapacity, Comparator<E> comparator): Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
PriorityQueue<E> pq = new PriorityQueue(int initialCapacity, Comparator<E> comparator); |
5. PriorityQueue(PriorityQueue<E> c): Creates a PriorityQueue containing the elements in the specified priority queue.
PriorityQueue<E> pq = new PriorityQueue(PriorityQueue<E> c); |
6. PriorityQueue(SortedSet<E> c): Creates a PriorityQueue containing the elements in the specified sorted set.
PriorityQueue<E> pq = new PriorityQueue<E>(SortedSet<E> c); |
Here ‘E’ refers to the type of element(Integer, String, etc.).
Let's now see some frequently used methods that are used to perform operations on the Priority Queue.