Deque is a Queue in which you can add and remove elements from both sides. In the Java Queue tutorial we have seen that the Queue follows FIFO (First in First out) and in PriorityQueue example we have seen how to remove and add elements based on the priority. In this tutorial, we will see how to use Deque.
Deque is an interface and has two implementations: LinkedList
and ArrayDeque
. By implementation I refer that these classes LinkedList
and ArrayDeque
implements Deque interface, so we can create the instance of these and assign it to the Deque like this:
Deque dq = new LinkedList(); Deque dq = new ArrayDeque();
Java Deque Interface Example
import java.util.Deque; import java.util.ArrayDeque; public class ArrayDequeExample { public static void main(String[] args) { /* * We cannot create instance of a Deque as it is an * interface, we can create instance of ArrayDeque or * LinkedList and assign it to Deque */ Deque<String> dq = new ArrayDeque<String>(); /* * Adding elements to the Deque. * addFirst() adds element at the beginning * and addLast() method adds at the end. */ dq.add("Glenn"); dq.add("Negan"); dq.addLast("Maggie"); dq.addFirst("Rick"); dq.add("Daryl"); System.out.println("Elements in Deque:"+dq); /* * We can remove element from Deque using remove() method, * we can use normal remove() method which removes first * element or we can use removeFirst() and removeLast() * methods to remove first and last element respectively. */ System.out.println("Removed element: "+dq.removeLast()); /* * element() method - returns the head of the * Deque. Head is the first element of Deque */ System.out.println("Head: "+dq.element()); /* * pollLast() method - this method removes and returns the * tail of the Deque(last element). Returns null if the Deque is empty. * We can also use poll() or pollFirst() to remove the first element of * Deque. */ System.out.println("poll(): "+dq.pollLast()); /* * peek() method - it works same as element() method, * however it returns null if the Deque is empty. We can also use * peekFirst() and peekLast() to retrieve first and last element */ System.out.println("peek(): "+dq.peek()); //Again printing the elements of Deque System.out.println("Elements in Deque:"+dq); } }
Output:
Elements in Deque:[Rick, Glenn, Negan, Maggie, Daryl] Removed element: Daryl Head: Rick poll(): Maggie peek(): Rick Elements in Deque:[Rick, Glenn, Negan]
When to use ArrayList and when to use ArrayDeque?
ArrayDeque has the ability to add or remove the elements from both ends (head or tail), on the other hand removing last element from ArrayList takes O(n) time as it traverses the whole list to reach the end. So if you want to add or remove elements from both ends choose ArrayDeque over ArrayList, however if you only want to perform the opreation on the tail (at the end) then you should choose ArrayList.
Methods of Deque interface
void addFirst(E e): Inserts the specified element at the beginning of the Deque.
void addLast(E e): Inserts the specified element at the end of the Deque.
boolean contains(Object o): Returns true if the specified element is present in the Deque.
E getFirst(): It returns the first element of the Deque.
E getLast(): It returns the last element of the Deque.
E peekFirst(): Returns the first element of Deque, or null if the Deque is empty.
E peekLast(): Returns the last element of Deque, or null if the Deque is empty.
E pollFirst(): Returns and removes the first element of Deque, or null if the Deque is empty.
E pollLast(): Returns and removes the last element of Deque, or null if the Deque is empty.
int size(): Returns the number of elements present in Deque.
Deque Official Documentation
Leave a Reply