Java Stack addAll() Method

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

Definition

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

ParameterDescription
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 ValueDescription
booleantrue if at least one element was added; false for an empty collection.

Compare with Similar Methods

Single-element and other bulk alternatives:

MethodHow it differs
push(e)Adds one element and returns it.
add(e)Adds one element and returns true.
ArrayList.addAll()The same two overloads on an unsynchronized list.
Collections.addAll(c, ...)Adds separate values or an array without building a list first.

Java Examples

Example 1 — Push a Whole Folder Path

Navigate into three folders at once, then go up one level.

Java
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

public class StackAddAllBasic {
    public static void main(String[] args) {
        Stack<String> path = new Stack<>();
        path.push("home");

        List<String> folders = Arrays.asList("docs", "java", "notes");
        boolean changed = path.addAll(folders);

        System.out.println("changed: " + changed);
        System.out.println("path: " + path);
        System.out.println("current folder: " + path.peek());
        System.out.println("up one level: " + path.pop() + " -> " + path.peek());
    }
}

How It Works

  1. Appended in list order. docs, java, and notes are added above home in exactly the order the list iterates them.
  2. The list's last item is the top. notes came last in the list, so it is the current folder that peek() shows.
  3. 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);
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;

public class StackAddAllEdgeCases {
    public static void main(String[] args) {
        Stack<Integer> scores = new Stack<>();
        scores.push(50);
        scores.push(60);

        System.out.println("empty list: " + scores.addAll(new ArrayList<Integer>()));
        scores.addAll(0, Arrays.asList(10, 20));
        System.out.println("addAll(0, ...): " + scores);
        try {
            scores.addAll(null);
        } catch (NullPointerException e) {
            System.out.println("addAll(null): NullPointerException");
        }
    }
}

How It Works

  1. Nothing to add, so false. An empty ArrayList leaves the stack unchanged, and addAll reports that by returning false.
  2. Insert at the bottom. addAll(0, ...) places 10 and 20 below the existing scores in list order, while 60 stays on top.
  3. null is not a collection. addAll(null) throws NullPointerException because there is nothing to copy from.

Frequently Asked Questions

In the collection's iteration order. The first element goes just above the old top and the last element becomes the new top.
Copy the list, reverse the copy with Collections.reverse, and then call addAll. Or loop over the list backwards and push each element.
true if the stack changed, which happens whenever the collection is not empty. An empty collection gives false.
No. addAll(null) throws NullPointerException. The collection itself may contain null elements, though.
No. addAll() is the bulk version of push(), inherited from Vector.

Next: clear()

Remove every element through the List API.

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

13 people found this page helpful