×

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 Pop

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:

Java Pop

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:

Java Pop

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.


Related Topics

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

Java Naming Conventions

JAVA NAMING CONVENTIONS Java naming convention is a standard pattern for writing your identifier name such as class, interfaces, methods, constants, variable, etc. These patterns are not standard rules that you must...

2 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.

Java String getBytes() method

getBytes() method returns sequence of bytes. Syntax: There are three variants of getBytes() method: public byte[] getBytes()        public byte[] getBytes(String charsetName) public byte[] getBytes(Charset charset) Returns: It returns sequence of bytes. Java String getBytes() Example...

2 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Three Partition Problems in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

8 minutes read.

Java Command Line Arguments

Java command line arguments Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and...

2 minutes read.

How to run Java Program in Command Prompt

How to run Java Program in Command Prompt In this section, we will learn how to write, save, compile, and execute or run a Java program in the Command Prompt. Note: One...

3 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Difference Between replace() and replaceall() in Java

In this article, you will be acknowledged about the replace() and replaceall() methods in java. Also, most importantly you will be acknowledged the differences between them along with the example...

4 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Java String subSequence() method

This method returns a new character sequence i.e. subsequence of current sequence Syntax: public CharSequence subSequence(int beginIndex, int endIndex) Parameter: beginIndex ? begin index, inclusive. endIndex ? end index, exclusive. Return: specified subsequnce Throws: It throws IndexOutOfBoundsException...

1 minute read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.