Home Java Collections Methods addAll() Java Collections addAll() Method 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).
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] Call What 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.util.Collections
@SafeVarargs
public static <T> boolean addAll(Collection<? super T> c, T... elements) Parameters Parameter Description 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 Value Description trueThe collection changed (at least one element was added). falseNothing was added, for example every value was already in a set.
Important
A collection that cannot grow, such as Arrays.asList or an unmodifiable view, throws UnsupportedOperationException. A collection that refuses null (such as ArrayDeque or TreeSet) throws NullPointerException for a null element. Elements are added one at a time, so elements before the failing one stay added.
Compare with Similar Methods Ways to add many elements at once:
Java Examples 1. Values and arrays 2. Return value with a set 3. What fails 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.
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);
}
} true [milk, eggs, bread]
[milk, eggs, bread, tea, jam] How It Works Varargs. The three strings after cart are collected into an array behind the scenes and added in the written order.Changed. A list always grows when you add elements, so the result is true.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.
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);
}
} add 1, 3, 2: true [1, 2, 3]
add 1, 2: false [1, 2, 3] How It Works Partly new. 3 is already present, but 1 and 2 are new, so the set changes and the call returns true.Sorted storage. A TreeSet keeps its elements in ascending order, so it prints [1, 2, 3] regardless of insertion order.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.
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);
}
} Arrays.asList: UnsupportedOperationException
null element: NullPointerException
Deque kept: [x] How It Works Fixed size. Arrays.asList returns a list whose length is tied to its array, so adding "c" throws UnsupportedOperationException.One at a time. addAll calls deque.add for each value in turn: "x" succeeds first.Stops at null. ArrayDeque rejects null, so the second add throws and "y" is never reached.Not all-or-nothing. The deque still holds "x", which shows the earlier additions are not undone.Frequently Asked Questions What does Collections.addAll() return? true if the collection changed as a result of the call, false if nothing was added.
Can I pass an array to Collections.addAll()? Yes. The elements parameter is varargs, so an object array such as String[] can be passed directly.
What is the difference between Collections.addAll() and List.addAll()? Collections.addAll takes loose values or an array. list.addAll takes another Collection.
Why does Collections.addAll() throw UnsupportedOperationException? The target collection cannot grow, for example a list from Arrays.asList or an unmodifiable view.
Is Collections.addAll() all-or-nothing? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion