Java Stack removeAllElements() Method

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

Definition

In Java, removeAllElements() removes every element from a Stack and sets its size to zero. The stack object itself survives, so you can keep pushing onto it afterwards.

It is the original Java 1.0 Vector name for clear(); in fact clear() just calls it. Both give the same result, but clear() is the name every collection shares.

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

Call it with no arguments.

Java
Stack<String> stack = new Stack<>();
stack.push("a");
stack.push("b");
stack.removeAllElements();     // []
boolean e = stack.empty();     // true
stack.push("c");               // [c], the same stack is reused
CallWhat it does
stack.removeAllElements()Empties the stack in one call.
stack.clear()The Collection name for the same operation.
stack.setSize(0)Another way to reach size 0.
while (!stack.empty()) stack.pop()Empties one element at a time; only useful if you need each element.

removeAllElements clears every slot of the internal array so the old objects can be garbage collected, but in the JDK implementation it does not shrink the array itself.

Library declarations
Java
// java.util.Vector<E>, inherited by Stack<E>
public synchronized void removeAllElements()

Parameters

ParameterDescription
NoneremoveAllElements() takes no parameters.

Return Value

Return ValueDescription
voidNothing. Afterwards size() is 0.

Compare with Similar Methods

Ways to empty or shrink a stack:

MethodHow it differs
clear()The same operation under its Collection name.
pop()Removes one element and returns it.
setSize(0)Also empties the stack.
removeElement(o)Removes only the first match of a value.

Java Examples

Example 1 — Reset a Score History

Wipe a stack of game scores in one call and confirm it is empty.

Java
import java.util.Stack;

public class StackRemoveAllElementsDemo {
    public static void main(String[] args) {
        Stack<Integer> scores = new Stack<>();
        scores.push(40);
        scores.push(75);
        scores.push(90);
        System.out.println("before: " + scores + ", size " + scores.size());

        scores.removeAllElements();
        System.out.println("after:  " + scores + ", size " + scores.size());
        System.out.println("empty(): " + scores.empty());
    }
}

How It Works

  1. Three scores stored. The stack prints [40, 75, 90], with 90, the latest score, on top.
  2. One call removes all. removeAllElements() drops every score at once, and an empty stack prints as [].
  3. Stack's own check agrees. empty() now returns true, so any loop guarded by it will not run.

Example 2 — Saving Clears the Undo History

Many editors forget the undo history after a save; model that with removeAllElements().

Java
import java.util.Stack;

public class StackRemoveAllElementsSave {
    static Stack<String> undo = new Stack<>();

    static void edit(String change) {
        undo.push(change);
        System.out.println("edit '" + change + "', undo steps: " + undo.size());
    }

    static void save() {
        undo.removeAllElements();
        System.out.println("saved, undo steps: " + undo.size());
    }

    public static void main(String[] args) {
        edit("add title");
        edit("fix typo");
        save();
        edit("add image");
        System.out.println("can undo: " + undo.peek());
    }
}

How It Works

  1. Edits pile up. Each edit call pushes its change, so the undo history grows to 2.
  2. Saving forgets the past. save() drops the whole history with a single removeAllElements() call, and the count falls to 0.
  3. The same stack keeps working. The next edit is pushed onto the now-empty stack, so add image is the only step that can be undone.

Example 3 — removeAllElements() vs clear(), and Shared References

Empty one stack each way, compare them, and check a second variable that points to the first stack.

Java
import java.util.Stack;

public class StackRemoveAllElementsVsClear {
    public static void main(String[] args) {
        Stack<String> a = new Stack<>();
        Stack<String> b = new Stack<>();
        for (String s : new String[] {"x", "y", "z"}) {
            a.push(s);
            b.push(s);
        }
        Stack<String> alias = a;

        a.removeAllElements();
        b.clear();
        System.out.println("a: " + a + ", b: " + b + ", equal: " + a.equals(b));
        System.out.println("alias sees: " + alias + ", same object: " + (alias == a));
    }
}

How It Works

  1. Two stacks, two methods. a is emptied with removeAllElements() and b with clear(); both print as [].
  2. Equal results. equals compares the elements in order, and two empty stacks are equal, which confirms the methods do the same thing.
  3. No copy is made. alias refers to the same object as a, so it sees the empty stack too.

Frequently Asked Questions

It removes every element and sets the size to 0. The stack can be used again right away.
There is no difference in behavior; clear() calls removeAllElements(). clear() is the Collection name, removeAllElements() the older Vector name.
Yes. It clears everything in one pass without returning each element, while popping in a loop makes one method call per element.
No, the capacity stays the same in the JDK implementation. Call trimToSize() afterwards if you want to release that memory.
pop() throws EmptyStackException, because the stack is empty.

Next: removeElement()

Remove the first match from the bottom; returns boolean.

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

7 people found this page helpful