×

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

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Vector in Java

Java Vector Class The Vector class is a legacy class that implements a growable array of objects. The components that it contains can be accessed using an integer index. The size of...

26 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

How to Convert Decimal to Binary in Java

How to Convert Decimal to Binary in Java There are two methods to convert Decimal to Binary. Using toBinaryString() method Using user-defined logic Using Integer.toBinaryString() The toBinaryString() is a static method of Integer...

2 minutes read.

Java Keywords

Java Keywords The particular words which are used in java programming language that act like a key or important words to write a code are called java keywords. Java Keywords are...

4 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Functional Interfaces in Java

Java has forever remained an Object-Oriented Programming language. By object-oriented programming language, we can declare that everything present in the Java programming language rotates throughout the Objects, except for some...

11 minutes read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.