Deque in Java

Java 8Object Oriented ProgrammingProgramming

The dequeue is a double ended queue and data elements can be added or removed from either end. The dequeue in Java is implemented using the java.util.Deque interface which is a subtype of the java.util.Queue interface.

A program that demonstrates some of the methods of a dequeue is given as follows −

Example

 Live Demo

import java.util.*;
public class Example {
   public static void main(String[] args) {
      Deque<String> d = new LinkedList<String>();
      d.add("5");
      d.addFirst("1");
      d.addLast("9");
      d.push("7");
      d.offer("8");
      d.offerFirst("6");
      d.offerLast("2");
      System.out.println("The deque is: " + d);
      System.out.print("\nDequeue using standard Iterator: ");
      Iterator i = d.iterator();
      while (i.hasNext()) {
         System.out.print(i.next() + " ");
      }
      System.out.println("\n\nUsing peek, the element at head of the deque is: " + d.peek());
      System.out.println("The deque after peek: " + d);
      System.out.println("\nUsing pop, the element removed from the head of the deque is: " + d.pop());
      System.out.println("The deque after pop: " + d);
      System.out.println("\nDoes the deque contain element 8: " + d.contains("8"));
      d.removeFirst();
      d.removeLast();
      System.out.println("\nDeque after removing the first and last elements is: " + d);
   }
}

Output

The deque is: [6, 7, 1, 5, 9, 8, 2]

Dequeue using standard Iterator: 6 7 1 5 9 8 2

Using peek, the element at head of the deque is: 6
The deque after peek: [6, 7, 1, 5, 9, 8, 2]

Using pop, the element removed from the head of the deque is: 6
The deque after pop: [7, 1, 5, 9, 8, 2]

Does the deque contain element 8: true

Deque after removing the first and last elements is: [1, 5, 9, 8]

Now let us understand the above program.

Various functions are used to perform operations on a deque. The add(), offer(), offerLast() as well as the addLast() function adds an element to the deque tail. The addFirst(), offerFirst() as well as the push() function adds an element to the deque head. The code snippet that demonstrates this is given as follows.

d.add("5");
d.addFirst("1");
d.addLast("9");
d.push("7");
d.offer("8");
d.offerFirst("6");
d.offerLast("2");

Then the deque is printed. After that it is printed using a standard iterator. The code snippet that demonstrates this is given as follows.

System.out.println("The deque is: " + d);
System.out.print("\nDequeue using standard Iterator: ");
Iterator i = d.iterator();
while (i.hasNext()) {
System.out.print(i.next() + " ");
}

Then peek() is used to see the element at the head of the deque and pop() is used to remove the element at the head of the deque. The code snippet that demonstrates this is given as follows −

System.out.println("\n\nUsing peek, the element at head of the deque is: " + d.peek());
System.out.println("The deque after peek: " + d);
System.out.println("\nUsing pop, the element removed from the head of the deque is: " + d.pop());
System.out.println("The deque after pop: " + d);

The contains() function is used to check if an element is contained in the deque. The removeFirst() and removeLast() functions remove the first and last elements of the deque respectively. The code snippet that demonstrates this is given as follows −

System.out.println("\nDoes the deque contain element 8: " + d.contains("8"));
d.removeFirst();
d.removeLast();
System.out.println("\nDeque after removing the first and last elements is: " + d);
raja
Published on 07-Dec-2018 07:28:46
Advertisements