DeQue Interface
Deque Interface
In Java, the Deque interface is under java.util.Deque and it is a subtype of java.util.Queue interface. A Deque is a double-ended queue that means addition and deletion of the elements can be done from both the ends. The Deque can be used as a Queue or as Stack i.e First-in-first-out or Last-in-last-out.
Below are the methods of Deque interface
| S.No. | Method | Description |
|---|---|---|
| 1 | add(element) | It is used for adding elements at the tail of the Deque. |
| 2 | addFirst(element) | It is used for adding elements at the head of the Deque. |
| 3 | addLast(element) | It is used for adding elements at the tail of the Deque. |
| 4 | offer(element) | It is used to add an element at the tail and returns a Boolean value if added successfully. |
| 5 | offerFirst(element) | It is used to addan element at the head and returns a Boolean value if added successfully. |
| 6 | offerLast(element) | It is used to addan element at the tail and returns a Boolean value if added successfully. |
| 7 | iterator() | It is used to iterate the deque. |
| 8 | descendingIterator() | It is used to iterate for the reverse order in the deque. |
| 9 | push(element) | It is used for adding elements at the head of the deque. |
| 10 | pop(element) | It is used for removing elements from the head of the deque. |
| 11 | removeFirst() | It is used for removing elements from the head of the deque. |
| 12 | removeLast() | It is used for removing elements from the tail of the deque |
| 13 | poll() | It is used to get and remove the first element from the deque. If the deque is empty then it returns null. |
| 14 | pollFirst() | It is used to get and remove the first element from the deque. If the deque is empty then it returns null. |
| 15 | pollLast() | It is used to get and remove the last element from the deque. If the deque is empty then it returns null. |
| 16 | peek() | It is used for removing the head of the deque. |
| 17 | peekFirst() | It is used for removing the head of the deque. |
| 18 | peekLast() | It is used for removing the tail of the deque. |
Example:
import java.util.*;
public class DequeDemo1
{
public static void main(String[] args)
{
Dequeobj = new LinkedList();
obj.add("A 1 (Tail)");
obj.addFirst("B 2 (Head)");
obj.addLast("C 3 (Tail)");
obj.push("D 4 (Head)");
obj.offer("E 5 (Tail)");
obj.offerFirst("F 6 (Head)");
obj.offerLast("G 7 (Tail)");
System.out.println("*****************************************************************************");
System.out.println(obj + "\n");
System.out.println("*****************************************************************************");
System.out.println("Standard Iterator");
Iterator iterator = obj.iterator();
while (iterator.hasNext())
System.out.println("\t" + iterator.next());
Iterator reverse = obj.descendingIterator();
System.out.println("*****************************************************************************");
System.out.println("Reverse Iterator");
while (reverse.hasNext())
System.out.println("\t" + reverse.next());
System.out.println("*****************************************************************************");
System.out.println("Peek " + obj.peek());
System.out.println("*****************************************************************************");
System.out.println("After peek: " + obj);
System.out.println("*****************************************************************************");
System.out.println("Pop " + obj.pop());
System.out.println("*****************************************************************************");
System.out.println("After pop: " + obj);
System.out.println("*****************************************************************************");
System.out.println("Contains element 3: " + obj.contains("Element 3 (Tail)"));
obj.removeFirst();
obj.removeLast();
System.out.println("*****************************************************************************");
System.out.println("Deque after removing " + "first and last: " + obj);
}
}









