Java Deque

A deque is a type of linear collection that enables the addition and removal of elements from both the front and the back. The term 'deque' is short for double-ended queue.

Deques do not have predetermined constraints on the number of elements they can hold. This interface accommodates deques with limited capacities as well as those without set size restrictions. It offers multiple approaches for adding, deleting, and inspecting elements.

These functions generally come in two variations: one that raises an exception when the specific operation encounters an error, and the other that provides a distinct value, such as null or false, based on the outcome of the operation.

Methods

The Java interface java.util.Deque offers functionalities for manipulating double-ended queues in Java. The corresponding class that implements this interface is java.util.ArrayDeque.

Methods Description
add(E e) This method is used to insert a specified element into the queue represented by the deque
addAll(Collection<? Extends E>c) Adds all the elements in the specified collection at the end of the deque.
addFirst(E e) Inserts the specified element at the front of the deque.
addLast(E e) Inserts the specified element at the end of the deque.
contains(object o) Returns true if the deque contains the specified element.
descendingIterator() Returns an iterator over the elements in reverse sequential order.
element() Retrieves the head of the queue represented by the deque.
getFirst() Retrieves but does not remove the first element of the deque.
getLast() Retrieves but does not remove the last element of the deque.
iterator() Returns an iterator over the element in the deque in a proper sequence.
offer(E e) Inserts the specified element into the deque, returning true upon success and false if no space is available.
offerFirst() Inserts the specified element at the front of the deque unless it violates the capacity restriction.
offerLast() Inserts the specified element at the end of the deque unless it violates the capacity restriction.
peek() Retrieves but does not move the head of the queue represented by the deque or may return null if the deque is empty.
peekFirst() Retrieves but does not move the first element of the deque or may return null if the deque is empty.
peekLast() Retrieves but does not move the last element of the deque or may return null if the deque is empty.
poll() Retrieves and remove the head of the queue represented by the deque or may return null if the deque is empty.
pollFirst() Retrieves and remove the first element of the deque or may return null if the deque is empty.
pollLast() Retrieves and remove the last element of the deque or may return null if the deque is empty.
pop() Pops an element from the stack represented by the deque.
push() Pushes an element onto the stack represented by the deque.
remove() Retrieves and remove the head of the queue represented by the deque.
remove(Object o) Removes the first occurrence of the specified element from the deque.
removeFirst() Retrieves and remove the first element from the deque.
removeFirstOccurrence(Object o) Remove the first occurrence of the element from the deque.
removeLast() Retrieve and remove the last element from the deque.
removeLastOccurrence(Object o) Remove the last occurrence of the element from the deque.
size() Returns the total number of elements in the deque.

Example 1

Example

Example

import java.util.ArrayDeque;

import java.util.Deque;



public class JavaDequeExample1 {

  public static void main(String[] args) {

	Deque<Integer> deque = new ArrayDeque<Integer>();

	// Inserts the element.

	deque.add(1);

	deque.add(2);

	deque.add(3);

	System.out.println("Inserting three elements : ");

	for (Integer integer : deque) {

	System.out.println(integer);	

	}

	// Popping the element.

	deque.pop();

	System.out.println("After popping : ");

	for (Integer integer : deque) {

		System.out.println(integer);

	}

	deque.remove(3);

	System.out.println("Removing the element 3 :"+deque);

  }

}

Output:

Output

Inserting three elements : 

1

2

3

After popping : 

2

3

Removing the element 3 :[2]

Example 2

Example

Example

import java.util.ArrayDeque;

import java.util.Deque;



public class JavaDequeExample2 {

public static void main(String[] args) {

   Deque<String> deque = new ArrayDeque<String>();

   // Adding the element in the front of the deque.

   deque.addFirst("Java");

    System.out.println("The first element is : "+deque);

   // Again adding the element in the front of the deque.

    deque.addFirst("Python");

    System.out.println("After adding the next element in the front of the deque : "+deque);

     deque.add("Ruby");

     System.out.println("The final deque is  : "+deque);

   // Returns the number of elements.

     int size =  deque.size();

     System.out.println("The number of elements are : "+size);

    // Removes the last element.

     deque.removeLast();

 System.out.println("Deque after removing the last element is given as :  "+deque);

   }	

}

Output:

Output

The first element is : [Java]

After adding the next element in the front of the deque : [Python, Java]

The final deque is  : [Python, Java, Ruby]

The number of elements are : 3

Deque after removing the last element is given as :  [Python, Java]

Input Required

This code uses input(). Please provide values below: