Skip to main content

Deque

tip

ArrayDeque implements Deque.

  • A deque (usually pronounced “deck”) stands for double ended queue and is a combination dfpoksd of a stack and a queue, in that it supports O(1) insertions and deletions from both the front and the back of the deque. In Java, the deque class is called ArrayDeque. The four methods for adding and removing are offerFirst(), pollFirst(), offerLast(), and pollLast().

Relation between Deque & ArrayDeque

note

As we mentioned before, Deque<E> extends Queue<E> and represents a queue where you can insert and remove elements from both ends. It combines access rules provided by queue (FIFO) and stack (LIFO) together.

The Deque interface provides methods for working with the first and the last element of a queue. Some of the methods throw an exception, while others just return a special value (null). Check out the table:

Since ArrayDeque and LinkedList implement this interface, they both can work as a queue (FIFO), a stack (LIFO), or a deque.