The Queue interface in Java is part of the java.util package and extends the Collection interface. It represents a collection designed to hold elements before processing them in a specified order.
- Supports FIFO (First-In-First-Out) and priority-based processing, depending on the implementation.
- Provides methods to insert, remove, and examine the head element.
- Does not support direct index-based access.
import java.util.PriorityQueue;
import java.util.Queue;
public class Geeks{
public static void main(String[] args){
Queue<Integer> pq = new PriorityQueue<>();
pq.add(50);
pq.add(20);
pq.add(40);
pq.add(10);
pq.add(30);
System.out.println("Head: " + pq.peek());
// Retrieve elements in priority order
while (!pq.isEmpty()) {
System.out.print(pq.poll() + " ");
}
}
}
Output
Head: 10 10 20 30 40 50
Explanation: PriorityQueue uses natural ordering by default, so the smallest integer has the highest retrieval priority. The peek() method returns the head without removing it, while poll() removes elements one by one in priority order.
Note: PriorityQueue does not guarantee sorted order when displaying or iterating over its elements. Priority order is maintained for head retrieval and removal.
Declaration of Queue Interface
The Queue interface extends Collection and inherits its common collection operations.
Syntax:
public interface Queue<E> extends Collection<E>
Here, E represents the type of elements stored in the Queue.
Since Queue is an interface, it cannot be instantiated directly. Objects are created using implementing classes.
Queue<Integer> q1 = new LinkedList<>();
Queue<Integer> q2 = new PriorityQueue<>();
Hierarchy of Queue Interface
The Queue interface extends Collection, while Deque extends Queue. Several classes implement these interfaces.

The actual class inheritance relationships also include:
- LinkedList extends AbstractSequentialList and implements List and Deque.
- ArrayDeque extends AbstractCollection and implements Deque.
- PriorityQueue extends AbstractQueue and implements Queue.
In Java 21 and later, Deque also extends SequencedCollection.
Common Implementations of Queue Interface
- LinkedList implements Deque and List. It supports FIFO operations and permits null elements, although null should generally be avoided in queues.
- ArrayDeque uses a resizable array and supports insertion and removal at both ends. It does not permit null elements.
- PriorityQueue processes elements according to natural ordering or a supplied Comparator.
- ConcurrentLinkedQueue is a thread-safe, non-blocking FIFO queue used in concurrent applications.
Operations on Queue Interface
The Queue interface supports insertion, removal, and head-element access. The following program demonstrates these operations using PriorityQueue.
import java.util.PriorityQueue;
import java.util.Queue;
public class Geeks{
public static void main(String[] args){
Queue<Integer> pq = new PriorityQueue<>();
// Adding elements
pq.add(30);
pq.offer(10);
pq.offer(20);
// Accessing the head
System.out.println("Head: " + pq.peek());
// Removing the head
System.out.println("Removed: " + pq.poll());
// Accessing the new head
System.out.println("New Head: " + pq.element());
// Removing remaining elements
System.out.println("Removed: " + pq.remove());
System.out.println("Removed: " + pq.poll());
// Checking an empty queue
System.out.println("Empty Queue: " + pq.poll());
}
}
Output
Head: 10 Removed: 10 New Head: 20 Removed: 20 Removed: 30 Empty Queue: null
Explanation:
- add() and offer() insert elements into the queue.
- peek() and element() retrieve the head without removing it.
- poll() and the no-argument remove() retrieve and remove the head.
- poll() returns null when the queue is empty, while remove() throws a NoSuchElementException..
Note: The overloaded remove(Object o) method removes a specified matching element, not necessarily the head. It is different from remove().
Iterating Over a Queue
A Queue can be traversed using an enhanced for loop or an Iterator. However, PriorityQueue iteration does not guarantee priority order.
import java.util.*;
class Geeks{
public static void main(String[] args){
Queue<Integer> q = new LinkedList<>();
q.add(10);
q.add(20);
q.add(30);
// Using enhanced for loop
for (int n : q)
System.out.print(n + " ");
System.out.println();
// Using Iterator
Iterator<Integer> it = q.iterator();
while (it.hasNext())
System.out.print(it.next() + " ");
}
}
Output
10 20 30 10 20 30
Methods of Queue Interface
| Method | Description |
|---|---|
| add(E e) | Inserts an element and throws an exception if it cannot be added. |
| offer(E e) | Attempts to insert an element and returns false if it cannot be added. |
| remove() | Retrieves and removes the head; throws an exception if the queue is empty. |
| poll() | Retrieves and removes the head; returns null if the queue is empty. |
| element() | Retrieves but does not remove the head; throws an exception if empty. |
| peek() | Retrieves but does not remove the head; returns null if empty. |