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:
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
Stack<String> stack = new Stack<String>();
stack.add("Geeks");
stack.add("for");
stack.add("Geeks");
stack.add("10");
stack.add("20");
System.out.println("Stack: " + stack);
boolean res = stack.remove("20");
System.out.println("Was 20 removed: "
+ res);
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:
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
Stack<Integer> stack = new Stack<Integer>();
stack.add(10);
stack.add(20);
stack.add(30);
stack.add(40);
stack.add(50);
System.out.println("Stack: " + stack);
boolean res = stack.remove("100");
System.out.println("Was 100 removed: "
+ res);
System.out.println("Final Stack: "
+ stack);
}
}
|
Output:
Stack: [10, 20, 30, 40, 50]
Was 100 removed: false
Final Stack: [10, 20, 30, 40, 50]
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
24 Dec, 2018
Like Article
Save Article