☕ Java Intermediate

What is the Deque interface in Java?

Answer

A Deque (Double-Ended Queue) supports adding and removing elements from both ends efficiently. It combines the functionality of both a Stack (LIFO) and a Queue (FIFO). The Deque interface provides methods for both ends: addFirst()/addLast(), removeFirst()/removeLast(), peekFirst()/peekLast(). Main implementations: ArrayDeque (resizable array, no null elements, faster than Stack and LinkedList for stack/queue use) and LinkedList (node-based). Java's Stack class is legacy and thread-synchronized — prefer ArrayDeque for stack and queue operations in single-threaded code.