Java Pop
The array, linked list, stack, queue, and other data structures are supported by Java programming. The insertion, deletion, and element searching operations are available for every data structure. And Java programming has built-in classes and methods to carry out these processes. With the aid of the Stack.pop() method, we will comprehend the pop operation in this section. Java's stack. pop() function removes an element from the stack and returns the current part at the top.
The three pop() methods that Java offers, which are associated with three distinct classes and interfaces, are listed below.
- Java Stack.pop()Method
- Java LinkedList.pop()Method
- Java Deque.pop()Method
Java Stack.pop()Method
To remove a stack element, use Java's java.util.Stack.pop() function. The element is taken out of the stack by being popped from the top.
Syntax:
StackS.pop()
Parameters: There are no parameters required for the technique.
Returns: This method removes the topmost element from the stack before returning it.
Exception: If the stack is empty, the procedure throws an EmptyStackException.
StackDemo.java
// Java code to illustrate pop()
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
// Creating an empty Stack
Stack<Integer> STACK = new Stack<Integer>();
// Use add() method to add elements
STACK.push(18);
STACK.push(45);
STACK.push(333);
STACK.push(555);
STACK.push(17);
// Displaying the Stack
System.out.println("Initial Stack: " + STACK);
// Removing elements using pop() method
System.out.println("Popped element: " +
STACK.pop());
System.out.println("Popped element: " +
STACK.pop());
// Displaying the Stack after pop operation
System.out.println("Stack after pop operation "
+ STACK);
}
}
Output:

Java LinkedList.pop()Method:
The Java LinkedList class implements a doubly linked list to store the elements. Inside the Java.util package, the LinkedList class definition is provided by the Java collections. The top part on the stack is removed by the class's pop() function.
Syntax:
public E pop()
Interface Deque<E> specifies the method. It returns the first item in the list or the top of the stack that this list is meant to represent. The remove() method's functionality is preserved.
If the list is empty, NoSuchElementException is thrown.
The programme below illustrates how to construct a pop operation in a LinkedList represented by a stack. Below is the sample program for the Linked list.pop()Method
import java.util.*;
public class LinkedListPopDemo
{
/* Driver Code */
public static void main(String args[])
{
/* Implementing LinkedList using a stack representation. */
LinkedList<String> listStack = new LinkedList<>();
/* Adding an element into stack. */
listStack.push("Stack");
listStack.push("List");
listStack.push("Implementating");
System.out.println("Linked List after adding new elements: " + listStack);
/* Deleting an element from stack. */
System.out.println("Deleted element 1: " + list stack.pop());
/* Adding an element into the stack. */
listStack.push("Linked");
/* Displaying the complete stack. */
System.out.println("New Linked List: " + listStack);
}
}
Output:

A linked list is implemented using stack representation in the code above. The linked list's elements can be added and removed using the push() and pop() functions.
Java Deque.pop() Method:
A linear data structure known as a deque (double-ended queue) permits insertion and deletion from either end. An interface named Deque is provided by the Java.util package. Many classes, including ArrayDeque, LinkedList, etc., implementit.
Syntax:
<E> pop()
It returns the first element in the Deque, or the top part of the stack this Deque represents. If the Deque is empty, NoSuchElementException is thrown.
The pop operation in a deque is demonstrated in the following programme.
A Sample program on Deque.pop() Method:
import java.util.ArrayDeque;
import java.util.Deque;
public class DequePopDemo
{
/* Driver Code */
public static void main(String[] args)
{
Deque<Integer> dq = new ArrayDeque<Integer>();
/* Inserting the elements. */
dq.push(12);
dq.push(14);
dq.push(16);
/* Displaying the Deque. */
System.out.println("Deque after insertion: ");
for (Integer integer: dq)
{
System.out.println(integer);
}
/* Deleting the element. */
dq.pop();
/* Displaying the complete Deque. */
System.out.println("After deletion: " );
for (Integer integer: dq)
{
System.out.println(integer);
}
}
}
Output:

An instance of the Deque class is created in the code above. Additionally, push() and pop() methods are used to implement the fundamental operations.
What do Java push() and pop() mean?
The Last In First Out (LIFO) data structure is a stack. Push and pop are two of the fundamental operations it provides. When the push operation adds an element, it is removed via the pop operation at the top of the stack. The Stack class in Java simulates the Stack data structure.