dsa

Java Deque Interface

In this tutorial, we will learn about Deque Interface and it's methods in Java.

Java Collection frameworks provide Deque Interface which works as both i.e, Stack(Last-In-First-Out) and Queue(First-In-First-Out).It is present in java.util package and it extends Queue Interface.The Deque works as double-ended queue that supports addition or removal of elements from either end of the data structure.

Derived classes of Deque Interface

We can not create objects of an interface so we need derived-classes to create objects. So Java provided us 2 derived classes for our use.They are :

  1. LinkedList
  2. ArrayDeque

Methods of Deque Interface

Some of the commonly used methods of the Queue interface are:

MethodsDescription
add(element)This method is used to add an element at the tail of the queue.
addFirst(element)This method is used to add an element at the head of the queue. The function returns true on successful insertion.
addLast(element)This method is used to add an element at the tail of the queue. The function returns true on successful insertion.
contains()This method is used to check whether the queue contains the given object or not.
descendingIterator()This method returns an iterator for the deque. The elements will be returned in order from last(tail) to first(head).
element()This method is used to retrieve, but not remove, the head of the queue represented by this deque.
getFirst()This method is used to retrieve, but not remove, the first element of this deque.
getLast()This method is used to retrieve, but not remove, the last element of this deque.
iterator()This method returns an iterator for the deque. The elements will be returned in order from first (head) to last (tail).
offer(element)This method is used to add an element at the tail of the queue.
offerFirst(element)This method is used to add an element at the head of the queue.
offerLast(element)This method is used to add an element at the tail of the queue.
peek()This method is used to retrieve the element at the head of the deque but doesn’t remove the element from the deque.
peekFirst()This method is used to retrieve the element at the head of the deque but doesn’t remove the element from the deque. This method returns null if the deque is empty.
peekLast()This method is used to retrieve the element at the tail of the deque but doesn’t remove the element from the deque. This method returns null if the deque is empty.
poll()This method is used to retrieve and remove the element at the head of the deque. This method returns null if the deque is empty.
pollFirst()This method is used to retrieve and remove the element at the head of the deque. This method returns null if the deque is empty.
pollLast()This method is used to retrieve and remove the element at the tail of the deque. This method returns null if the deque is empty.
pop()This method is used to remove an element from the head and return it.
push(element)This method is used to add an element at the head of the queue.
removeFirst()This method is used to remove an element from the head of the queue.
removeLast()This method is used to remove an element from the tail of the queue.
size()This method is used to find and return the size of the deque.