Stack remove(Object) method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes 1 Likes Like Report The Java.util.Stack.remove(Object o) method is used to remove any particular element from the Stack. Syntax: Stack.remove(Object o) Parameters: This method accepts a mandatory parameter o is of the object type of Stack and specifies the element to be removed from the Stack. Return Value: Returns True if the specified element is found and removed from the Stack, else False. Below program illustrate the Java.util.Stack.remove(Object o) method: Example 1: Java // Java code to illustrate remove() when position of // element is passed as parameter import java.util.*; public class StackDemo { public static void main(String args[]) { // Creating an empty Stack Stack<String> stack = new Stack<String>(); // Use add() method to add elements in the Stack stack.add("Geeks"); stack.add("for"); stack.add("Geeks"); stack.add("10"); stack.add("20"); // Output the Stack System.out.println("Stack: " + stack); // Remove the element using remove() boolean res = stack.remove("20"); // Print the removed element System.out.println("Was 20 removed: " + res); // Print the final Stack System.out.println("Final Stack: " + stack); } } Output: Stack: [Geeks, for, Geeks, 10, 20] Was 20 removed: true Final Stack: [Geeks, for, Geeks, 10] Example 2: Java // Java code to illustrate remove() when position of // element is passed as parameter 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 in the Stack stack.add(10); stack.add(20); stack.add(30); stack.add(40); stack.add(50); // Output the Stack System.out.println("Stack: " + stack); // Remove the element using remove() boolean res = stack.remove("100"); // Print the removed element System.out.println("Was 100 removed: " + res); // Print the final Stack System.out.println("Final Stack: " + stack); } } Output: Stack: [10, 20, 30, 40, 50] Was 100 removed: false Final Stack: [10, 20, 30, 40, 50] Create Quiz Comment C code_r Follow 1 Improve C code_r Follow 1 Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-Stack +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like