Introduction
The addAll()
method in Java's ArrayList
is a handy tool for adding multiple elements from another collection (like an array, HashSet
, or even another ArrayList
) to your list. It comes in two flavors: one that adds all the elements at the end of the list and another that lets you insert them at a specific index.
Syntax
public boolean addAll(Collection<? extends E> c)
public boolean addAll(int index, Collection<? extends E> c)
Parameters
Parameter | Description |
---|---|
c |
The collection whose elements are to be added to this list. |
index |
The index at which the first element of the specified collection will be inserted. If the index is out of range (index < 0 or index > size), an IndexOutOfBoundsException is thrown. |
Return Value
Both versions of the addAll()
method return a boolean
value. It returns true
if all elements were successfully added; otherwise, it might return false
(though this is rare in typical scenarios).
Examples
Example 1: Adding Elements at the End
This example demonstrates how to use the first form of addAll()
– adding all elements from a collection to the end of an existing ArrayList
.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class AddAllExample1 {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<String> list1 = new ArrayList<>(
Arrays.asList("apple", "banana"));
// Create another collection (an array converted to a List)
List<String> list2 = Arrays.asList("orange", "grape");
// Add all elements from list2 to list1
boolean added = list1.addAll(list2);
System.out.println("Added: " + added); // Should print true.
System.out.println("list1: " + list1); // Print the modified ArrayList
}
}
Added: true
list1: [apple, banana, orange, grape]
In this example, list2
(containing “orange” and “grape”) is appended to the end of list1
. The output shows that all elements were successfully added.
Example 2: Inserting Elements at a Specific Index
This example uses the second form of addAll()
, which allows you to insert the elements from another collection at a specific index in your list. Be careful with this method - it shifts existing elements.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class AddAllExample2 {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<Integer> list1 = new ArrayList<>(
Arrays.asList(1, 2, 3));
// Create another collection
List<Integer> list2 = Arrays.asList(4, 5);
// Insert all elements from list2 at index 1 in list1
list1.addAll(1, list2);
System.out.println("list1: " + list1); // Print the modified ArrayList
}
}
list1: [1, 4, 5, 2, 3]
Here, the elements of list2
(4 and 5) are inserted at index 1 in list1
. The original elements at index 1 and beyond (2 and 3) are shifted to make space for the new elements.
Example 3: Handling Different Types
The addAll()
method is type-safe, thanks to generics. Trying to add an incompatible collection will result in a compile-time error.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class AddAllExample3 {
public static void main(String[] args) {
// Create an ArrayList of Strings
ArrayList<String> stringList = new ArrayList<>(
Arrays.asList("hello", "world"));
// Attempt to add a list of Integers (this will cause a compile-time error)
// List<Integer> intList = Arrays.asList(1, 2);
// stringList.addAll(intList); // Compile-time error: incompatible types
System.out.println("This code won't compile because of type mismatch.");
}
}
The commented-out line demonstrates what happens when you attempt to add a collection with an incompatible type (a list of integers to a list of strings). The compiler will catch this error, preventing runtime issues.