Java ArrayDeque Class: pop() Method
public E pop()
The pop() method is used to pop an element from the stack represented by a given deque.
This method is equivalent to removeFirst().
Package: java.util
Java Platform: Java SE 8
Syntax:
pop()
Return Value:
the element at the front of this deque (which is the top of the stack represented by this deque)
Return Value Type: E - the type of elements held in this collection
Throws:
NoSuchElementException - if this deque is empty
Pictorial Presentation
Example: Java ArrayDeque Class: pop() Method
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
// Create an array deque
Deque<Integer> deque = new ArrayDeque<Integer>(8);
// use add() method to add elements in the deque
deque.add(100);
deque.add(200);
deque.add(150);
deque.add(95);
// Print all the elements of the original deque
System.out.println("Elements of the original deque:");
for (Integer number : deque) {
System.out.println("Number = " + number);
}
int rval = deque.pop();
System.out.println("Removed element: " + rval);
// printing all the elements available in deque after using pop()
for (Integer number : deque) {
System.out.println("Number = " + number);
}
}
}
Output:
Elements of the original deque: Number = 100 Number = 200 Number = 150 Number = 95 Removed element: 100 Number = 200 Number = 150 Number = 95
Java Code Editor:
Previous:pollLast Method
Next:push Method
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_pop.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics