Queue Interface in Java
Introduction
In programming, we often need to process elements in a first-in, first-out sequence—such as processing incoming network packets, handling requests in a web server, or scheduling print jobs.
The Java Collection Framework (JCF) provides the Queue interface to represent collections designed for holding elements prior to processing.
Why Queue?
Unlike a List where you can insert or retrieve elements at any random index, a Queue enforces strict access rules. Elements are appended to the tail (end) of the queue and retrieved from the head (front) of the queue. This prevents out-of-order processing, making it ideal for message routing and task pipelines.
The FIFO Principle
A standard queue operates on the First-In, First-Out (FIFO) principle:
The first element added to the queue is the first one removed.
Queue Class Hierarchy
classDiagram
class Collection {
<<interface>>
}
class Queue {
<<interface>>
}
class Deque {
<<interface>>
}
class PriorityQueue {
}
class LinkedList {
}
class ArrayDeque {
}
Collection <|-- Queue : extends
Queue <|-- Deque : extends
Queue <|.. PriorityQueue : implements
Deque <|.. LinkedList : implements
Deque <|.. ArrayDeque : implements Creating a Queue
Since Queue is an interface, you cannot instantiate it directly. Instead, you instantiate one of its implementations:
1. Using LinkedList (Standard FIFO Queue)
2. Using ArrayDeque (Standard FIFO Queue, faster than LinkedList)
Common Methods
The Queue interface defines two sets of methods for adding, removing, and inspecting elements. One set throws an exception if the operation fails, while the other returns a special value (like null or false):
| Operation | Throws Exception | Returns Special Value |
|---|---|---|
| Insert (Add to tail) | add(e) | offer(e) |
| Remove (Remove from head) | remove() | poll() |
| Examine (Inspect head) | element() | peek() |
Complete Programs
Example: Basic FIFO Operations
import java.util.ArrayDeque;
import java.util.Queue;
public class QueueDemo {
public static void main(String[] args) {
Queue<String> customerLine = new ArrayDeque<>();
// 1. Enqueue elements
customerLine.offer("Rahul");
customerLine.offer("Amit");
customerLine.offer("Priya");
System.out.println("Customer Line: " + customerLine); // [Rahul, Amit, Priya]
// 2. Inspect head
System.out.println("Next customer to serve: " + customerLine.peek()); // Rahul
// 3. Dequeue elements
System.out.println("Served: " + customerLine.poll()); // Rahul
System.out.println("Served: " + customerLine.poll()); // Amit
System.out.println("Remaining Line: " + customerLine); // [Priya]
}
}
Best Practices
- Use
offer(),poll(), andpeek(): Prefer these methods overadd(),remove(), andelement()to avoid writing try-catch blocks for normal queue operations. - Prefer ArrayDeque over LinkedList: When implementing a standard FIFO queue,
ArrayDequeis faster and consumes less memory because it does not require allocating node objects for every element.
Common Mistakes
- Adding Nulls: Do not insert
nullelements into a queue. Methods likepoll()andpeek()returnnullto indicate the queue is empty, so storingnullvalues makes it impossible to check if the queue is empty. - Random Access: Do not cast a
Queueto aListto retrieve elements by index. This violates the Queue design pattern.
Key Takeaways
- The
Queueinterface represents collections designed for FIFO processing. - Standard implementations include
ArrayDequeandLinkedList. - Safe methods (
offer,poll,peek) return special values instead of throwing exceptions.
Back to Queues Home: Queues Index