Deque & ArrayDeque

Java Deque Interface and ArrayDeque

Last Updated : 28 Mar 2026

Java Deque is used to insert and remove elements from both ends of a collection that allows it to work as both a stack and a queue.

In this chapter, you will learn about the Java Deque interface, its methods, key features, and how it works as a double-ended queue. You will also understand how to use the ArrayDeque class to implement Deque with practical examples in Java.

What is Java Deque Interface?

The Deque interface is a part of the java.util package and is a subtype of the Queue interface. Deque stands for "double-ended queue," which allows insertion and deletion of elements from both the front and the rear. It can function as both a stack (LIFO) and a queue (FIFO) that makes it a flexible data structure for handling elements efficiently from both ends.

Deque Interface Declaration

The Deque interface is declared as an interface that extends the Queue interface.

Here is the syntax:

Methods of Java Deque Interface

The following table shows the commonly used methods of the Deque interface:

MethodDescription
boolean add(object)It is used to insert the specified element into this deque and return true upon success.
boolean offer(object)It is used to insert the specified element into this deque.
Object remove()It is used to retrieve and removes the head of this deque.
Object poll()It is used to retrieve and removes the head of this deque, or returns null if this deque is empty.
Object element()It is used to retrieve, but does not remove, the head of this deque.
Object peek()It is used to retrieve, but does not remove, the head of this deque, or returns null if this deque is empty.
Object peekFirst()The method returns the head element of the deque. The method does not remove any element from the deque. Null is returned by this method, when the deque is empty.
Object peekLast()The method returns the last element of the deque. The method does not remove any element from the deque. Null is returned by this method, when the deque is empty.
Boolean offerFirst(e)Inserts the element e at the front of the queue. If the insertion is successful, true is returned; otherwise, false.
Object offerLast(e)Inserts the element e at the tail of the queue. If the insertion is successful, true is returned; otherwise, false.

Key Features of Deque Interface:

Several key features of deque interface are as follows:

Double-Ended Operations: Deque supports operations that allow elements to be added or removed from both the front and rear of the queue. These operations include addFirst(E e), addLast(E e), removeFirst(), and removeLast().

Accessors: Deque provides methods to access elements from both ends without removing them. These methods include getFirst() and getLast().

Special Insertion and Removal Operations: Apart from regular insertion and removal operations, Deque offers specialized operations such as offerFirst(E e) and offerLast(E e) for inserting elements, and pollFirst() and pollLast() for removing elements.

ArrayDeque Class

In Java, we cannot create objects of an interface directly. So, to use the Deque interface, we use the ArrayDeque class, which implements it.

ArrayDeque is a resizable array that can grow or shrink as needed. It also extends the AbstractCollection class.

Some of the key features of ArrayDeque are:

  • Elements can be added or removed from both ends (front and rear).
  • Null elements are not allowed.
  • ArrayDeque is not thread-safe.
  • It has no fixed size limit (no capacity restrictions).
  • It is faster than LinkedList and Stack in most cases.

ArrayDeque Hierarchy

The hierarchy of ArrayDeque class is given in the figure displayed at the right side of the page. The following image shows the hierarchy of the ArrayDeque class:

java arraydeque hierarchy

ArrayDeque Class Declaration

The ArrayDeque class is declared as a class that extends AbstractCollection and implements Deque, Cloneable, and Serializable interfaces.

Here is the syntax:

Java ArrayDeque Example

Here are some examples of the Java ArrayDeque class to understand its usage.

Example 1: Simple ArrayDeque Example

In this example, we are creating an ArrayDeque, adding elements, and traversing them.

Output:

Example 2: offerFirst() and pollLast() Methods

In this example, we are using offerFirst() to add elements at the beginning and pollLast() to remove elements from the end.

Output:

After offerFirst Traversal...
jai
arvind
vimal
mukul
After pollLast() Traversal...
jai
arvind
vimal

Example 3: Storing Objects in ArrayDeque

In this example, we are storing user-defined Book objects in an ArrayDeque.

Output:

101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6