Java Deque interface allows operations from both ends of a collection. In this chapter, you will learn about the Java Deque, its features, and how it supports insertion and deletion operations from both ends.
A Deque (double-ended queue) is a linear collection that supports insertion and deletion of elements from both ends. The name deque is an abbreviation for double-ended queue.
There is no fixed limit on the number of elements a deque can contain. However, this interface supports both:
The Deque interface provides various methods to insert, remove, and examine elements.
These methods generally exist in two forms:
The java.util.Deque interface provides methods to perform double-ended queue operations in Java. Its commonly used implementation class is java.util.ArrayDeque.
The following table lists the important methods of the Deque interface along with their descriptions.
| Methods | Description |
|---|---|
| add(E e) | This method is used to insert a specified element into the queue represented by the deque |
| addAll(Collection<? Extends E>c) | Adds all the elements in the specified collection at the end of the deque. |
| addFirst(E e) | Inserts the specified element at the front of the deque. |
| addLast(E e) | Inserts the specified element at the end of the deque. |
| contains(object o) | Returns true if the deque contains the specified element. |
| descendingIterator() | Returns an iterator over the elements in reverse sequential order. |
| element() | Retrieves the head of the queue represented by the deque. |
| getFirst() | Retrieves but does not remove the first element of the deque. |
| getLast() | Retrieves but does not remove the last element of the deque. |
| iterator() | Returns an iterator over the element in the deque in a proper sequence. |
| offer(E e) | Inserts the specified element into the deque, returning true upon success and false if no space is available. |
| offerFirst() | Inserts the specified element at the front of the deque unless it violates the capacity restriction. |
| offerLast() | Inserts the specified element at the end of the deque unless it violates the capacity restriction. |
| peek() | Retrieves but does not move the head of the queue represented by the deque or may return null if the deque is empty. |
| peekFirst() | Retrieves but does not move the first element of the deque or may return null if the deque is empty. |
| peekLast() | Retrieves but does not move the last element of the deque or may return null if the deque is empty. |
| poll() | Retrieves and remove the head of the queue represented by the deque or may return null if the deque is empty. |
| pollFirst() | Retrieves and remove the first element of the deque or may return null if the deque is empty. |
| pollLast() | Retrieves and remove the last element of the deque or may return null if the deque is empty. |
| pop() | Pops an element from the stack represented by the deque. |
| push() | Pushes an element onto the stack represented by the deque. |
| remove() | Retrieves and remove the head of the queue represented by the deque. |
| remove(Object o) | Removes the first occurrence of the specified element from the deque. |
| removeFirst() | Retrieves and remove the first element from the deque. |
| removeFirstOccurrence(Object o) | Remove the first occurrence of the element from the deque. |
| removeLast() | Retrieve and remove the last element from the deque. |
| removeLastOccurrence(Object o) | Remove the last occurrence of the element from the deque. |
| size() | Returns the total number of elements in the deque. |
The following examples demonstrate how to use the Deque interface and its methods for performing operations like insertion, deletion, and traversal.
In this example, we create a Deque using ArrayDeque, insert elements, traverse them, and perform pop and remove operations.
Output:
Inserting three elements : 1 2 3 After popping : 2 3 Removing the element 3 :[2]
In this example, we add elements at the front of the deque, check its size, and remove the last element.
Output:
The first element is : [Java] After adding the next element in the front of the deque : [Python, Java] The final deque is : [Python, Java, Ruby] The number of elements are : 3 Deque after removing the last element is given as : [Python, Java]
We request you to subscribe our newsletter for upcoming updates.