Introduction
The Java Queue interface is an essential part of the Java Collection Framework that is used to store and process elements in a specific order. Unlike a Stack, which follows the LIFO (Last In, First Out) principle, a Queue generally follows the FIFO (First In, First Out) principle. This means the element that is inserted first is removed first.
The Java Queue interface is an essential part of the Java Collection Framework that is used to store and process elements in a specific order. Unlike a Stack, which follows the LIFO (Last In, First Out) principle, a Queue generally follows the FIFO (First In, First Out) principle. This means the element that is inserted first is removed first.
The Queue interface is available in the java.util package and extends the Collection interface. Since Queue is an interface, it cannot be instantiated directly. Instead, Java provides several implementations such as LinkedList, PriorityQueue, ArrayDeque, and various concurrent queue implementations.
What is Java Queue?
A Queue is a collection designed to hold elements before processing them. In most implementations, elements are processed according to the FIFO (First In, First Out) principle.
For example:
- Rahul joins the queue.
- Amit joins the queue.
- Neha joins the queue.
Processing order:
Rahul → Amit → Neha
Rahul leaves first because he entered first.
Key Characteristics
- Follows FIFO order
- Stores duplicate elements
- Supports dynamic size
- Generally does not allow random access
- Allows insertion at the rear
- Removes elements from the front
- Supports multiple implementations
Java Queue Hierarchy
Iterable
│
Collection
│
Queue
│
├── LinkedList
├── PriorityQueue
├── ArrayDeque
└── Deque
Some concurrent queue implementations include:
- PriorityBlockingQueue
- LinkedBlockingQueue
- ArrayBlockingQueue
- ConcurrentLinkedQueue
Features of Java Queue
Some important features include:
- FIFO processing
- Dynamic size
- Duplicate elements allowed
- Supports Generics
- Multiple implementations
- Efficient insertion and removal
- Easy traversal
- Suitable for scheduling tasks
Syntax
Import Statement
import java.util.Queue;
import java.util.LinkedList;
or
import java.util.*;
Creating a Queue
Queue<String> queue = new LinkedList<>();
Another implementation:
Queue<Integer> queue = new PriorityQueue<>();
Common Methods of Queue
| Method | Description |
|---|---|
| offer() | Inserts an element into the queue |
| add() | Adds an element (throws exception if insertion fails) |
| poll() | Removes and returns the front element |
| remove() | Removes the front element (throws exception if empty) |
| peek() | Returns the front element without removing it |
| element() | Returns the front element (throws exception if empty) |
| size() | Returns the number of elements |
| isEmpty() | Checks whether the queue is empty |
| clear() | Removes all elements |
| contains() | Checks whether an element exists |
Example 1: Creating a Queue
import java.util.*;
public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.offer("Java");
queue.offer("Python");
queue.offer("C++");
System.out.println(queue);
}
}
Output
Example 2: Removing an Element
import java.util.*;
public class PollExample {
public static void main(String[] args) {
Queue<Integerg> queue = new LinkedList<>();
queue.offer(10);
queue.offer(20);
queue.offer(30);
System.out.println(queue.poll());
System.out.println(queue);
}
}
Output
[20, 30]
Example 3: Viewing the Front Element
import java.util.*;
public class PeekExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.offer("Apple");
queue.offer("Banana");
queue.offer("Mango");
System.out.println(queue.peek());
}
}
Output
The peek() method returns the first element without removing it.
Example 4: Checking if Queue is Empty
import java.util.*;
public class EmptyExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
System.out.println(queue.isEmpty());
queue.offer("Java");
System.out.println(queue.isEmpty());
}
}
Output
false
Example 5: Iterating Through a Queue
import java.util.*;
public class LoopExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.offer("Java");
queue.offer("Python");
queue.offer("Spring Boot");
for(String course : queue){
System.out.println(course);
}
}
}
Output
Python
Spring Boot
Queue Implementations
1. LinkedList
- Implements Queue and Deque
- Maintains insertion order
- FIFO processing
- Allows null elements
Best for:
- General queue operations
2. PriorityQueue
- Processes elements based on priority
- Does not follow FIFO for all elements
- Natural ordering by default
- Does not allow null values
Best for:
- Task scheduling
- Job prioritization
3. ArrayDeque
- Faster than Stack and LinkedList for queue operations
- Double-ended queue
- Does not allow null values
Best for:
- Queue and stack implementations
Queue vs Stack
| Feature | Queue | Stack |
|---|---|---|
| Principle | FIFO | LIFO |
| Insertion | Rear | Top |
| Removal | Front | Top |
| Common Methods | offer(), poll() | push(), pop() |
| Common Uses | Scheduling | Undo operations |
Queue vs PriorityQueue
| Data Type | Typical Size |
|---|---|
| Queue (LinkedList) | PriorityQueue |
| FIFO | Priority-based |
| Maintains insertion order | Does not maintain insertion order |
| Suitable for sequential processing | Suitable for priority scheduling |
Time Complexity
| Operation | Time Complexity |
|---|---|
| offer() | O(1) |
| poll() | O(1) |
| peek() | O(1) |
| search | O(n) |
| contains() | O(n) |
Time complexity may vary depending on the specific Queue implementation. For example, PriorityQueue has O(log n) complexity for insertion and removal.
Real-life Example
Imagine you’re developing a ticket booking system.
Customers join a waiting queue:
1. Rahul
2. Amit
3. Neha
4. Priya
The booking system serves customers in the same order:
Rahul → Amit → Neha → Priya
This perfectly follows the FIFO principle.
Other real-world applications include:
- Printer job scheduling
- CPU scheduling
- Call center management
- Hospital patient queues
- Message queues
- Order processing systems
- Customer support systems
- Network packet processing
Advantages of Java Queue
- Simple FIFO processing
- Dynamic size
- Efficient insertion and removal
- Multiple implementations
- Supports Generics
- Easy traversal
- Suitable for scheduling applications
- Integrates well with the Java Collection Framework
Limitations of Java Queue
- Does not support efficient random access
- Some implementations do not allow null elements
- Behavior differs across implementations (for example, PriorityQueue)
- Queue is an interface and cannot be instantiated directly
Common Mistakes
1. Instantiating Queue Directly
Incorrect:
Queue<String> queue = new LinkedList<>();
Correct:
Queue<String> queue = new LinkedList<>();
2. Confusing Queue with Stack
A Queue follows FIFO, while a Stack follows LIFO.
3. Using remove() Without Checking
Calling remove() on an empty queue throws a NoSuchElementException.
Safer alternative:
queue.poll();
4. Assuming PriorityQueue Maintains Insertion Order
PriorityQueue arranges elements according to their priority, not the order in which they were inserted.
5. Forgetting Generics
Incorrect:
Queue queue = new LinkedList();
Correct:
Queue<String> queue = new LinkedList<>();
Best Practices
- Program to the Queue interface instead of concrete classes.
Queue<String> queue = new LinkedList<>();
- Use offer() and poll() for safer queue operations.
- Choose LinkedList for standard FIFO queues.
- Use PriorityQueue when processing should depend on priority.
- Prefer ArrayDeque for high-performance queue operations when thread safety is not required.
- Always use Generics for type safety.
Conclusion
The Java Queue interface is one of the most useful components of the Java Collection Framework for processing elements in sequence. By following the FIFO (First In, First Out) principle, Queue is ideal for scheduling, buffering, task management, and many other real-world applications.