A PriorityQueue in Java is a queue where elements are ordered based on their priority, rather than the order of insertion. By default, it uses natural ordering (min-heap), but a custom comparator can be used to define different priorities.
- Elements are processed based on priority rather than insertion order.
- Supports standard queue operations like add(), poll(), and peek().
- Automatically grows as elements are added.
- Uses a heap data structure internally to ensure efficient insertion and removal of the highest-priority element.
Declaration of PriorityQueue
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
where, E is the type of elements held in this queue.
import java.util.PriorityQueue;
public class Geeks
{
public static void main(String[] args)
{
// Priority Queue Min Type
PriorityQueue<Integer> p = new PriorityQueue<>();
// Add elements to the queue
p.add(3);
p.add(10);
p.add(7);
p.add(2);
// Print the head of the queue
System.out.println("Head of Queue: " + p.peek());
}
}
Output
Head of Queue: 2
Hierarchy of PriorityQueue
It implements the Queue interface which extends the Collection Interface.