Queue in Java
Introduction
A Queue in Java is a collection that is used to store elements in a particular order. It generally follows the FIFO (First In, First Out) principle.
This means the element added first is usually the element removed first.
The Queue interface is part of the Java Collection Framework and is available in the java.util package.
Example
Queue ---------------- A → B → C → D First element added: A First element removed: A
Features of Queue
- Generally follows FIFO order.
- Elements are added at the rear.
- Elements are removed from the front.
- Supports insertion and removal operations.
- Allows efficient processing of elements.
- Useful for scheduling and waiting-line applications.
Queue Syntax
Queue<Type> queueName = new LinkedList<>();
Example:
Queue<String> students = new LinkedList<>();
Example of Queue
import java.util.*; public class QueueExample { public static void main(String[] args) { Queue<String> queue = new LinkedList<>(); queue.add("Anu"); queue.add("Ram"); queue.add("Sita"); System.out.println(queue); System.out.println(queue.poll()); System.out.println(queue); } }
Output
[Anu, Ram, Sita] Anu [Ram, Sita]
The first element "Anu" was removed first because Queue generally follows FIFO.
Common Queue Methods
| Method | Description |
|---|---|
add() | Adds an element |
offer() | Adds an element |
remove() | Removes and returns the front element |
poll() | Removes and returns the front element |
element() | Returns the front element |
peek() | Returns the front element without removing it |
size() | Returns the number of elements |
isEmpty() | Checks whether the Queue is empty |
Example of Queue Methods
import java.util.*; public class QueueMethods { public static void main(String[] args) { Queue<Integer> numbers = new LinkedList<>(); numbers.offer(10); numbers.offer(20); numbers.offer(30); System.out.println(numbers); System.out.println(numbers.peek()); System.out.println(numbers.poll()); System.out.println(numbers); } }
Output
[10, 20, 30] 10 10 [20, 30]
Difference Between add() and offer()
Both methods are used to insert elements into a Queue.
add() | offer() |
|---|---|
| Adds an element | Adds an element |
| May throw an exception if insertion fails | Returns false if insertion fails |
| Part of Collection interface | Specifically useful for Queue operations |
Difference Between remove() and poll()
Both methods remove the front element.
remove() | poll() |
|---|---|
| Removes the front element | Removes the front element |
| Throws exception if Queue is empty | Returns null if Queue is empty |
Difference Between element() and peek()
Both methods return the front element without removing it.
element() | peek() |
|---|---|
| Returns the front element | Returns the front element |
| Throws exception if Queue is empty | Returns null if Queue is empty |
Types of Queue
1. LinkedList
LinkedList can be used as a Queue.
Queue<String> queue = new LinkedList<>(); queue.add("A"); queue.add("B"); queue.add("C"); System.out.println(queue);
2. PriorityQueue
PriorityQueue processes elements according to their priority rather than simple insertion order.
Queue<Integer> numbers = new PriorityQueue<>(); numbers.add(30); numbers.add(10); numbers.add(20); System.out.println(numbers);
The smallest element has the highest priority by default.
For example, removing elements:
System.out.println(numbers.poll()); System.out.println(numbers.poll());
Output:
10 20
3. ArrayDeque
ArrayDeque is a resizable-array implementation of the Deque interface and can be used as a Queue.
Queue<String> queue = new ArrayDeque<>(); queue.add("A"); queue.add("B"); queue.add("C"); System.out.println(queue);
Queue vs Stack
| Queue | Stack |
|---|---|
| Generally follows FIFO | Follows LIFO |
| First element is removed first | Last element is removed first |
| Insertion at rear | Insertion at top |
| Removal from front | Removal from top |
| Used in scheduling | Used in undo operations |
Queue vs List
| Queue | List |
|---|---|
| Designed for processing elements in queue order | Designed for general ordered storage |
| Usually uses front/rear operations | Uses index-based operations |
poll(), offer(), peek() are common | get(), set(), add() are common |
| Useful for waiting-line systems | Useful for general collections |
Real-Life Examples
Queue is commonly used in situations where the first person or item should be processed first.
Examples:
- Printer job scheduling
- Customer service lines
- CPU task scheduling
- Ticket booking systems
- Network data processing
- Call center waiting systems
Real-Life Example
Customer Queue Anu → Ram → Sita → Hari ↑ First customer Anu is served first.
Advantages of Queue
- Follows FIFO processing in typical Queue implementations.
- Useful for scheduling tasks.
- Provides efficient insertion and removal operations.
- Useful for managing waiting lines.
- Supports different implementations such as
LinkedList,PriorityQueue, andArrayDeque.
Key Points
Queueis an interface in Java.- It belongs to the
java.utilpackage. - Queue generally follows FIFO.
offer()andadd()are used for insertion.poll()andremove()are used for removal.peek()returns the front element without removing it.PriorityQueueprocesses elements according to priority.ArrayDequecan also be used as a Queue.
Conclusion
A Queue is an important Java collection used when elements need to be processed in a specific order. It is commonly based on the FIFO principle, making it useful for scheduling, task processing, and real-world waiting-line systems.