Deque Interface in Java

  • November 8, 2022
  • Java
java thread class

The Deque interface in java.util package is a queue interface subtype. A linear collection with the ability to insert and remove elements at both ends. Deque is an abbreviation for “double-ended queue” and is usually pronounced “deck.” It can function as either a queue (first-in-first-out/FIFO) or a stack (last-in-first-out/LIFO).

Declaration:
public interface Deque extends Queue
Methods of Deque interface:
Method Description
boolean add(E e)Inserts the specified element at the tail of the queue represented by this deque if it is possible to do so immediately without violating capacity constraints, returning true on success and throwing an IllegalStateException if no space is currently available.
void addFirst(E e)If it is possible to do so immediately without violating capacity constraints, this function inserts the specified element at the front of this deque.
void addLast(E e)Inserts the specified element at the end of this deque if doing so immediately is possible without violating capacity constraints.
boolean contains(Object o)Returns true if this deque contains the specified element.
Iterator<E> descendingIterator()Returns an iterator over the elements in this deque in reverse sequential order.
E element()The head of the queue represented by this deque is retrieved but not removed (in other words, the first element of this deque).
E getFirst()The first element of this deque is retrieved but not removed.
E getLast()The last element of this deque is retrieved but not removed.
Iterator<E> iterator()Returns an iterator through the elements of this deque in the correct order.
boolean offer(E e)Inserts the specified element into the queue represented by this deque (at the tail of this deque) if it is possible to do so immediately without violating capacity constraints, returning true on success and false otherwise.
boolean offerFirst(E e)Unless it would violate capacity restrictions, inserts the specified element at the front of this deque.
boolean offerLast(E e)Unless it would violate capacity restrictions, inserts the specified element at the end of this deque.
E peek()Retrieves the head of the queue represented by this deque (the first element of this deque), but does not remove it, or returns null if this deque is empty.
E peekFirst()Returns the first element of this deque, but does not remove it, or returns null if the deque is empty.
E peekLast()Returns the last element of this deque, but does not remove it, or returns null if the deque is empty.
E poll()Returns the head of the queue represented by this deque (that is, the first element of this deque) or null if this deque is empty.
E pollFirst()Returns the first element of this deque and removes it, or returns null if the deque is empty.
E pollLast()Returns the last element of this deque and removes it, or returns null if the deque is empty.
E pop()This deque’s element is popped from the stack it represents.
void push(E e)If it is possible to do so immediately without violating capacity restrictions, it pushes an element onto the stack represented by this deque (at the head of this deque), returning true on success and throwing an IllegalStateException if no space is currently available.
E remove()Retrieves and removes the queue’s head represented by this deque (in other words, the first element of this deque).
boolean remove(Object o)This deque’s first occurrence of the specified element is removed.
E removeFirst()This deque’s first element is retrieved and removed.
boolean removeFirstOccurrence(Object o)This deque’s first occurrence of the specified element is removed.
E removeLast()This deque’s final element is retrieved and removed.
boolean removeLastOccurrence(Object o)This deque’s last occurrence of the specified element is removed.
int size()The number of elements in this deque is returned.
Example:

import java.util.*;

public class Coderz {
	public static void main(String[] args)
	{
		Deque<String> deque
			= new LinkedList<String>();

		// Add at the last
		deque.add("1.Tail");

		// Add at the first
		deque.addFirst("2.Head");

		// Add at the last
		deque.addLast("3.Tail");

		// Add at the first
		deque.push("4.Head");

		// Add at the last
		deque.offer("5.Tail");

		// Add at the first
		deque.offerFirst("6.Head");

		System.out.println(deque + "\n");

		// We can remove the first element
		// or the last element.
		deque.removeFirst();
		deque.removeLast();
		System.out.println("Deque after removing "
						+ "first and last: "
						+ deque);
	}
}
Output:
[6.Head, 4.Head, 2.Head, 1.Tail, 3.Tail, 5.Tail]
Deque after removing first and last: [4.Head, 2.Head, 1.Tail, 3.Tail]

Note: also read about the Queue Interface in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *