Deque in Java

Interface Deque (short for Double-Ended Queue) in Java is an interface that defines a data structure that allows us to add or remove elements at the front or rear:

This Deque interface extends from Queue interface and it has 2 main implementations: ArrayQueue and LinkedList as follows:

We can use Deque to implement:

Stack with LIFO (Last In First Out): add elements and remove elements from the front
or Queue with FIFO (First In First Out): add elements from the rear, remove elements from the front

To work with Deque, you can use some of its methods as follows:

addFirst() and addLast() methods

  • addFirst(E e): add element at the front.
  • addLast(E e): add element at the rear.For example:

The add() method is similar to the addLast() method! The push() method is similar to the addFirst() method!

In the above example, initially, my deque only has 3 elements. Using the addFirst() and addLast() methods, I can add more elements to the beginning and end. The result is as follows:

offerFirst() and offerLast() methods

The two methods offerFirst() and offerLast() are similar to the two methods addFirst() and addLast(), but the difference is that if your collection has capacity. Adding new elements may not be possible when your collection is full capacity.

offerFirst(E e): add elements to the front, if it cannot be added, it will return false, otherwise it will return true.

offerLast(E e): add elements to the rear, similar to offerFirst(), it will return false if it cannot be added, otherwise it will return true.

With the two implementations ArrayQueue or LinkedList, the size of the collection will automatically increase, so when you use the two methods offerFirst() and offerLast(), the result will always return true!

For example:

Result:

As you can see, although I only initialized the ArrayDeque object with a capacity of 1, this object can automatically increase its capacity and save all the elements that I added using the offer methods, without any errors.

removeFirst(), pollFirst(), removeLast() and pollLast() methods

The two methods removeFirst() and pollFirst() remove and return the element at the beginning of the collection. The difference is that if there is no element to remove, removeFirst() will throw java.util.NoSuchElementException, while pollFirst() will return null.

Similarly, the methods removeLast() and pollLast() remove and return the element at the end of the collection. The difference is that if there is no element to remove, removeLast() will throw java.util.NoSuchElementException, while pollLast() will return null.

For example:

Result:

The poll() method is similar to the pollFirst() method!

getFirst(), peekFirst(), getLast() and peekLast() methods

The getFirst() and peekFirst() methods will only return the element at the front without removing it. The difference is that if there is no element to return, getFirst() will throw java.util.NoSuchElementException, while peekFirst() will return null.

The same goes for the getLast() and peekLast() methods!

For example:

Result:

 

Add Comment