Java Queue & Deque — Explained with Simple Examples

 

Java Queue & Deque — Explained with Simple Examples

In Java, Queue and Deque are important data structures widely used in real-world systems like messaging, scheduling, caching, undo/redo, browser history, task execution, and more. They come from the java.util package and follow FIFO / LIFO styles of processing.


✅ 1. What is a Queue in Java?

A Queue stores elements in the order they arrive and processes them in the same order.

👉 Works on: FIFO (First In – First Out)
📦 Example in real life: People standing in a line at a ticket counter.

Queue Interface Hierarchy

Collection | ---> Queue

Common Queue Implementations in Java

ImplementationDescription
LinkedListSimple queue (FIFO)
PriorityQueueOrders elements by priority (NOT FIFO)
ArrayDequeFaster queue without capacity limit

🔹 Basic Queue Operations

MethodDescription
add(e) / offer(e)Insert element
remove() / poll()Remove head
element() / peek()View head

💡 Difference between add() & offer()

  • add() → throws exception if queue is full

  • offer() → returns false if queue is full


🧪 Example 1 — Simple Queue using LinkedList

import java.util.Queue; import java.util.LinkedList; public class SimpleQueueExample { public static void main(String[] args) { Queue<String> queue = new LinkedList<>(); queue.offer("A"); queue.offer("B"); queue.offer("C"); System.out.println("Queue: " + queue); System.out.println("Peek: " + queue.peek()); System.out.println("Removed: " + queue.poll()); System.out.println("After Removal: " + queue); } }

Output

Queue: [A, B, C] Peek: A Removed: A After Removal: [B, C]

🧪 Example 2 — PriorityQueue (NOT FIFO)

PriorityQueue orders elements by natural order or custom comparator.

import java.util.PriorityQueue; public class PriorityQueueExample { public static void main(String[] args) { PriorityQueue<Integer> pq = new PriorityQueue<>(); pq.add(30); pq.add(5); pq.add(20); System.out.println(pq.poll()); System.out.println(pq.poll()); System.out.println(pq.poll()); } }

Output

5 20 30

👉 Smallest element gets highest priority.


🧪 Example 3 — Max Priority Queue

import java.util.*; public class MaxPriorityQueueExample { public static void main(String[] args) { PriorityQueue<Integer> maxPQ = new PriorityQueue<>(Collections.reverseOrder()); maxPQ.add(10); maxPQ.add(50); maxPQ.add(30); while(!maxPQ.isEmpty()) { System.out.println(maxPQ.poll()); } } }

Output

50 30 10

✅ 2. What is a Deque in Java?

Deque = Double Ended Queue
You can insert and remove from BOTH ends.

👉 Works as:

  • Queue (FIFO)

  • Stack (LIFO)

Deque Interface Hierarchy

Collection | Deque

Common Deque Implementation

ImplementationDescription
ArrayDequeMost common + fastest
LinkedListAlso supports Deque

Deque Operations

Insert

Method
addFirst()
addLast()
offerFirst()
offerLast()

Remove

Method
pollFirst()
pollLast()
removeFirst()
removeLast()

Peek

Method
peekFirst()
peekLast()

🧪 Example 4 — Deque as Queue (FIFO)

import java.util.Deque; import java.util.ArrayDeque; public class DequeAsQueue { public static void main(String[] args) { Deque<String> dq = new ArrayDeque<>(); dq.offerLast("A"); dq.offerLast("B"); dq.offerLast("C"); System.out.println(dq); System.out.println("Removed: " + dq.pollFirst()); System.out.println(dq); } }

Output

[A, B, C] Removed: A [B, C]

🧪 Example 5 — Deque as Stack (LIFO)

import java.util.ArrayDeque; import java.util.Deque; public class DequeAsStack { public static void main(String[] args) { Deque<Integer> stack = new ArrayDeque<>(); stack.push(10); stack.push(20); stack.push(30); System.out.println(stack); System.out.println("Popped: " + stack.pop()); System.out.println(stack); } }

Output

[30, 20, 10] Popped: 30 [20, 10]

👉 ArrayDeque is recommended instead of Stack class
because Stack is synchronized & slower.


Queue vs Deque — Quick Comparison

FeatureQueueDeque
OrderFIFOFIFO + LIFO
Insert EndsRear onlyBoth ends
Remove EndsFront onlyBoth ends
Common ImplLinkedList, PriorityQueueArrayDeque

When to Use What?

Use CaseBest Choice
Simple FIFO tasksQueue (LinkedList)
Priority-based processingPriorityQueue
Stack replacementDeque
Both stack + queue flexibilityArrayDeque

Conclusion

Java provides powerful Queue and Deque implementations that are widely used in real-world systems for task scheduling, background processing, messaging, caching, and more.

  • Use Queue when order matters (FIFO)

  • Use PriorityQueue when priority matters

  • Use Deque / ArrayDeque when you need both stack + queue behavior

No comments:

Post a Comment

System Design + Java Program for Tic-Tac-Toe (TicTac)

  🎮 System Design + Java Program for Tic-Tac-Toe (TicTac) In this mini system design, we’ll build a console-based Tic-Tac-Toe game in Java...

Featured Posts