Let’s dive into the interfaces Queue and Deque in Java — both are part of the java.util package and extend the Collection interface, but they have different purposes and capabilities.
🔷 Queue Interface
A FIFO (First-In-First-Out) structure.
Used for holding elements prior to processing.
Think of it like a line at the supermarket.
🔑 Common Methods in Queue:
Method
Description
add(e)
Inserts element; throws if full (rare in Java)
offer(e)
Inserts element; returns false if fails
remove()
Removes and returns head; throws if empty
poll()
Removes and returns head; returns null if empty
element()
Returns head without removing; throws if empty
peek()
Returns head without removing; returns null if empty
🔶 Deque Interface (extends Queue)
Stands for Double-Ended Queue.
Can be used as:
A FIFO Queue (like Queue)
A LIFO Stack (like Stack)
Think of it like a deck of cards: you can add/remove from both ends.