In Java, a Stack inherits two remove overloads from Vector. remove(int index) deletes the element at a position and returns it. remove(Object o) deletes the first element equal to o, counted from the bottom, and returns true if it found one.
Both close the gap by shifting the elements above it down by one. Neither follows LIFO order, so in stack code they are mostly used for cleanup, such as dropping the oldest entry at index 0 or cancelling one item by value. To take the top, use pop().
An instance method inherited from java.util.Vector (since Java 1.2). Stack does not override it: index 0 is the bottom of the stack and index size() - 1 is the top.
The Integer trap.
On a Stack<Integer>, stack.remove(1) calls remove(int) and removes the element at index 1. To remove the value 1, write stack.remove(Integer.valueOf(1)).
Syntax
Pass an index to remove a position, or an object to remove a value.
Java
Stack<String> stack = new Stack<>();
stack.push("a");
stack.push("b");
stack.push("c"); // [a, b, c]
String gone = stack.remove(0); // "a", stack is [b, c]
boolean found = stack.remove("c"); // true, stack is [b]
boolean again = stack.remove("c"); // false, nothing to remove
Call
What it does
stack.remove(0)
Removes and returns the bottom element.
stack.remove(stack.size() - 1)
Removes and returns the top, like pop() (but an index exception when empty).
stack.remove(o)
Removes the first match from the bottom; true or false.
stack.remove(Integer.valueOf(n))
Removes the value n from an Integer stack.
Both overloads shift the elements above the removed one, so removing near the bottom of a big stack costs more than popping.
Library declarations
Java
// java.util.Vector<E>, inherited by Stack<E>
public synchronized E remove(int index)
public boolean remove(Object o)
// remove(Object o) delegates to removeElement(o)
Parameters
Parameter
Description
index (int)
For remove(int): position to remove, from 0 (bottom) to size() - 1 (top).
o (Object)
For remove(Object): the value to remove, compared with equals(); null is allowed.
Return Value
Return Value
Description
E
From remove(int): the element that was removed.
boolean
From remove(Object): true if an element was removed, false if none matched.
An int literal picks remove(int). Java prefers the exact int parameter over boxing, so remove(1) deletes the element at index 1, which is 20, not the value 1.
Box it to remove a value.Integer.valueOf(1) is an object, so Java calls remove(Object), finds the value 1, and returns true.
Different return types. The index version returns the removed Integer; the value version only reports true or false.
Example 3 — A Missing Value vs a Bad Index
Remove items from a shopping cart stack by name, then try an index that does not exist.
Java
import java.util.Stack;
public class StackRemoveMissing {
public static void main(String[] args) {
Stack<String> cart = new Stack<>();
cart.push("apple");
cart.push("bread");
System.out.println("remove(\"milk\"): " + cart.remove("milk"));
System.out.println("remove(\"apple\"): " + cart.remove("apple"));
try {
cart.remove(5);
} catch (IndexOutOfBoundsException e) {
System.out.println("remove(5): no element at index 5");
}
System.out.println("cart: " + cart);
}
}
Output
remove("milk"): false
remove("apple"): true
remove(5): no element at index 5
cart: [bread]
How It Works
Absent value, no exception. milk is not in the cart, so remove(Object) simply returns false.
Present value removed. apple is found at the bottom and removed, so the call returns true and bread slides down to index 0.
Bad index throws.remove(5) picks the index overload, and with only one element left it throws an index exception, caught here as IndexOutOfBoundsException.
Frequently Asked Questions
remove(int) deletes the element at an index and returns it. remove(Object) deletes the first element equal to the argument and returns true or false.
Use stack.remove(Integer.valueOf(5)). stack.remove(5) would remove the element at index 5 instead.
No. pop() always removes the top. remove(int) can remove any position and remove(Object) removes the first match from the bottom. remove(stack.size() - 1) behaves like pop() except on an empty stack.
The first one from the bottom, the oldest copy. The others stay.
Nothing is removed and it returns false. No exception is thrown.