Java Programming Handbook

    Java ArrayDeque

    The ArrayDeque class in Java is a resizable-array implementation of the Deque interface. Unlike regular queues or stacks, ArrayDeque supports insertion and removal at both ends, making it ideal for queue and stack operations.

    It is faster than LinkedList for most queue operations and does not allow null elements.

    Key Characteristics#

    • Implements Deque, Queue, and Iterable interfaces.
    • Backed by a resizable array.
    • Faster than Stack and LinkedList for stack/queue operations.
    • Null elements are not allowed.
    • Not thread-safe (must use externally synchronized if needed).

    Internal Working#

    Array Deque
    • Backed by a circular array.
    • When the array is full, it resizes (doubles capacity).
    • Offers amortized constant time for insertions/removals from both ends.

    Constructors#

    ConstructorDescription
    ArrayDeque()Creates an empty deque with default capacity.
    ArrayDeque(int numElements)Creates a deque with enough space for the given number of elements.
    ArrayDeque(Collection<? extends E> c)Creates a deque initialized with elements from the specified collection.

    Commonly Used Methods (with Examples)#

    1. addFirst(E e) - Inserts element at the front#

    ArrayDeque<Integer> deque = new ArrayDeque<>(); deque.addFirst(10); deque.addFirst(20); System.out.println("Deque: " + deque);

    Output:

    Deque: [20, 10]

    2. addLast(E e) - Inserts element at the end#

    deque.addLast(30); System.out.println("After addLast: " + deque);

    Output:

    After addLast: [20, 10, 30]

    3. offerFirst(E e) - Inserts at front, returns false if capacity restricted#

    deque.offerFirst(40); System.out.println("After offerFirst: " + deque);

    Output:

    After offerFirst: [40, 20, 10, 30]

    4. offerLast(E e) - Inserts at rear#

    deque.offerLast(50); System.out.println("After offerLast: " + deque);

    Output:

    After offerLast: [40, 20, 10, 30, 50]

    5. removeFirst() - Removes and returns the first element#

    System.out.println("Removed First: " + deque.removeFirst()); System.out.println("After removeFirst: " + deque);

    Output:

    Removed First: 40 After removeFirst: [20, 10, 30, 50]

    6. removeLast() - Removes and returns the last element#

    System.out.println("Removed Last: " + deque.removeLast()); System.out.println("After removeLast: " + deque);

    Output:

    Removed Last: 50 After removeLast: [20, 10, 30]

    7. pollFirst() - Retrieves and removes first element or null if empty#

    System.out.println("Poll First: " + deque.pollFirst());

    Output:

    Poll First: 20

    8. pollLast() - Retrieves and removes last element or null if empty#

    System.out.println("Poll Last: " + deque.pollLast());

    Output:

    Poll Last: 30

    9. peekFirst() - Retrieves but doesn't remove the first element#

    System.out.println("Peek First: " + deque.peekFirst());

    Output:

    Peek First: 10

    10. peekLast() - Retrieves but doesn't remove the last element#

    System.out.println("Peek Last: " + deque.peekLast());

    Output:

    Peek Last: 10

    11. contains(Object o) - Checks if deque contains the element#

    System.out.println("Contains 10? " + deque.contains(10));

    Output:

    Contains 10? true

    12. size() - Returns the number of elements#

    System.out.println("Size: " + deque.size());

    Output:

    Size: 1

    13. clear() - Removes all elements#

    deque.clear(); System.out.println("After clear: " + deque);

    Output:

    After clear: []

    Performance Overview#

    OperationTime Complexity
    addFirst() / addLast()O(1) amortized
    removeFirst() / removeLast()O(1) amortized
    peekFirst() / peekLast()O(1)
    contains()O(n)
    clear()O(n)
    • Fast queue and stack operations.
    • No overhead of node pointers (as in LinkedList).

    Limitations#

    • Not thread-safe.
    • No capacity limit enforcement.
    • Cannot store null elements (throws NullPointerException).

    Real-World Use Cases#

    • Browser history (forward/backward navigation).
    • Undo/Redo functionality.
    • Task Scheduling (round-robin execution).
    • Palindrome checking (by comparing ends).

    Conclusion#

    ArrayDeque is an efficient and flexible choice when you need a double-ended queue or a stack with better performance than Stack or LinkedList. It is an excellent general-purpose queue with fast and predictable performance for typical queue and stack use cases.

    Last updated on Apr 09, 2025