Java Stack removeElement() Method

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

Definition

In Java, removeElement(obj) removes the first occurrence of obj from a Stack, searching upward from the bottom with equals(). Elements above it move down one place, and the method returns true if something was removed.

It is the legacy Vector name behind remove(Object), which simply calls it. Because the search starts at the bottom, it removes the oldest copy of a repeated value, not the copy nearest the top that search() would report.

An instance method inherited from java.util.Vector (since Java 1.0). Stack does not override it: index 0 is the bottom of the stack and index size() - 1 is the top.

Syntax

Pass the value to remove.

Java
Stack<String> stack = new Stack<>();
stack.push("x");
stack.push("y");
stack.push("x");                          // [x, y, x]
boolean a = stack.removeElement("x");     // true, [y, x]  (the bottom x went)
boolean b = stack.removeElement("z");     // false, unchanged
CallWhat it does
stack.removeElement(o)Removes the first match from the bottom; true or false.
while (stack.removeElement(o)) { }Removes every copy of o.
stack.remove(o)The List name; it calls removeElement.
stack.removeElementAt(stack.lastIndexOf(o))Removes the copy nearest the top instead (check for -1 first).

removeElement scans from the bottom and then shifts the elements above the match, so its cost grows with the stack size.

Library declarations
Java
// java.util.Vector<E>, inherited by Stack<E>
public synchronized boolean removeElement(Object obj)

Parameters

ParameterDescription
obj (Object)The value to remove, compared with equals(); null removes the first null element.

Return Value

Return ValueDescription
booleantrue if a matching element was removed, false if none was found.

Compare with Similar Methods

Other removal methods:

MethodHow it differs
remove(o)The List name for the same operation; also has the index overload remove(int).
removeElementAt(i)Removes by position instead of by value.
removeAllElements()Removes everything.
contains(o)Checks for the value without removing it.

Java Examples

Example 1 — Which Milk Carton Disappears?

A fridge shelf holds two milk cartons; remove one and see which.

Java
import java.util.Stack;

public class StackRemoveElementFirst {
    public static void main(String[] args) {
        Stack<String> shelf = new Stack<>();
        shelf.push("milk");
        shelf.push("eggs");
        shelf.push("milk");
        shelf.push("butter");

        boolean removed = shelf.removeElement("milk");
        System.out.println("removed: " + removed);
        System.out.println("shelf: " + shelf);
        System.out.println("milk now at index " + shelf.indexOf("milk"));
    }
}

How It Works

  1. The search starts at the bottom. There are two milk cartons, and the scan meets the one at index 0 first.
  2. Only that copy goes. The bottom carton is removed and the call returns true; the newer carton stays.
  3. Everything above slides down. eggs moves to index 0 and the remaining milk to index 1, as indexOf shows.

Example 2 — Cancel a Pending Job by Name

A job stack runs the newest job first; cancel one job by its name.

Java
import java.util.Stack;

public class StackRemoveElementCancel {
    static void cancel(Stack<String> jobs, String name) {
        if (jobs.removeElement(name)) {
            System.out.println("cancelled " + name + ", left: " + jobs);
        } else {
            System.out.println(name + " not found");
        }
    }

    public static void main(String[] args) {
        Stack<String> jobs = new Stack<>();
        jobs.push("print report");
        jobs.push("send invoice");
        jobs.push("backup db");

        cancel(jobs, "send invoice");
        cancel(jobs, "send invoice");
        System.out.println("next job: " + jobs.peek());
    }
}

How It Works

  1. Use the boolean. removeElement returns true when the job existed, so one call both removes it and chooses the message.
  2. A second cancel finds nothing. The invoice job is already gone, so the call returns false and the stack stays as it was.
  3. LIFO order for the rest. Cancelling from the middle did not disturb the top: backup db is still the next job to run.

Example 3 — Remove Every Copy of a Value

Clear all the 3s out of a stack of server ping times.

Java
import java.util.Stack;

public class StackRemoveElementAll {
    public static void main(String[] args) {
        Stack<Integer> pings = new Stack<>();
        int[] values = {3, 7, 3, 3, 9};
        for (int v : values) {
            pings.push(v);
        }

        int count = 0;
        while (pings.removeElement(3)) {
            count++;
        }
        System.out.println("removed " + count + " copies of 3");
        System.out.println("left: " + pings);
    }
}

How It Works

  1. No index confusion here. removeElement only accepts an Object, so the 3 is boxed and treated as a value, never as an index.
  2. Repeat until false. Each call removes the lowest remaining 3 and returns true; the fourth call finds none and ends the loop.
  3. Count the removals. The counter reaches 3, and only 7 and 9 remain, in their original order.

Frequently Asked Questions

The first occurrence, searching from the bottom. That is the oldest copy, not the one nearest the top.
None in behavior: remove(Object) calls removeElement. removeElement is the Vector name from Java 1.0.
true if an element was removed, false if no element equal to the argument was found.
No. removeElement has only an Object parameter, so 1 is boxed and the value 1 is removed. The index trap exists only with remove().
Loop with while (stack.removeElement(value)) { } until it returns false.

Next: removeElementAt()

Delete the element at an index; returns nothing.

removeElementAt() 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.

8 people found this page helpful