public class DequeAsStack {
public static void main(String[] args) {
// Creating a Deque as Stack
Deque<String> stack = new ArrayDeque<>();
System.out.println("=== DEQUE AS STACK (LIFO) ===\n");
// 1. PUSH - Add elements to top
System.out.println("Pushing elements:");
stack.push("First"); // Bottom
stack.push("Second");
stack.push("Third"); // Top
System.out.println("Stack: " + stack); // [Third, Second, First]
// 2. PEEK - View top element without removing
System.out.println("\nPeek top: " + stack.peek()); // Third
System.out.println("Stack after peek: " + stack); // [Third, Second, First]
// 3. POP - Remove and return top element
System.out.println("\nPopping elements:");
System.out.println("Popped: " + stack.pop()); // Third
System.out.println("Popped: " + stack.pop()); // Second
System.out.println("Stack after pops: " + stack); // [First]
// 4. Check if empty
System.out.println("\nIs stack empty? " + stack.isEmpty()); // false
// 5. Push more elements
stack.push("Fourth");
stack.push("Fifth");
System.out.println("Stack after more pushes: " + stack); // [Fifth, Fourth, First]
// 6. Pop all elements
System.out.println("\nPopping all elements:");
while (!stack.isEmpty()) {
System.out.println("Popped: " + stack.pop());
}
System.out.println("Stack is now empty: " + stack.isEmpty()); // true
}
}
public class DequeAsQueue {
public static void main(String[] args) {
// Creating a Deque as Queue
Deque<String> queue = new ArrayDeque<>();
System.out.println("=== DEQUE AS QUEUE (FIFO) ===\n");
// 1. OFFER/ADD - Add elements to tail (end)
System.out.println("Adding elements to queue:");
queue.offer("First"); // Head (will be processed first)
queue.offer("Second");
queue.offer("Third"); // Tail (will be processed last)
System.out.println("Queue: " + queue); // [First, Second, Third]
// 2. PEEK - View head element without removing
System.out.println("\nPeek head: " + queue.peek()); // First
System.out.println("Queue after peek: " + queue); // [First, Second, Third]
// 3. POLL - Remove and return head element
System.out.println("\nProcessing queue elements:");
System.out.println("Processed: " + queue.poll()); // First
System.out.println("Processed: " + queue.poll()); // Second
System.out.println("Queue after processing: " + queue); // [Third]
// 4. Add more elements
queue.offer("Fourth");
queue.offer("Fifth");
System.out.println("\nQueue after more adds: " + queue); // [Third, Fourth, Fifth]
// 5. Process all remaining elements
System.out.println("\nProcessing remaining elements:");
while (!queue.isEmpty()) {
System.out.println("Processed: " + queue.poll());
}
System.out.println("Queue is now empty: " + queue.isEmpty()); // true
// 6. Alternative methods
queue.add("Element 1"); // Throws exception if full
queue.offer("Element 2"); // Returns false if full
System.out.println("\nUsing add(): " + queue);
}
}
10. Summary Table
|
Aspect |
Stack |
Deque (ArrayDeque) |
|
Thread Safety |
✅ Synchronized |
❌ Not synchronized |
|
Performance |
🟡 Moderate |
✅ Excellent |
|
Memory |
🟡 Moderate |
✅ Efficient |
|
Flexibility |
❌ Stack only |
✅ Stack + Queue |
|
Modern API |
❌ Legacy |
✅ Modern |
|
Recommended |
❌ No |
✅ Yes |
// ❌ DON'T use Stack in new code
Stack<String> stack = new Stack<>();
// ✅ DO use Deque with ArrayDeque
Deque<String> stack = new ArrayDeque<>();
// ✅ For thread-safe operations, use ConcurrentLinkedDeque
Deque<String> concurrent = new ConcurrentLinkedDeque<>();
// ✅ Or synchronize your Deque
Deque<String> synchronizedDeque = Collections.synchronizedDeque(new ArrayDeque<>());