Java Utility Library

Java ArrayDeque - pop() Method



The java.util.ArrayDeque.pop() method deletes an element from the stack represented by the deque and returns the deleted element as the value of this method. Every deletion of element results into reducing the deque size by one unless the deque is empty.

Syntax

public E pop()

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


Parameters

No parameter is required.

Return Value

Returns the deleted element (first element) of the stack represented by the deque.

Exception

Throws NoSuchElementException, if the deque is empty.

Example:

In the example below, the java.util.ArrayDeque.pop() method is used to delete the first element of the stack represented by the given 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);

    //printing ArrayDeque
    System.out.println("MyDeque contains: " + MyDeque);

    //deleting first element
    while(MyDeque.size() != 0) {
      System.out.println(MyDeque.pop() + " is deleted from deque.");
   }
  }
}

The output of the above code will be:

MyDeque contains: [10, 20, 30]
10 is deleted from deque.
20 is deleted from deque.
30 is deleted from deque.

❮ Java.util - ArrayDeque