In Java, addAll(c) appends every element of a collection to a Stack in the order the collection's iterator returns them. The first element lands just above the old top, and the last element of the collection becomes the new top.
It returns true if the stack changed, which happens whenever the collection is not empty. A second overload, addAll(index, c), inserts the whole collection at an index instead of at the top.
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 collection's last element ends on top.
addAll(Arrays.asList("a", "b", "c")) leaves "c" on top, so the next pop() returns "c". If "a" should come off first, reverse a copy of the list before adding it, for example with Collections.reverse().
Syntax
Pass a collection, or an index plus a collection.
Java
Stack<String> stack = new Stack<>();
stack.push("start");
stack.addAll(Arrays.asList("a", "b", "c")); // true, [start, a, b, c]
String top = stack.peek(); // "c"
stack.addAll(0, Arrays.asList("x", "y")); // [x, y, start, a, b, c]
Call
What it does
stack.addAll(list)
Appends all elements; the list's last element becomes the top.
stack.addAll(0, list)
Inserts all elements at the bottom, in list order.
stack.addAll(otherStack)
Copies another stack bottom to top, so its top stays on top.
for (E e : list) stack.push(e)
The same result as addAll(list), one push at a time.
addAll copies the collection into an array once and appends that array, which is cheaper than many separate pushes.
Library declarations
Java
// java.util.Vector<E>, inherited by Stack<E>
public synchronized boolean addAll(Collection<? extends E> c)
public synchronized boolean addAll(int index, Collection<? extends E> c)
Parameters
Parameter
Description
c (Collection<? extends E>)
The elements to add. Must not be null, but may contain null elements.
index (int)
Only for addAll(index, c): where the first new element goes, from 0 to size().
Return Value
Return Value
Description
boolean
true if at least one element was added; false for an empty collection.
changed: true
path: [home, docs, java, notes]
current folder: notes
up one level: notes -> java
How It Works
Appended in list order. docs, java, and notes are added above home in exactly the order the list iterates them.
The list's last item is the top. notes came last in the list, so it is the current folder that peek() shows.
Going up is a pop. The last line pops notes and then peeks at java; string concatenation evaluates left to right, so the pop happens before the peek.
Example 2 — Make the First Task Come Off First
A plan lists tasks in the order to do them; reverse a copy so the first task ends on top.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
public class StackAddAllReversed {
public static void main(String[] args) {
List<String> plan = Arrays.asList("email", "report", "call");
Stack<String> asIs = new Stack<>();
asIs.addAll(plan);
System.out.println("as is, first pop: " + asIs.pop());
List<String> reversed = new ArrayList<>(plan);
Collections.reverse(reversed);
Stack<String> tasks = new Stack<>();
tasks.addAll(reversed);
System.out.println("reversed, first pop: " + tasks.pop());
System.out.println("remaining: " + tasks);
}
}
Output
as is, first pop: call
reversed, first pop: email
remaining: [call, report]
How It Works
Plain addAll. The plan is added in order, so call, the last task, sits on top and comes off first, the opposite of what the plan intends.
Reverse a copy. Copying the plan into a new ArrayList leaves the original list untouched; Collections.reverse then flips the copy to call, report, email.
First task on top. After addAll, email is the top, so the first pop() returns it while call waits at the bottom.
Example 3 — Empty List, Index Insert, and null
Three special cases of addAll on a stack of scores.