Dequeue Interface in Java

Dequeue

In the previous blog, we learned about Queue Interface in Java. If you want to know more about it visit Queue Interface in Java. In this blog, we will go through the subtype Queue Interface i.e. Dequeue interface.


Dequeue Interface

Dequeue stands for double-ended Queue, meaning we can add or delete elements from both ends of the data structure. Due to this reason, Dequeue can be used as a stack or queue. As we know the stack follows the Last in First Out (LIFO) approach and the queue follows the First in First Out (FIFO) approach. It supports both approaches. Dequeue is present in java.util package.


Syntax of Dequeue

public interface Deque extends Queue

Working of Dequeue

As we have already seen in our previous blog, Queue elements are added from rear and deleted from front. The advantage of Deueue is that we can insert or remove elements from both front and rear.

...

Program to demonstrate Dequeue

import java.util.*;
public class DequeExample {
  public static void main(String[] args){
    Deque<String> str = new LinkedList<String>();
    str.add("Element 1 (Tail)");
    str.addFirst("Element 2 (Head)");
    str.addLast("Element 3 (Tail)");
    System.out.println(str);
  }
}

Output

[Element 2 (Head), Element 1 (Tail), Element 3 (Tail)]

Methods of Dequeue

  1. add()
  2. add() method inserts or adds the element to the end of the Dequeue only if there is space. But if the Dequeue is full and there is no space for the element, it throws an exception called IllegalStateException. It returns true when the insertion or addition is successful.

    e.g.

    import java.util.*;
    public class DequeueAddExample {
      public static void main(String[] args)throws IllegalStateException{
        Deque<String> dequeue
          = new LinkedList<String>();
        dequeue.add("Java");
        dequeue.add("React");
        dequeue.add("JS");
        System.out.println("Deque: " + dequeue);
      }
    }

    Output

    Deque: [Java, React, JS]
  3. addFirst()
  4. addFirst() method adds or inserts the element passed in the parameter to the front of the Deque if there is space. But if the Dequeue is full and there is no space for the element, it throws an exception called IllegalStateException. It returns true when the insertion or addition is successful.

    e.g.

    import java.util.*;
    public class DequeueAddFirstExample {
      public static void main(String[] args)throws IllegalStateException{
        Deque<String> dequeue
          = new LinkedList<String>();
        dequeue.addFirst("Java");
        dequeue.addFirst("React");
        dequeue.addFirst("JS");
        System.out.println("Deque: " + dequeue);
      }
    }

    Output

    Deque: [JS, React, Java]
  5. addLast()
  6. addLast() method inserts or adds the element to the end of the Dequeue only if there is space. But if the Dequeue is full and there is no space for the element then it throws an exception called IllegalStateException. It returns true when the insertion or addition is successful.

    e.g.

    import java.util.*;
    public class DequeueAddLastExample {
      public static void main(String[] args)throws IllegalStateException{
        Deque<String> dequeue
          = new LinkedList<String>();
        dequeue.add("Java");
        dequeue.add("React");
        dequeue.add("JS");
        System.out.println("Deque: " + dequeue);
      }
    }

    Output

    Deque: [Java, React, JS]
  7. offer()
  8. offer() method adds or inserts an element at the tail of the queue. This method is the same as add() method but the difference is, it does not throw an exception when the capacity of the container is full instead it returns false.

    e.g.

    import java.util.*;
    public class DequeueOfferExample {
      public static void main(String[] args)throws IllegalStateException{
        Deque<Integer> dequeue = new LinkedList<Integer>();
        dequeue.offer(30);
        dequeue.offer(90);
        dequeue.offer(50);
        dequeue.offer(25);
        dequeue.offer(75);
        System.out.println("Deque: " + dequeue);
      }
    }

    Output

    Deque: [30, 90, 50, 25, 75]
  9. offerFirst()
  10. offerFirst() method adds or inserts an element at the head of the queue. This method is the same as the addFirst() method but only the difference is, this method does not throw an exception when the capacity of the container is full instead it returns false.

    e.g.

    import java.util.*;
    import java.util.concurrent.LinkedBlockingDeque;
    public class DequeueOfferFirstExample {
      public static void main(String[] args)    throws IllegalStateException{
        Deque<String> dequeue    = new LinkedBlockingDeque<String>(4);
        if (dequeue.offerFirst("Java"))
          System.out.println("Java is inserted");
        else
          System.out.println("The Deque is full");
    
        if (dequeue.offerFirst("React"))
          System.out.println("React is inserted");
        else
          System.out.println("The Deque is full");
    
        if (dequeue.offerFirst("JS"))
          System.out.println("JS is inserted");
        else
          System.out.println("The Deque is full");
    
        if (dequeue.offerFirst("Angular"))
          System.out.println("Angular is inserted");
        else
          System.out.println("The Deque is full");
    
        if (dequeue.offerFirst("PHP"))
          System.out.println("PHP is inserted");
        else
          System.out.println("The Deque is full");
    
        System.out.println("Deque: " + dequeue);
      }
    }

    Output

    Java is inserted
    React is inserted
    JS is inserted
    Angular is inserted
    The Deque is full
    Deque: [Angular, JS, React, Java]
  11. offerLast()
  12. offerLast() method adds or inserts an element at the tail of the queue. This method is the same as add() method but the only difference is, this method does not throw an exception when the capacity of the container is full instead it returns false.

    e.g.

    import java.util.*;
    public class DequeueOfferLastExample {
      public static void main(String[] args)throws IllegalStateException{
        Deque<String> dequeue= new LinkedList<String>();
        dequeue.offerLast("Angular");
        dequeue.offerLast("JS");
        dequeue.offerLast("React");
        dequeue.offerLast("PHP");
        System.out.println("Deque: " + dequeue);
      }
    }

    Output

    Deque: [Angular, JS, React, PHP]
  13. getFirst()
  14. getFirst() method returns the first element of the deque. If the deque is empty then it throws an exception

    e.g.

    import java.util.*;
    public class DequeueGetFirstExample {
      public static void main(String[] args) throws IllegalStateException{
        Deque<String> dequeue= new LinkedList<String>();
        dequeue.add("Angular");
        dequeue.add("JS");
        dequeue.add("React");
        dequeue.add("PHP");
        System.out.println("Deque: " + dequeue);
        System.out.println("Deque getFirst() method: " + dequeue.getFirst());
      }
    }

    Output

    Deque: [Angular, JS, React, PHP]
    Deque getFirst() method: Angular
  15. getLast()
  16. getLast() returns the last element of the deque. If the deque is empty, an exception is thrown.

    e.g.

    import java.util.*;
    public class DequeueGetLastExample {
      public static void main(String[] args)throws IllegalStateException{
        Deque<String> dequeue= new LinkedList<String>();
        dequeue.add("Angular");
        dequeue.add("JS");
        dequeue.add("React");
        dequeue.add("PHP");
        System.out.println("Deque: " + dequeue);
        System.out.println("Deque getLast() method: " + dequeue.getLast());
      }
    }

    Output

    Deque: [Angular, JS, React, PHP]
    Deque getLast() method: PHP
  17. peekFirst()
  18. peekFirst() returns the first element of the deque. If the dequeue is empty then it returns null

    e.g.

    import java.util.*;
      public class DequeuePeekFirstExample {
        public static void main(String[] args)
            throws IllegalStateException
        {
             Deque<String> dequeue= new LinkedList<String>();
            dequeue.add("Angular");
            dequeue.add("JS");
            dequeue.add("React");
            dequeue.add("PHP");
             System.out.println("Deque: " + dequeue);
             System.out.println("Deque peekFirst() method: " + dequeue.peekFirst());
        }
      }

    Output

    Deque: [Angular, JS, React, PHP]
      Deque peekFirst() method: Angular
  19. peekLast()
  20. peekLast() returns the last element of the deque. If the dequeue is empty then it returns null.

    e.g.

    import java.util.*;
    public class DequeuePeekLastExample {
      public static void main(String[] args)throws IllegalStateException{
        Deque<String> dequeue= new LinkedList<String>();
        dequeue.add("Angular");
        dequeue.add("JS");
        dequeue.add("React");
        dequeue.add("PHP");
        System.out.println("Deque: " + dequeue);
        System.out.println("Deque peekLast() method: " + dequeue.peekLast());
      }
    }

    Output

    Deque: [Angular, JS, React, PHP]
    Deque peekLast() method: PHP
  21. removeFirst()
  22. removeFirst() method returns and removes the first element of the deque. If the deque is empty, an exception is thrown.

    e.g.

    import java.util.*;
    public class DequeueRemoveFirstExample {
      public static void main(String[] args)throws IllegalStateException{
        Deque<String> dequeue= new LinkedList<String>();
        dequeue.add("Angular");
        dequeue.add("JS");
        dequeue.add("React");
        dequeue.add("PHP");
        System.out.println("Deque: " + dequeue);
        System.out.println("Deque removeFirst() method: " + dequeue.removeFirst());
        System.out.println("Deque After using removeFirst() method : " + dequeue);
      }
    }

    Output

    Deque: [Angular, JS, React, PHP]Deque removeFirst() method: Angular
    Deque After using removeFirst() method : [JS, React, PHP]
  23. removeLast()
  24. removeLast() method returns and removes the last element of the deque. If the deque is empty, an exception is thrown.

    e.g.

    import java.util.*;
    public class DequeueRemoveLastExample {
      public static void main(String[] args)throws IllegalStateException{
        Deque<String> dequeue= new LinkedList<String>();
        dequeue.add("Angular");
        dequeue.add("JS");
        dequeue.add("React");
        dequeue.add("PHP");
        System.out.println("Deque: " + dequeue);
        System.out.println("Deque removeFirst() method: " + dequeue.removeLast());
        System.out.println("Deque After using removeLast() method : " + dequeue);
      }
    }

    Output

    Deque: [Angular, JS, React, PHP]
    Deque removeFirst() method: PHP
    Deque After using removeLast() method : [Angular, JS, React]
  25. pollFirst()
  26. pollFirst() returns and removes the first element of the deque. If the deque is empty, the method returns null.

    e.g.

    import java.util.*;
    public class DequeuePollFirstExample {
      public static void main(String[] args)throws IllegalStateException{
        Deque<String> dequeue= new LinkedList<String>();
        dequeue.add("Angular");
        dequeue.add("JS");
        dequeue.add("React");
        dequeue.add("PHP");
        System.out.println("Deque: " + dequeue);
        System.out.println("Deque pollFirst() method: " + dequeue.pollFirst());
        System.out.println("Deque After using pollFirst() method : " + dequeue);
      }
    }

    Output

    Deque: [Angular, JS, React, PHP]
    Deque pollFirst() method: Angular
    Deque After using pollFirst() method : [JS, React, PHP]
  27. pollLast()
  28. pollLast() returns and removes the last element of the deque. It returns null if the deque is empty.

    e.g.

    import java.util.*;
    public class DequeuePollLastExample {
      public static void main(String[] args)throws IllegalStateException{
        Deque<String> dequeue= new LinkedList<String>();
        dequeue.add("Angular");
        dequeue.add("JS");
        dequeue.add("React");
        dequeue.add("PHP");
        System.out.println("Deque: " + dequeue);
        System.out.println("Deque pollLast() method: " + dequeue.pollLast());
        System.out.println("Deque After using pollLast() method : " + dequeue);
      }
    }

    Output

    Deque: [Angular, JS, React, PHP]
    Deque pollLast() method: PHP
    Deque After using pollLast() method : [Angular, JS, React]

Core Java Tutorial

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.