Java Stack remove() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Inherited from Vector

Definition

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

ParameterDescription
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 ValueDescription
EFrom remove(int): the element that was removed.
booleanFrom remove(Object): true if an element was removed, false if none matched.

Compare with Similar Methods

Other ways to take elements off a stack:

MethodHow it differs
pop()Removes and returns the top; EmptyStackException when empty.
removeElement(o)The legacy twin of remove(Object).
removeElementAt(i)Like remove(int) but returns nothing.
ArrayList.remove()The same two overloads, and the same Integer trap, on a list.

Java Examples

Example 1 — Drop the Oldest Notification

Notifications are pushed as they arrive; remove the oldest one at the bottom.

Java
import java.util.Stack;

public class StackRemoveByIndex {
    public static void main(String[] args) {
        Stack<String> alerts = new Stack<>();
        alerts.push("low battery");
        alerts.push("new email");
        alerts.push("update ready");

        String oldest = alerts.remove(0);
        System.out.println("removed oldest: " + oldest);
        System.out.println("alerts: " + alerts);
        System.out.println("top unchanged: " + alerts.peek());
    }
}

How It Works

  1. Index 0 is the oldest alert. low battery was pushed first, so it sits at the bottom, where pop() cannot reach it.
  2. remove(int) returns the element. remove(0) takes low battery out, hands it back, and shifts the other two alerts down one index.
  3. The top is untouched. update ready is still the newest alert and the next one that peek() and pop() would see.

Example 2 — remove(1) vs remove(Integer.valueOf(1))

On a stack of numbers, the same-looking calls remove different things.

Java
import java.util.Stack;

public class StackRemoveIntegerTrap {
    public static void main(String[] args) {
        Stack<Integer> nums = new Stack<>();
        nums.push(10);
        nums.push(20);
        nums.push(1);
        nums.push(30);

        Integer atIndex = nums.remove(1);
        System.out.println("remove(1) removed " + atIndex + ": " + nums);

        boolean valueGone = nums.remove(Integer.valueOf(1));
        System.out.println("remove(Integer.valueOf(1)) -> " + valueGone + ": " + nums);
    }
}

How It Works

  1. 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.
  2. Box it to remove a value. Integer.valueOf(1) is an object, so Java calls remove(Object), finds the value 1, and returns true.
  3. 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);
    }
}

How It Works

  1. Absent value, no exception. milk is not in the cart, so remove(Object) simply returns false.
  2. Present value removed. apple is found at the bottom and removed, so the call returns true and bread slides down to index 0.
  3. 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.

Next: removeAllElements()

Vector's legacy way to empty the stack.

removeAllElements() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful