The java.util.Deque interface is a subtype of the java.util.Queue interface. The Deque is related to the double-ended queue that supports addition or removal of elements from either end of the data structure, it can be used as a queue (first-in-first-out/FIFO) or as a stack (last-in-first-out/LIFO). These are faster than Stack and LinkedList.
This is the hierarchy of Deque interface in Java:
Few important features of Deque are:
Methods of Deque:
Summarizing the methods, we get:
Below program illustrates few operations of Deque interface:
// Java program to demonstrate working of
// Deque in Java
import java.util.*;
public class DequeExample
{
public static void main(String[] args)
{
Deque deque = new LinkedList();
// We can add elements to the queue in various ways
deque.add("Element 1 (Tail)"); // add to tail
deque.addFirst("Element 2 (Head)");
deque.addLast("Element 3 (Tail)");
deque.push("Element 4 (Head)"); //add to head
deque.offer("Element 5 (Tail)");
deque.offerFirst("Element 6 (Head)");
deque.offerLast("Element 7 (Tail)");
System.out.println(deque + "\n");
// Iterate through the queue elements.
System.out.println("Standard Iterator");
Iterator iterator = deque.iterator();
while (iterator.hasNext())
System.out.println("\t" + iterator.next());
// Reverse order iterator
Iterator reverse = deque.descendingIterator();
System.out.println("Reverse Iterator");
while (reverse.hasNext())
System.out.println("\t" + reverse.next());
// Peek returns the head, without deleting
// it from the deque
System.out.println("Peek " + deque.peek());
System.out.println("After peek: " + deque);
// Pop returns the head, and removes it from
// the deque
System.out.println("Pop " + deque.pop());
System.out.println("After pop: " + deque);
// We can check if a specific element exists
// in the deque
System.out.println("Contains element 3: " +
deque.contains("Element 3 (Tail)"));
// We can remove the first / last element.
deque.removeFirst();
deque.removeLast();
System.out.println("Deque after removing " +
"first and last: " + deque);
}
}
Output:
[Element 6 (Head), Element 4 (Head), Element 2 (Head), Element 1 (Tail), Element 3 (Tail), Element 5 (Tail), Element 7 (Tail)] Standard Iterator Element 6 (Head) Element 4 (Head) Element 2 (Head) Element 1 (Tail) Element 3 (Tail) Element 5 (Tail) Element 7 (Tail) Reverse Iterator Element 7 (Tail) Element 5 (Tail) Element 3 (Tail) Element 1 (Tail) Element 2 (Head) Element 4 (Head) Element 6 (Head) Peek Element 6 (Head) After peek: [Element 6 (Head), Element 4 (Head), Element 2 (Head), Element 1 (Tail), Element 3 (Tail), Element 5 (Tail), Element 7 (Tail)] Pop Element 6 (Head) After pop: [Element 4 (Head), Element 2 (Head), Element 1 (Tail), Element 3 (Tail), Element 5 (Tail), Element 7 (Tail)] Contains element 3: true Deque after removing first and last: [Element 2 (Head), Element 1 (Tail), Element 3 (Tail), Element 5 (Tail)]
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
The add(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion.
Syntax:
boolean add(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the end of the Deque.
Returns: This method returns true on successful insertion.
Exceptions: The function throws four exceptions which are described as below:
Below programs illustrate add() method of Deque:
Program 1: With the help of LinkedList.
// Java Program Demonstrate add()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of De1ue
Deque DQ
= new LinkedList();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// before removing print Deque
System.out.println("Deque: " + DQ);
}
}
Output:
Deque: [7855642, 35658786, 5278367, 74381793]
Program 2: With the help of ArrayDeque.
// Java Program Demonstrate add()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of De1ue
Deque DQ
= new ArrayDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// before removing print Deque
System.out.println("Deque: " + DQ);
}
}
Output:
Deque: [7855642, 35658786, 5278367, 74381793]
Program 3: With the help of ConcurrentLinkedDeque.
// Java Program Demonstrate add()
// method of Deque
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of De1ue
Deque DQ
= new ConcurrentLinkedDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// before removing print Deque
System.out.println("Deque: " + DQ);
}
}
Output:
Deque: [7855642, 35658786, 5278367, 74381793]
Program 4: With the help of LinkedBlockingDeque.
// Java Program Demonstrate add()
// method of Deque
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of De1ue
Deque DQ
= new LinkedBlockingDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// before removing print Deque
System.out.println("Deque: " + DQ);
}
}
Output:
Deque: [7855642, 35658786, 5278367, 74381793]
Below programs illustrate exceptions thrown by addFirst() method:
Program 5: To show NullPointerException.
/// Java Program Demonstrate add()
// method of DeQue when Null is passed
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of DeQue
Deque DQ
= new LinkedBlockingDeque();
// Add numbers to end of DeQue
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
// when null is inserted
DQ.add(null);
// before removing print DeQue
System.out.println("DeQue: " + DQ);
}
}
Output:
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.LinkedBlockingDeque.offerLast(LinkedBlockingDeque.java:357) at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:334) at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633) at GFG.main(GFG.java:20)
Program 6: To show IllegalStateException.
// Java Program Demonstrate add()
// method of Deque when capacity is full
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new LinkedBlockingDeque(3);
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
// when capacity is full
DQ.add(10);
// before removing print Deque
System.out.println("Deque: " + DQ);
}
}
Output:
Exception in thread "main" java.lang.IllegalStateException: Deque full at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:335) at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633) at GFG.main(GFG.java:21)
Note: The other two exceptions are internal and are caused depending on the compiler hence cannot be shown in the compiler.
The contains(E e) method of Deque Interface check for the presence of the element e in the Deque container. If the Deque contains one occurrence of the element, then it returns true else it returns false.
Syntax:
boolean contains(Object o)
Parameters: This method accepts a mandatory parameter o which is the element that needs to be tested if it is present in the Deque or not.
Return Value: The method returns True if the element is present in the Deque otherwise it returns False.
Exceptions: The function throws two exceptions as shown below:
Below programs illustrate the contains() method in Java:
Program 1: With the help of LinkedList.
// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
// Creating an empty Deque
Deque de_que = new LinkedList();
// Use add() method to add elements into the Queue
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the Deque
System.out.println("Deque: " + de_que);
// Check for "Geeks" in the deque
System.out.println("Does the deque contains 'Geeks'? "
+ de_que.contains("Geeks"));
// Check for "4" in the deque
System.out.println("Does the deque contains '4'? "
+ de_que.contains("4"));
// Check if the deque contains "No"
System.out.println("Does the deque contains 'No'? "
+ de_que.contains("No"));
}
}
Output:
Deque: [Welcome, To, Geeks, 4, Geeks] Does the deque contains 'Geeks'? true Does the deque contains '4'? true Does the deque contains 'No'? false
Program 2:
// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
// Creating an empty Deque
Deque de_que = new LinkedList();
// Use add() method to add elements into the Queue
de_que.add(10);
de_que.add(15);
de_que.add(30);
de_que.add(20);
de_que.add(5);
// Displaying the Deque
System.out.println("Deque: " + de_que);
// Check for '15' in the Deque
System.out.println("Does the Deque contains '15'? "
+ de_que.contains(15));
// Check for '2' in the Deque
System.out.println("Does the Deque contains '2'? "
+ de_que.contains(2));
// Check if the Deque contains '10'
System.out.println("Does the Deque contains '10'? "
+ de_que.contains(10));
}
}
Output:
Deque: [10, 15, 30, 20, 5] Does the Deque contains '15'? true Does the Deque contains '2'? false Does the Deque contains '10'? true
Program 3: With the help of ArrayDeque.
// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
// Creating an empty Deque
Deque de_que = new ArrayDeque();
// Use add() method to add elements into the Queue
de_que.add(10);
de_que.add(15);
de_que.add(30);
de_que.add(20);
de_que.add(5);
// Displaying the Deque
System.out.println("Deque: " + de_que);
// Check for '15' in the Deque
System.out.println("Does the Deque contains '15'? "
+ de_que.contains(15));
// Check for '2' in the Deque
System.out.println("Does the Deque contains '2'? "
+ de_que.contains(2));
// Check if the Deque contains '10'
System.out.println("Does the Deque contains '10'? "
+ de_que.contains(10));
}
}
Output:
Deque: [10, 15, 30, 20, 5] Does the Deque contains '15'? true Does the Deque contains '2'? false Does the Deque contains '10'? true
Program 4: With the help of ConcurrentLinkedDeque.
// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String args[])
{
// Creating an empty Deque
Deque de_que = new ConcurrentLinkedDeque();
// Use add() method to add elements into the Queue
de_que.add(10);
de_que.add(15);
de_que.add(30);
de_que.add(20);
de_que.add(5);
// Displaying the Deque
System.out.println("Deque: " + de_que);
// Check for '15' in the Deque
System.out.println("Does the Deque contains '15'? "
+ de_que.contains(15));
// Check for '2' in the Deque
System.out.println("Does the Deque contains '2'? "
+ de_que.contains(2));
// Check if the Deque contains '10'
System.out.println("Does the Deque contains '10'? "
+ de_que.contains(10));
}
}
Output:
Deque: [10, 15, 30, 20, 5] Does the Deque contains '15'? true Does the Deque contains '2'? false Does the Deque contains '10'? true
Program 5: With the help of LinkedBlockingDeque.
// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String args[])
{
// Creating an empty Deque
Deque de_que = new LinkedBlockingDeque();
// Use add() method to add elements into the Queue
de_que.add(10);
de_que.add(15);
de_que.add(30);
de_que.add(20);
de_que.add(5);
// Displaying the Deque
System.out.println("Deque: " + de_que);
// Check for '15' in the Deque
System.out.println("Does the Deque contains '15'? "
+ de_que.contains(15));
// Check for '2' in the Deque
System.out.println("Does the Deque contains '2'? "
+ de_que.contains(2));
// Check if the Deque contains '10'
System.out.println("Does the Deque contains '10'? "
+ de_que.contains(10));
}
}
Output:
Deque: [10, 15, 30, 20, 5] Does the Deque contains '15'? true Does the Deque contains '2'? false Does the Deque contains '10'? true
The getLast() method of Deque Interface returns an iterator over the elements in this deque in a proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is a “weakly consistent” iterator.
Syntax:
Iterator iterator()
Parameters: This method does not accepts any parameter.
Returns: This method returns an iterator over the elements in this deque in a proper sequence.
Below programs illustrate iterator() method of Deque:
Program 1: With the help of ArrayDeque.
// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new ArrayDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// Call iterator() method of Deque
Iterator iteratorVals = DQ.iterator();
// Print elements of iterator
// created from Deque
System.out.println("The iterator values"
+ " of Deque are:");
// prints the elements using an iterator
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
Output:
The iterator values of Deque are: 7855642 35658786 5278367 74381793
Program 2: With the help of LinkedList.
// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new LinkedList();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// Call iterator() method of Deque
Iterator iteratorVals = DQ.iterator();
// Print elements of iterator
// created from Deque
System.out.println("The iterator values"
+ " of Deque are:");
// prints the elements using an iterator
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
Output:
The iterator values of Deque are: 7855642 35658786 5278367 74381793
Program 3: With the help of LinkedBlockingDeque.
// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new LinkedBlockingDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// Call iterator() method of Deque
Iterator iteratorVals = DQ.iterator();
// Print elements of iterator
// created from Deque
System.out.println("The iterator values"
+ " of Deque are:");
// prints the elements using an iterator
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
Output:
The iterator values of Deque are: 7855642 35658786 5278367 74381793
Program 4: With the help of ConcurrentLinkedDeque.
// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new ConcurrentLinkedDeque();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// Call iterator() method of Deque
Iterator iteratorVals = DQ.iterator();
// Print elements of iterator
// created from Deque
System.out.println("The iterator values"
+ " of Deque are:");
// prints the elements using an iterator
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
Output:
The iterator values of Deque are: 7855642 35658786 5278367 74381793
The getFirst() method of Deque Interface returns the first element or the head of the Deque. It does not deletes the element. It throws an exception when the Deque is empty.
Syntax:
E getFirst()
Parameters: This method does not accepts any parameter.
Returns: Thie method returns the first element or the head of the Deque but does not delete it.
Exception: The function throws NoSuchElementException when the Deque is empty and the function is called.
Below programs illustrate getFirst() method of Deque:
Program 1: With the help of LinkedList.
// Java Program Demonstrate getFirst()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new LinkedList();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.getFirst());
}
}
Output:
Deque: [7855642, 35658786, 5278367, 74381793] Deque's head: 7855642
The getLast() method of Deque Interface returns the last element or the tail of the Deque. It does not deletes the element. It throws an exception when the Deque is empty.
Syntax:
E getLast()
Parameters: This method does not accepts any parameter.
Returns: Thie method returns the last element or the tail of the Deque but does not delete it.
Exception: The function throws NoSuchElementException when the Deque is empty and the function is called.
Below programs illustrate getLast() method of Deque:
Program 1: With the help of LinkedList.
// Java Program Demonstrate getLast()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new LinkedList();
// Add numbers to end of Deque
DQ.add(7855642);
DQ.add(35658786);
DQ.add(5278367);
DQ.add(74381793);
// print Deque
System.out.println("Deque: " + DQ);
// print head
System.out.println("Deque's head: " + DQ.getLast());
}
}
The addFirst(E e) method of Deque Interface inserts the element passed in the parameter to the front of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion.
Syntax:
void addFirst(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted to the front of the Deque.
Returns: This method returns true on successful insertion.
Exceptions: The function throws four exceptions which are described as below:
Below programs illustrate addFirst() method of Deque:
Program 1: With the help of LinkedList.
// Java Program Demonstrate addFirst()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of De1ue
Deque DQ
= new LinkedList();
// Add numbers to end of Deque
DQ.addFirst(7855642);
DQ.addFirst(35658786);
DQ.addFirst(5278367);
DQ.addFirst(74381793);
// print Deque
System.out.println("Deque: " + DQ);
}
}
Output:
Deque: [74381793, 5278367, 35658786, 7855642]
The addLast(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion.
Syntax:
boolean addLast(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the end of the Deque.
Returns: This method returns true on successful insertion.
Exceptions: The function throws four exceptions which are described as below:
Below programs illustrate implementation of addLast() method by various classes that implement Deque Interface:
Program 1: With the help of LinkedList.
// Java Program Demonstrate addLast()
// method of Deque
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of De1ue
Deque DQ
= new LinkedList();
// Add numbers to end of Deque
DQ.addLast(7855642);
DQ.addLast(35658786);
DQ.addLast(5278367);
DQ.addLast(74381793);
// before removing print Deque
System.out.println("Deque: " + DQ);
}
}
Output:
Deque: [7855642, 35658786, 5278367, 74381793]
The offerFirst(E e) method of Deque Interface inserts the specified element into the front of the Deque if it is possible to do so immediately without violating capacity restrictions. This method is preferable to addFirst() method since this method does not throws an exception when the capacity of the container is full since it returns false.
Syntax:
boolean offerFirst(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the front of the Deque.
Returns: This method returns true on successful insertion else it returns false.
Exceptions: The function throws four exceptions which are described as below:
Below programs illustrate offerFirst() method of Deque:
Program 1:
// Java Program Demonstrate offerFirst()
// method of Deque when Null is passed
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Deque
Deque DQ
= new LinkedBlockingDeque(3);
if (DQ.offerFirst(10))
System.out.println("The Deque is not full and 10 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerFirst(15))
System.out.println("The Deque is not full and 15 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerFirst(25))
System.out.println("The Deque is not full and 25 is inserted");
else
System.out.println("The Deque is full");
if (DQ.offerFirst(20))
System.out.println("The Deque is not full and 20 is inserted");
else
System.out.println("The Deque is full");
// before removing print Deque
System.out.println("Deque: " + DQ);
}
}
Output:
The Deque is not full and 10 is inserted The Deque is not full and 15 is inserted The Deque is not full and 25 is inserted The Deque is full Deque: [25, 15, 10]