Stack remove(Object) method in Java with Example

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 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 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]
				
					


