ArrayDeque in Java: Methods, Constructors and a Working Example

ArrayDeque in Java: An Overview

ArrayDeque belongs to the Java Collections Framework. It extends AbstractCollection and implements the Deque, Cloneable, and Serializable interfaces. Because it provides a resizable-array implementation of the Deque interface, it is commonly referred to as an Array Double Ended Queue, or Array Deck.

  • Array deques grow on their own, so capacity is never fixed.
  • They are not thread-safe and do not allow concurrent access from multiple threads.
  • Null elements cannot be stored in an ArrayDeque.
  • In general, ArrayDeque runs faster than Stack and other Deque implementations.

ArrayDeque Constructors

  • ArrayDeque(): The default constructor, which creates an empty ArrayDeque with a default initial capacity of 16.
  • ArrayDeque(Collection<E> c): Creates an ArrayDeque containing the elements of the given collection c.
  • ArrayDeque(int initialCapacity): Creates an empty ArrayDeque with the capacity you choose.

ArrayDeque Methods in Java

Adding Elements

  • add(E e): Appends the given element to the end of the Deque; returns boolean.
  • addFirst(E e): Inserts the given element at the front of the Deque; returns void.
  • addLast(E e): Inserts the given element at the end of the Deque; returns void.
  • offer(E e): Adds the given element at the end of the Deque; returns boolean.
  • offerFirst(E e): Adds the given element at the front of the Deque; returns boolean.
  • offerLast(E e): Adds the given element at the end of the Deque; returns boolean.
  • push(): Adds the given element at the front of the Deque; returns void.

Removing Elements

  • remove(): Removes and returns the first element of the Deque; returns E.
  • removeFirst(): Removes and returns the first element of the Deque; returns E.
  • removeLast(): Removes and returns the last element of the Deque; returns E.
  • removeFirstOccurrence(): Removes the first occurrence of the specified element; returns boolean.
  • removeLastOccurrence(): Removes the last occurrence of the specified element; returns boolean.
  • poll(): Removes and returns the first element of the Deque; returns E.
  • pollFirst(): Removes and returns the first element of the Deque; returns E.
  • pollLast(): Removes and returns the last element of the Deque; returns E.
  • pop(): Removes and returns the first element of the Deque; returns E.
  • clear(): Removes every element from the Deque; returns void.

Accessing Elements

  • getFirst(): Retrieves the first element of the Deque; returns E.
  • getLast(): Retrieves the last element of the Deque; returns E.
  • peek(): Retrieves the first element of the Deque; returns E.
  • peekFirst(): Retrieves the first element of the Deque; returns E.
  • peekLast(): Retrieves the last element of the Deque; returns E.
  • element(): Retrieves the first element without removing it; returns E.

Utility and Inspection Methods

  • isEmpty(): Checks whether the Deque is empty; returns boolean.
  • contains(Object o): Checks whether the given element exists in the collection; returns boolean.
  • size(): Reports how many elements the Deque currently holds.
  • clone(): Returns a shallow copy of this Deque instance.
  • iterator(): Returns an iterator over the elements of this Deque.
  • descendingIterator(): Returns an iterator over the elements in descending order.

Example Program

The Java program below shows a few ArrayDeque methods in action:

// Java program to demonstrate few functions of
// ArrayDeque in Java
import java.util.ArrayDeque;

public class ExampleOfArrayDeque {
    public static void main(String[] args) {
        ArrayDeque<Integer> numbers = new ArrayDeque<Integer>(10);
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
        numbers.add(40);
        numbers.add(50);

        for (Integer element : numbers) {
            System.out.println("Element : " + element);
        }

        // remove() method : to get head
        System.out.println("Head element remove : " + numbers.remove());
        System.out.println("The final array is: " + numbers);
    }
}

Output:

Element : 10
Element : 20
Element : 30
Element : 40
Element : 50
Head element remove : 10
The final array is: [20, 30, 40, 50]

Keep Exploring

Explore the Java Collections Framework and related topics in more depth on Javagoal.

Explore Collections in Java Back to Home Leave a Comment

ArrayDeque is a practical choice whenever you need fast insertion and removal at both ends of a collection. Because it is not thread-safe, it suits single-threaded use where speed matters, and its lack of null support keeps its behavior predictable. Use the constructors and methods above as a quick reference while you build and test your own examples.