ArrayDeque Class in Java

In this tutorial, we will learn about ArrayDeque class in Java and how to use it

ArrayDeque implements both Deque interface and Queue interface and inherits their properties, where Queue follows FIFO data structure and Deque allows user to add/remove from both ends.

It is also known as Array Double Ended Queue. It allows users to add or remove an element from both sides of the queue.

ArrayDeque Declaration:

Below is the ArrayDeque class present in the JDK 

public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E>, Cloneable, Serializable {

    transient Object[] elements;
    transient int head;
    transient int tail;
    private static final int MIN_INITIAL_CAPACITY = 8;
    private static final long serialVersionUID = 2340985798034038923L;

    ....methods
}

You can see ArrayDeque implements Deque Interface and it further extends Queue Interface.

ArrayDeque Methods:

Here are some frequently used methods of ArrayDeque Class in Java.

METHODDESCRIPTION
add()Inserts the element at the end of the deque.
addFirst()Inserts the element at the beginning of the deque. Throws an exception if the deque is full.
addLast()Inserts the element at the end of the deque. Throws an exception if the deque is full.
removeFirst()Returns and removes the first element of the deque. Throws an exception if the deque is empty.
removeLast()Returns and removes the last element of the deque. Throws an exception if the deque is empty.
clear() The method removes all deque elements.
clone()The method copies the deque.
getFirst()Returns the first element of the deque. Throws an exception if the deque is empty.
getLast()Returns the last element of the deque. Throws an exception if the deque is empty.
element() The method returns the element at the head of the deque
peek()The method returns head element without removing it.
peekFirst()Returns the first element of the deque. Returns null if the deque is empty.
peekLast()Returns the last element of the deque. Returns null if the deque is empty.
offerFirst()Inserts the element at the beginning of the deque. Returns false if the deque is full.
offerLast()Inserts the element at the end of the deque. Returns false if the deque is full.
poll()The method returns head element and also removes it
pollFirst()Returns and removes the first element of the deque. Returns null if the deque is empty.
pollLast()Returns and removes the last element of the deque. Returns null if the deque is empty.
pop()It pops out an element for stack represented by deque
size()Returns the number of elements in this deque.
toArray()Returns an array containing all of the elements in this deque in proper sequence (from first to the last element).

Example:

Let’s see some examples of Array Deque in Java.

public class ArrayDequeExample {
    public static void main(String[] args) {

        // Creating String type ArrayDeque
        ArrayDeque<String> deque = new ArrayDeque<>();

        // Adding Elements
        deque.add("Peter");
        deque.add("Olive");
        deque.add("Smith");

        // Printing elements
        System.out.println(deque);

        // Printing First Element
        System.out.println("First Element == " + deque.getFirst());
    }
}

Output:

[Peter, Olive, Smith]
First Element == Peter

How to Perform Operations In Vector?

Let’s see some examples of adding, accessing, and removing elements from Array Deque in Java.

1. Adding Elements: To add elements, we can use the methods  add(), addFirst(), addLast(), offer(), offerFirst(), offerLast() methods.

public class ArrayDequeExample {
    public static void main(String[] args) {

        ArrayDeque<String> animals = new ArrayDeque<>();

        // Adding elements with add methods
        animals.add("Dog");
        animals.addFirst("Cat");
        animals.addLast("Horse");

        System.out.println("Printing: " + animals);

        // Adding elements with Offer methods
        animals.offer("Lion");
        animals.offerFirst("Panda");
        animals.offerLast("Squirrel");

        System.out.println("Printing: " + animals);
    }
}

Output:

Printing: [Cat, Dog, Horse]
Printing: [Panda, Cat, Dog, Horse, Lion, Squirrel]

Note: If the array deque is full, all these methods add(), addFirst() and addLast() throws IllegalStateException.

However, offer(), offerFirst() and offerLast() returns false if array deque is full.

2. Accessing the Elements: To access the elements, we can use methods like getFirst(), getLast(), peek(), peekFirst(), etc.

public class ArrayDequeExample {
    public static void main(String[] args) {
        ArrayDeque<String> animals = new ArrayDeque<>();

        // Adding elements with Offer methods
        animals.offer("Lion");
        animals.offerFirst("Panda");
        animals.offerLast("Squirrel");

        System.out.println("Complete Array Deque: " + animals);

        // Getting elements with Get Methods
        System.out.println("First Element: " + animals.getFirst());

        System.out.println("Last Element: " + animals.getLast());

        // Getting elements with Peek Methods
        System.out.println("Head Element: " + animals.peek());

        System.out.println("First Element: " + animals.peekFirst());

        System.out.println("Last Element: " + animals.peekLast());
    }
}

Output:

Complete Array Deque: [Panda, Lion, Squirrel]
First Element: Panda
Last Element: Squirrel
Head Element: Panda
First Element: Panda
Last Element: Squirrel

Note: If the array deque is empty, getFirst() and getLast() throws NoSuchElementException.

However, peek(), peekFirst(), peekLast() return null if the element is not found.

3. Removing Elements:  To remove an element from a deque, we can use methods like poll(), pop(), pollFirst(), pollLast() methods. If we wish to remove elements from both the ends, the can use methods such as removeFirst(), removeLast().

public class ArrayDequeExample {
    public static void main(String[] args) {
        ArrayDeque<String> animals = new ArrayDeque<>();

        // Adding elements with Offer methods
        animals.offer("Dog");
        animals.offer("Horse");
        animals.offer("Lion");
        animals.offerFirst("Panda");
        animals.offerLast("Squirrel");

        System.out.println("Complete Array Deque: " + animals);

        System.out.println("Removed Element: " +  animals.remove());

        System.out.println("New Array Deque: " + animals);

        System.out.println("Removed First Element: " + animals.removeFirst());

        System.out.println("Removed Last Element: " + animals.removeLast());

        System.out.println("Final Array Deque: " + animals);
    }
}

Output:

Complete Array Deque: [Panda, Dog, Horse, Lion, Squirrel]
Removed Element: Panda
New Array Deque: [Dog, Horse, Lion, Squirrel]
Removed First Element: Dog
Removed Last Element: Squirrel
Final Array Deque: [Horse, Lion]

Note: If the array deque is empty, remove(), removeFirst() and removeLast() throws an exception.

However, poll(), pollFirst(), and pollLast() returns null if the element is not found.

Official Docs: Read Here

ArrayDeque Vs. LinkedList

Both Array Deque and LinkedList implement the Deque interface but there are some differences.

  • LinkedList supports null elements, whereas Array Deque doesn’t.
  • LinkedList requires more storage than Array Deque because each node in a linked list contains links to other nodes.
  • LinkedList consumes more memory than the Array Deque
  • Array Deque is faster than a LinkedList.

I hope you’ve learned a lot from this article. Don’t forget to share it.

Deque Interface in Java

HashSet In Java

Leave a Comment