Queue Methods: add, remove, element, offer, poll
You'll learn how Deque supports standard Queue interface methods by delegating to its tail operations for FIFO behavior.
The Bridge Between Queue and Deque
When you use a Deque (like LinkedList or ArrayDeque) as a Queue, you get access to the standard Queue methods: add(), remove(), element(), offer(), and poll(). Behind the scenes, these methods work by delegating to the Deque's tail operations to maintain FIFO (first-in, first-out) behavior.
How the Delegation Works
Think of a Deque as a two-door building. Queue methods always use the same door for entrance (the tail) and the opposite door for exit (the head):
add(e)→ delegates toaddLast(e)— inserts at the tailoffer(e)→ delegates toofferLast(e)— inserts at the tail (returns false if full)remove()→ delegates toremoveFirst()— removes from the head (throws if empty)poll()→ delegates topollFirst()— removes from the head (returns null if empty)element()→ delegates togetFirst()— peeks at the head (throws if empty)
Why This Matters
This delegation is what makes a Deque behave like a proper Queue. Elements added via add() or offer() go to the back of the line (tail), while remove() and poll() pull from the front (head). The result: perfect FIFO ordering.
You don't have to call the Deque-specific methods yourself—just use the familiar Queue methods, and the Deque implementation handles the rest.
Key Takeaway: Queue methods on Deque implementations delegate to tail operations for insertion and head operations for removal, automatically maintaining FIFO semantics without you needing to think about "first" or "last."