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
Call
What 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()
Edits pile up. Each edit call pushes its change, so the undo history grows to 2.
Saving forgets the past.save() drops the whole history with a single removeAllElements() call, and the count falls to 0.
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));
}
}
Output
a: [], b: [], equal: true
alias sees: [], same object: true
How It Works
Two stacks, two methods.a is emptied with removeAllElements() and b with clear(); both print as [].
Equal results.equals compares the elements in order, and two empty stacks are equal, which confirms the methods do the same thing.
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.