Java Collections addAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

Definition

In Java, Collections.addAll(c, e1, e2, ...) adds each listed element to the collection c, in order. You can also pass an array as the second argument, because the elements are a varargs parameter.

It returns true if the collection changed. For a list that is always the case when you pass at least one element; for a set it is false when every element was already present. Since Java 5.

A static method of the java.util.Collections utility class, available since Java 5. Call it on the class name, as in Collections.addAll(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Pass the target collection, then the elements (or an array of them).

Java
List<String> cart = new ArrayList<>();
Collections.addAll(cart, "milk", "eggs");        // true, cart = [milk, eggs]
String[] more = {"tea", "jam"};
Collections.addAll(cart, more);                   // cart = [milk, eggs, tea, jam]
CallWhat it does
Collections.addAll(c, a, b, c2)Adds the listed elements in order.
Collections.addAll(c, array)Adds every element of an object array.
c.addAll(Arrays.asList(a, b))The instance method; needs a collection, not loose values.
new ArrayList<>(Arrays.asList(a, b))Creates a new list instead of filling an existing one.

The documentation notes that Collections.addAll is likely to run significantly faster than c.addAll(Arrays.asList(...)) under most implementations, because it does not need to wrap the values in a list first.

Library declarations
Java
// java.util.Collections
@SafeVarargs
public static <T> boolean addAll(Collection<? super T> c, T... elements)

Parameters

ParameterDescription
cThe collection to add to. It must support add.
elementsThe elements to add, written one by one or passed as an array.

Return Value

Return ValueDescription
trueThe collection changed (at least one element was added).
falseNothing was added, for example every value was already in a set.

Compare with Similar Methods

Ways to add many elements at once:

MethodHow it differs
list.addAll(coll)Instance method that takes another collection.
Arrays.asList(...)Makes a fixed-size list of values; you cannot add to it later.
nCopies(n, v)Many copies of one value.
deque.addAll(coll)The same instance method on an ArrayDeque.

Java Examples

Example 1 — Add Loose Values and a Whole Array

Fill a shopping cart with three items in one call, then add an array of extra items.

Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsAddAllCart {
    public static void main(String[] args) {
        List<String> cart = new ArrayList<>();
        boolean changed = Collections.addAll(cart, "milk", "eggs", "bread");
        System.out.println(changed + " " + cart);

        String[] extras = {"tea", "jam"};
        Collections.addAll(cart, extras);
        System.out.println(cart);
    }
}

How It Works

  1. Varargs. The three strings after cart are collected into an array behind the scenes and added in the written order.
  2. Changed. A list always grows when you add elements, so the result is true.
  3. Arrays fit the same slot. Passing extras directly works because a String[] matches the T... parameter, so tea and jam are appended.

Example 2 — When addAll Returns false

Add IDs to a TreeSet; the second call adds only duplicates.

Java
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

public class CollectionsAddAllSet {
    public static void main(String[] args) {
        Set<Integer> ids = new TreeSet<>();
        ids.add(3);

        System.out.println("add 1, 3, 2: " + Collections.addAll(ids, 1, 3, 2) + " " + ids);
        System.out.println("add 1, 2:    " + Collections.addAll(ids, 1, 2) + " " + ids);
    }
}

How It Works

  1. Partly new. 3 is already present, but 1 and 2 are new, so the set changes and the call returns true.
  2. Sorted storage. A TreeSet keeps its elements in ascending order, so it prints [1, 2, 3] regardless of insertion order.
  3. Nothing new. 1 and 2 are both duplicates in the second call, so the set is unchanged and the result is false.

Example 3 — Fixed-Size Lists and Collections That Reject null

Adding to an Arrays.asList list fails, and a null stops the adding part-way through.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

public class CollectionsAddAllFailures {
    public static void main(String[] args) {
        List<String> fixed = Arrays.asList("a", "b");
        try {
            Collections.addAll(fixed, "c");
        } catch (UnsupportedOperationException e) {
            System.out.println("Arrays.asList: " + e.getClass().getSimpleName());
        }

        Deque<String> deque = new ArrayDeque<>();
        try {
            Collections.addAll(deque, "x", null, "y");
        } catch (NullPointerException e) {
            System.out.println("null element: " + e.getClass().getSimpleName());
        }
        System.out.println("Deque kept: " + deque);
    }
}

How It Works

  1. Fixed size. Arrays.asList returns a list whose length is tied to its array, so adding "c" throws UnsupportedOperationException.
  2. One at a time. addAll calls deque.add for each value in turn: "x" succeeds first.
  3. Stops at null. ArrayDeque rejects null, so the second add throws and "y" is never reached.
  4. Not all-or-nothing. The deque still holds "x", which shows the earlier additions are not undone.

Frequently Asked Questions

true if the collection changed as a result of the call, false if nothing was added.
Yes. The elements parameter is varargs, so an object array such as String[] can be passed directly.
Collections.addAll takes loose values or an array. list.addAll takes another Collection.
The target collection cannot grow, for example a list from Arrays.asList or an unmodifiable view.
No. Elements are added one by one, so if one fails, the earlier ones stay in the collection.

Next: asLifoQueue()

View a Deque as a last-in-first-out Queue (a stack behind the Queue interface).

asLifoQueue() 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