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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Stack: [Geeks, for, Geeks, 10, 20]
Was 20 removed: true
Final Stack: [Geeks, for, Geeks, 10]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Stack: [10, 20, 30, 40, 50]
Was 100 removed: false
Final Stack: [10, 20, 30, 40, 50]


My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.