Complete Java Collections.addAll tutorial with examples. Learn how to use Java Collections Framework.
Last modified: April 20, 2025
The Collections.addAll method is a utility method in Java’s Collections framework. It provides a convenient way to add multiple elements to a collection in a single operation. This method is part of the java.util.Collections class.
Collections.addAll is particularly useful when you need to add several elements to a collection at once. It can accept either an array of elements or a variable number of arguments. The method is type-safe and works with all Collection implementations.
The addAll method signature is: public static boolean addAll(Collection c, T… elements). It adds all specified elements to the given collection. Elements can be provided as an array or as individual arguments.
The method returns true if the collection changed as a result of the call. It throws NullPointerException if either the collection or elements parameter is null. The method is optimized for performance with various collection types.
This example demonstrates the most basic usage of Collections.addAll. We create an empty ArrayList and add several String elements to it using the method. The example shows both the array and varargs forms.
BasicAddAllExample.java
package com.zetcode;
import java.util.ArrayList; import java.util.Collections; import java.util.List;
public class BasicAddAllExample {
public static void main(String[] args) {
List<String> colors = new ArrayList<>();
// Using varargs
Collections.addAll(colors, "Red", "Green", "Blue");
System.out.println("After varargs add: " + colors);
// Using array
String[] moreColors = {"Yellow", "Purple"};
Collections.addAll(colors, moreColors);
System.out.println("After array add: " + colors);
// Check return value
boolean changed = Collections.addAll(colors, "Red");
System.out.println("Collection changed? " + changed);
}
}
This code creates an empty ArrayList and adds elements using both forms of Collections.addAll. The first call uses varargs to add three colors. The second call adds two more colors from an array.
The output shows the collection after each addition. The return value demonstration shows that adding a duplicate element returns false as the collection wasn’t modified (for a Set it would be different).
Collections.addAll works with all Collection implementations. This example demonstrates adding elements to different collection types: ArrayList, HashSet, and LinkedList. Each has different behavior regarding duplicates and ordering.
DifferentCollectionTypes.java
package com.zetcode;
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set;
public class DifferentCollectionTypes {
public static void main(String[] args) {
// ArrayList - maintains insertion order, allows duplicates
List<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList, "A", "B", "A", "C");
System.out.println("ArrayList: " + arrayList);
// HashSet - no order, no duplicates
Set<String> hashSet = new HashSet<>();
Collections.addAll(hashSet, "A", "B", "A", "C");
System.out.println("HashSet: " + hashSet);
// LinkedList - maintains insertion order, allows duplicates
List<String> linkedList = new LinkedList<>();
Collections.addAll(linkedList, "A", "B", "A", "C");
System.out.println("LinkedList: " + linkedList);
}
}
This example shows how Collections.addAll behaves with different collection types. ArrayList and LinkedList maintain insertion order and allow duplicates. HashSet doesn’t maintain order and eliminates duplicates.
The output demonstrates these differences clearly. The same elements added to different collection types produce different results based on each collection’s characteristics.
One common use case for Collections.addAll is adding elements from an array to a collection. This example shows how to efficiently transfer array contents to various collection types. The method handles the conversion automatically.
ArrayToCollection.java
package com.zetcode;
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; import java.util.TreeSet;
public class ArrayToCollection {
public static void main(String[] args) {
Integer[] numbers = {5, 2, 8, 1, 9, 3, 5};
// Add to ArrayList
List<Integer> numberList = new ArrayList<>();
Collections.addAll(numberList, numbers);
System.out.println("ArrayList: " + numberList);
// Add to TreeSet (sorted, no duplicates)
Set<Integer> numberSet = new TreeSet<>();
Collections.addAll(numberSet, numbers);
System.out.println("TreeSet: " + numberSet);
// Partial array add
List<Integer> partialList = new ArrayList<>();
Collections.addAll(partialList, Arrays.copyOfRange(numbers, 2, 5));
System.out.println("Partial add: " + partialList);
}
}
This example demonstrates adding elements from an array to different collection types. We first add all elements from an Integer array to an ArrayList, preserving order and duplicates. Then we add the same elements to a TreeSet, which sorts them and removes duplicates.
The final part shows how to add only a portion of the array using Arrays.copyOfRange. This technique is useful when you need to add only specific elements from an array.
Collections.addAll can be used to combine multiple collections. This example shows how to merge elements from different collections into one. We demonstrate both same-type and mixed-type collections.
CombineCollections.java
package com.zetcode;
import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Set;
public class CombineCollections {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
Collections.addAll(fruits, "Apple", "Banana", "Cherry");
Set<String> vegetables = new LinkedHashSet<>();
Collections.addAll(vegetables, "Carrot", "Broccoli", "Spinach");
// Combine into new List
List<String> produce = new ArrayList<>();
Collections.addAll(produce, fruits.toArray(new String[0]));
Collections.addAll(produce, vegetables.toArray(new String[0]));
System.out.println("Combined produce: " + produce);
// Add array and collection together
String[] moreFruits = {"Orange", "Mango"};
Collections.addAll(produce, moreFruits);
Collections.addAll(produce, vegetables.toArray(new String[0]));
System.out.println("After more additions: " + produce);
}
}
This example demonstrates combining elements from different collections using Collections.addAll. We first create separate collections for fruits and vegetables. Then we combine them into a new List by converting each collection to an array first.
The second part shows adding both an array and another collection to an existing collection. The output shows how the elements are combined while maintaining the insertion order (for List) and allowing duplicates.
This example compares the performance of Collections.addAll with alternative methods of adding multiple elements. We measure the time taken for different approaches to add elements to various collection types.
AddAllPerformance.java
package com.zetcode;
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set;
public class AddAllPerformance {
public static void main(String[] args) {
final int SIZE = 1000000;
Integer[] data = new Integer[SIZE];
Arrays.setAll(data, i -> i);
timeArrayList(data);
timeHashSet(data);
}
private static void timeArrayList(Integer[] data) {
// Method 1: Collections.addAll
List<Integer> list1 = new ArrayList<>();
long start = System.nanoTime();
Collections.addAll(list1, data);
long end = System.nanoTime();
System.out.printf("ArrayList Collections.addAll: %d ms%n",
(end - start) / 1000000);
// Method 2: addAll from another collection
List<Integer> list2 = new ArrayList<>();
start = System.nanoTime();
list2.addAll(Arrays.asList(data));
end = System.nanoTime();
System.out.printf("ArrayList addAll from List: %d ms%n",
(end - start) / 1000000);
}
private static void timeHashSet(Integer[] data) {
// Method 1: Collections.addAll
Set<Integer> set1 = new HashSet<>();
long start = System.nanoTime();
Collections.addAll(set1, data);
long end = System.nanoTime();
System.out.printf("HashSet Collections.addAll: %d ms%n",
(end - start) / 1000000);
// Method 2: Constructor with collection
start = System.nanoTime();
Set<Integer> set2 = new HashSet<>(Arrays.asList(data));
end = System.nanoTime();
System.out.printf("HashSet constructor: %d ms%n",
(end - start) / 1000000);
}
}
This performance test compares different methods of adding elements to collections. For ArrayList, Collections.addAll is compared with addAll from another collection. For HashSet, we compare with the constructor that accepts a collection.
The results will vary by system, but generally show that Collections.addAll is highly optimized. For ArrayList, it’s often faster than other methods. For HashSet, the constructor might be faster as it can optimize capacity.
Collections.addAll works seamlessly with custom objects. This example demonstrates adding instances of a custom class to different collection types. We’ll use a simple Person class with name and age fields.
CustomObjectsExample.java
package com.zetcode;
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects;
public class CustomObjectsExample {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
// Add multiple Person objects
Collections.addAll(people,
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 35)
);
System.out.println("People list:");
people.forEach(System.out::println);
// Add from array
Person[] morePeople = {
new Person("David", 40),
new Person("Eve", 28)
};
Collections.addAll(people, morePeople);
System.out.println("\nAfter adding more people:");
people.forEach(System.out::println);
}
static class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name + " (" + age + ")";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
}
This example shows Collections.addAll working with custom Person objects. We first add several Person instances using varargs, then add more from an array. The Person class includes proper equals and hashCode implementations.
The output demonstrates that custom objects can be added to collections just as easily as built-in types. This makes Collections.addAll versatile for any object type that follows Java’s collection requirements.
This example explores edge cases and exception handling with Collections.addAll. We demonstrate what happens with null collections, null elements, and incompatible types. Proper error handling is crucial for robust code.
EdgeCasesExample.java
package com.zetcode;
import java.util.ArrayList; import java.util.Collections; import java.util.List;
public class EdgeCasesExample {
public static void main(String[] args) {
// 1. Null collection
try {
Collections.addAll(null, "A", "B", "C");
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException for null collection: " + e.getMessage());
}
// 2. Null elements array
List<String> list = new ArrayList<>();
try {
Collections.addAll(list, (String[]) null);
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException for null elements: " + e.getMessage());
}
// 3. Null elements in varargs
try {
Collections.addAll(list, "A", null, "C");
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException for null element: " + e.getMessage());
}
// 4. Incompatible types
List<Integer> numbers = new ArrayList<>();
try {
Collections.addAll(numbers, "String");
} catch (ClassCastException e) {
System.out.println("Caught ClassCastException for incompatible type: " + e.getMessage());
}
// 5. Unmodifiable collection
List<String> unmodifiableList = Collections.unmodifiableList(new ArrayList<>());
try {
Collections.addAll(unmodifiableList, "A", "B");
} catch (UnsupportedOperationException e) {
System.out.println("Caught UnsupportedOperationException for unmodifiable collection: " + e.getMessage());
}
// Successful case
Collections.addAll(list, "X", "Y", "Z");
System.out.println("Successfully added elements: " + list);
}
}
This example demonstrates various edge cases for Collections.addAll. We test:
The output shows the exceptions caught for each case and a successful addition to demonstrate correct usage. Proper exception handling ensures robust code when using Collections.addAll.
This example demonstrates a practical application of Collections.addAll in a data processing service that collects and manages user preferences. The service uses the method to efficiently add multiple preferences to a collection.
PreferenceService.java
package com.zetcode;
import java.util.ArrayList; import java.util.Collections; import java.util.List;
public class PreferenceService {
private final List<String> userPreferences = new ArrayList<>();
public void addPreferences(String... preferences) {
Collections.addAll(userPreferences, preferences);
}
public void addPreferencesFromArray(String[] preferences) {
Collections.addAll(userPreferences, preferences);
}
public List<String> getPreferences() {
return Collections.unmodifiableList(userPreferences);
}
public static void main(String[] args) {
PreferenceService service = new PreferenceService();
// Add preferences using varargs
service.addPreferences("Dark Mode", "Email Notifications", "Auto Save");
System.out.println("Preferences after varargs: " + service.getPreferences());
// Add preferences from array
String[] morePrefs = {"High Contrast", "Two-Factor Auth"};
service.addPreferencesFromArray(morePrefs);
System.out.println("Preferences after array: " + service.getPreferences());
// Demonstrate unmodifiable return
try {
service.getPreferences().add("Invalid");
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify preferences directly: " + e.getMessage());
}
}
}
This example shows a PreferenceService that uses Collections.addAll to add user preferences. The service provides methods to add preferences via varargs or an array and returns an unmodifiable view of the preferences to prevent external modification.
The output shows the preferences after each addition and demonstrates that clients cannot modify the returned collection directly. This pattern is common in APIs where you need to manage collections internally while providing safe access to clients.
Java Collections.addAll Documentation
In this tutorial, we’ve explored Collections.addAll in depth. We’ve covered basic usage, different collection types, array conversions, combining collections, performance, custom objects, edge cases, and practical applications. This method is valuable for efficiently adding multiple elements to collections.
My name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts.
List all Java tutorials.