You are reading the article How Does The Pop Method Work In Java With Examples? updated in September 2023 on the website Uyenanhthammy.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Does The Pop Method Work In Java With Examples?
Introduction to Java PopIn Java, the pop is a method that removes elements in the stack, array, LinkedList, etc. An element can be removed from the stack using the Stack.pop() method, and it will be removed from the top. In the case of LinkedList, LinkedListObject.pop() method is used to remove from the top of the stack denoted by LinkedList. LinkedBlockingDeque.pop()method in ArrayDequere moves the element from the top of the Deque. More details on each of them will be discussed in the following sections.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
SyntaxLet us see the syntax of the pop method in a stack, LinkedList, and Deque.
1. StackSyntax:
STACK.pop()Parameters: No parameters for this method.
2. LinkedListObjectSyntax:
LinkedListObject.pop()Parameters: No parameters for this method.
Return Type: Returns the element and removes it from the top of the stack.
3. LinkedBlockingDequeSyntax:
LinkedBlockingDeque.pop()Parameters: No parameters for this method.
Return Value: Returns the element and removes it from the top of the stack.
How does the pop method work in Java?The pop method works similarly in Stack, LinkedList, and Deque. The following steps can be performed for that.
1. Create a stack, LinkedList, or Deque based on the requirement.
2. Remove elements from it using the pop method.
s.pop() li.pop() dq.pop() Examples of Java PopDifferent examples are mentioned below:
Example #1Java program to pop elements of string type from a stack.
Code:
import java.util.*; public class PopMethodExample { public static void main(String args[]) { st.push("Happy"); st.push("Sad"); st.push("Confused"); st.push("Tensed"); st.push("Mixed Emotions"); System.out.println("Stack Elements: " + st ); st.pop(); st.pop(); System.out.println("Stack after removing elements " + st); } }Output:
First, create a stack s and add elements using the push() method. Then, print the stack and remove the elements using the pop() method. As there is two pop() methods are called, two elements from the top of the stack will be removed on executing the code. At last, the stack after removing elements gets printed.
Example #2Java program to pop elements of integer type from LinkedList.
Code:
import java.util.*; public class PopMethodExample { public static void main(String args[]) { lis.push(45); lis.push(90); lis.push(67); lis.push(33); lis.push(56); System.out.println("LinkedList Elements: " + lis); lis.pop(); lis.pop(); System.out.println("LinkedList after removing elements " + lis); } }Output:
Create a LinkedListlisand add elements of integer type using the push() method. Then, print the LinkedListand remove the elements using the pop() method. On executing the code, LinkedList before removing elements and after removing elements can be displayed.
Example #3Java program to remove integer elements from the stack.
Code:
import java.util.*; public class PopMethodExample { public static void main(String args[]) { st.push(45); st.push(90); st.push(67); st.push(33); st.push(56); System.out.println("stack Elements: " + st); st.pop(); st.pop(); System.out.println("stack after removing elements " + st); } }Output:
A stack to accept integer elements is created first. Once it is created, elements are added to the stack using the push() method. After printing the current elements in the stack, two elements are removed from it. For checking whether those elements are from the stack, elements are printed one more time. On executing the code, it can be seen that two elements are removed from the stack.
Example #4Java program to remove string elements from the LinkedList.
Code:
import java.util.*; public class PopMethodExample { public static void main(String args[]) { lis.push("Happy"); lis.push("Sad"); lis.push("Confused"); lis.push("Tensed"); lis.push("Mixed Emotions"); System.out.println("LinkedList Elements: " + lis ); lis.pop() ; lis.pop() ; System.out.println("LinkedList after removing elements " + lis ) ; } }Output:
Example #5Java program to remove string elements fromLinkedBlockigDeque.
Code:
import java.util.concurrent.LinkedBlockingDeque; public class PopMethodExample { public static void main(String[] args) { dq.add("Hia"); dq.add("Ahi"); dq.add("Liba"); dq.add("Geru"); String s = dq.pop(); System.out.println("First stack element : "+ s); System.out.println("Stack after removing element : "+ dq); } }Output:
Create a deque for adding the elements. For that, use the method add() and add the elements of string type. Then, print the Deque and identify the current elements present in it. After printing the current elements in the stack, remove the first element from the stack. For checking whether those elements are removed from the Deque, elements are printed one more time. On executing the code, it can be seen that one element is removed from the Deque.
Example #6Java program to remove integer elements from LinkedBlockigDeque.
Code:
import java.util.concurrent.LinkedBlockingDeque; public class PopMethodExample { public static void main(String[] args) { dq.add(34); dq.add(45); dq.add(56); Integer i = dq.pop(); System.out.println("First stack element : "+ i ); System.out.println("Stack after removing element : "+ dq); } }Output:
Unlike the above program, elements of integer type are added using add() method. On executing the code, it can be seen that one element is removed from the Deque.
ConclusionPop is a method that is used to remove elements from the stack, LinkedList, array with the help of Stack.pop(), LinkedList.pop() and LinkedBlockingDeque.pop() respectively. In this article, details such as syntax, working, and example of the pop method is explained in detail.
Recommended ArticlesThis is a guide to Java Pop. Here we discuss how the pop method works in Java and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –
You're reading How Does The Pop Method Work In Java With Examples?
Update the detailed information about How Does The Pop Method Work In Java With Examples? on the Uyenanhthammy.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!