CodingBison

A Deque interface is one of the widely used Collection interfaces. Deque interface as shown in the below figure extends Queue interface, and hence it inherits all the operations from Queue interface. Deque, in general is a very popular data structure and is used in many scenarios. The name "deque" is a short form of "double ended queue", this means insertions and deletion can be done at both the ends.



Figure: Queue and Deque Interface

It might be a good idea to familiarize your self with Queue interface, before getting into the details of the "Deque".

Things To Remember
Insertion of "null" elements is not allowed into the Deque.

Methods defined in Deque interface

The Deque interface inherits all the methods from the Collection and Queue interfaces, but as we know Deque has its own constraints/properties that are different from the other Collection interfaces. So, Deque interface defines few methods, on top of the ones it inherits from its parent interface.

Deque interface offers two sets of methods for the same functionality. One set of methods throws an "exception" on failure while the other set does not throw an exception, but instead it lets the caller know the result by returning an appropriate value.


Table: Generic Methods of Deque Interface
MethodDescription
boolean addFirst(E e)Inserts the specified element "e" at the front of the Deque immediately. Returns True on success and throws an Exception if the Deque is full. Throws "illegalStateException" if the Deque is full.
E removeFirst()Retrieves and removes the first element of the Deque immediately. Returns the "removed element" on success and throws an Exception if the Deque is empty. Throws "NoSuchElementException" if the Deque is empty.
E getFirst()Retrieves, but "does not" remove the first element of the Deque immediately. Returns the "retrieved element" on success and throws an Exception if the Deque is empty. Throws "NoSuchElementException" if the Deque is empty.
boolean offerFirst(E e)Inserts the specified element "e" into the front of the Deque immediately. Returns True on success and false if the Deque is full.
E pollFirst()Retrieves and removes the element at the front of the Deque immediately. Returns the "removed element" on success or "null" if the Deque is empty.
E peekFirst()Retrieves, but "does not" remove the element at the front of the Deque immediately. Returns the "retrieved element" on success or "null" if the Deque is empty.
boolean addLast(E e)Inserts the specified element "e" at the end of the Deque immediately. Returns True on success and throws an Exception if the Deque is full. Throws "illegalStateException" if the Deque is full.
E removeLast()Retrieves and removes the element from end of the Deque immediately. Returns the "removed element" on success and throws an Exception if the Deque is empty. Throws "NoSuchElementException" if the Deque is empty.
E getLast()Retrieves, but "does not" remove the last element of the Deque immediately. Returns the "retrieved element" on success and throws an Exception if the Deque is empty. Throws "NoSuchElementException" if the Deque is empty.
boolean offerLast(E e)Inserts the specified element "e" at the end of the Deque immediately. Returns True on success and false if the Deque is full.
E pollLast()Retrieves and removes the element from the end of the Deque immediately. Returns the "removed element" on success or "null" if the Deque is empty.
E peekLast()Retrieves, but "does not" remove the element from the end of the Deque immediately. Returns the "retrieved element" on success or "null" if the Deque is empty.

Here is an example program that demonstrates the usage of the above mentioned methods. The example have used LinkedList, one of the implementation classes of Deque and demonstrated the usage of the methods defined by the Deque interface. LinkedList implements List interface and also Queue/Deque interfaces, LinkedList is discussed in detail here understanding LinkedList.

 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.Deque;;

 public class DequeInterfaceDemo {
     public static void main(String[] args) {
         Deque dqobj = new LinkedList();
         dqobj.addFirst(200);
         dqobj.addFirst(300);
         dqobj.addLast("e3");
         System.out.println(" All Deque Elements: " + dqobj);

         Iterator iterobj = dqobj.iterator();
         System.out.println(" Retrieve/remove all the elements of the Deque");
         while(iterobj.hasNext()) {
             System.out.println("\t Deque element " + dqobj.pollFirst());
         }

         System.out.println(" Try Retrieving the element from the Deque");
         try {
             System.out.println(" First Element: " + dqobj.getFirst());
         } catch (Exception e) {
             System.out.println(" Exception Handled: Deque Empty !");
         }

         System.out.println(" First Element: " + dqobj.peekFirst());
     }
 }

The above program demonstrates usage of the methods defined in the Deque interface. As we know, Deque interface offers two sets of methods for the same functionality, one set which throws exceptions while the other set of methods does not. As you can in the output below, the methods getFirst and peekFirst does the same thing, tries to retrieve the element, however the method "getFirst" throws an exception when the Deque is empty, and hence the getFirst() method, in the above program is guarded with a try/catch block. Also, notice the output below, the method peekFirst() returns null when the Queue is empty.

Let us Compile/run the program, Here is the output:

  All Deque Elements: [300, 200, e3]
  Retrieve/remove all the elements of the Deque
 	 Deque element 300
 	 Deque element 200
 	 Deque element e3
  Try Retrieving the element from the Deque
  Exception Handled: Deque Empty !
  First Element: null




comments powered by Disqus