

Dont worry we study this hirarchy one by one, first you understand these points.
Now see the below image.

Queue:
Queue interface represents a collection designed for holding elements prior to processing. It follows the FIFO (First-In-First-Out) principle.| Methods | Description |
|---|---|
| boolean add(E e) |
|
| boolean offer(E e) |
|
| E remove() |
|
| E poll() |
|
| E element() |
|
| E peek() |
|
import java.util.Queue;import java.util.ArrayDeque;public class QueueExample { public static void main(String[] args) { // Create a Queue using ArrayDeque Queue<String> queue = new ArrayDeque<>(); // Add elements to the queue queue.add("A"); queue.add("B"); queue.add("C"); // Display the queue System.out.println("Queue: " + queue); // Output: Queue: [A, B, C] // Retrieve and remove the head of the queue System.out.println("Removed from queue: " + queue.remove()); // Output: Removed from queue: A System.out.println("Queue after removal: " + queue); // Output: Queue after removal: [B, C] // Retrieve the head of the queue without removing System.out.println("Head of queue: " + queue.element()); // Output: Head of queue: B // Poll the queue (retrieve and remove head or return null if empty) System.out.println("Polled from queue: " + queue.poll()); // Output: Polled from queue: B System.out.println("Queue after polling: " + queue); // Output: Queue after polling: [C] // Peek at the head of the queue without removing System.out.println("Peek at head: " + queue.peek()); // Output: Peek at head: C }}Deque interface (Double-Ended Queue) extends the Queue interface and allows elements to be added or removed from both ends of the queue.| Operation | Methods |
|---|---|
| Adding Elements | void addFirst(E e)
void addLast(E e)
boolean offerFirst(E e)
boolean offerLast(E e)
|
| Removing Elements | E removeFirst()
E removeLast()
E pollFirst()
E pollLast()
|
| Retrieving Elements | E getFirst()
E getLast()
E peekFirst()
E peekLast()
|
| Stack Operations | void push(E e)
E pop()
|
xxxxxxxxxximport java.util.Deque;import java.util.ArrayDeque;public class DequeExample { public static void main(String[] args) { // Create a Deque using ArrayDeque Deque<String> deque = new ArrayDeque<>(); // Add elements to the deque at both ends deque.addFirst("A"); deque.addLast("B"); deque.addFirst("C"); deque.addLast("D"); // Display the deque System.out.println("Deque: " + deque); // Output: Deque: [C, A, B, D] // Retrieve and remove elements from both ends System.out.println("Removed first from deque: " + deque.removeFirst()); // Output: Removed first from deque: C System.out.println("Removed last from deque: " + deque.removeLast()); // Output: Removed last from deque: D System.out.println("Deque after removals: " + deque); // Output: Deque after removals: [A, B] // Peek at the first and last elements without removing them System.out.println("Peek first: " + deque.peekFirst()); // Output: Peek first: A System.out.println("Peek last: " + deque.peekLast()); // Output: Peek last: B // Poll the deque (retrieve and remove first or return null if empty) System.out.println("Polled first: " + deque.pollFirst()); // Output: Polled first: A System.out.println("Polled last: " + deque.pollLast()); // Output: Polled last: B System.out.println("Deque after polling: " + deque); // Output: Deque after polling: [] }}Queue Method Equivalent Deque Method
------------- -----------------------
add(e) addLast(e)
offer(e) offerLast(e)
remove() removeFirst()
poll() pollFirst()
element() getFirst()
peek() peekFirst()
ArrayDeque is a resizable-array implementation of the Deque interface. It is not thread-safe but provides fast and efficient implementations of the methods defined by the Deque interface.Deque and Queue.Constructors:
Key Points:
ArrayDeque dynamically resizes its array to accommodate more elements.ArrayDeque provides constant time for add, remove, and peek operations at both ends.xxxxxxxxxximport java.util.ArrayDeque;import java.util.Deque;import java.util.Queue;public class DequeExample { public static void main(String[] args) { // Using ArrayDeque as a Queue Queue<String> queue = new ArrayDeque<>(); // Add elements to the queue queue.add("A"); queue.add("B"); queue.add("C"); // Display the queue System.out.println("Queue: " + queue); // Output: Queue: [A, B, C] // Retrieve and remove the head of the queue System.out.println("Removed from queue: " + queue.remove()); // Output: Removed from queue: A System.out.println("Queue after removal: " + queue); // Output: Queue after removal: [B, C] // Retrieve the head of the queue without removing System.out.println("Head of queue: " + queue.element()); // Output: Head of queue: B // Using ArrayDeque as a Deque Deque<String> deque = new ArrayDeque<>(); // Add elements to the deque at both ends deque.addFirst("A"); deque.addLast("B"); deque.addFirst("C"); deque.addLast("D"); // Display the deque System.out.println("Deque: " + deque); // Output: Deque: [C, A, B, D] // Retrieve and remove elements from both ends System.out.println("Removed first from deque: " + deque.removeFirst()); // Output: Removed first from deque: C System.out.println("Removed last from deque: " + deque.removeLast()); // Output: Removed last from deque: D System.out.println("Deque after removals: " + deque); // Output: Deque after removals: [A, B] // Using ArrayDeque as a Stack ArrayDeque<String> stack = new ArrayDeque<>(); // Push elements onto the stack stack.push("A"); stack.push("B"); stack.push("C"); // Display the stack System.out.println("Stack: " + stack); // Output: Stack: [C, B, A] // Pop elements from the stack System.out.println("Popped from stack: " + stack.pop()); // Output: Popped from stack: C System.out.println("Stack after pop: " + stack); // Output: Stack after pop: [B, A] // Peek at the top element of the stack System.out.println("Top of stack: " + stack.peek()); // Output: Top of stack: B }}