Java Utility Library

Java ArrayDeque - remove() Method



The java.util.ArrayDeque.remove() method is used to retrieve and remove the head (first element) of the queue represented by the deque.

Syntax

public E remove()

Here, E is the type of element maintained by the container.


Parameters

No parameter is required.

Return Value

Returns the head of the queue represented by the deque.

Exception

Throws NoSuchElementException, if this deque is empty.

Example:

In the example below, the java.util.ArrayDeque.remove() method is used to display the content of the deque.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a ArrayDeque
    ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>();

    //populating ArrayDeque 
    MyDeque.add(10);
    MyDeque.add(20);
    MyDeque.add(30);
    MyDeque.add(40);

    //printing ArrayDeque
    System.out.print("MyDeque contains: ");
    while(MyDeque.size() != 0)
      System.out.print(MyDeque.remove()+ " ");
  }
}

The output of the above code will be:

MyDeque contains: 10 20 30 40 

❮ Java.util - ArrayDeque