The addAll() method of the ArrayList class is used to add all elements from a specified collection to an ArrayList. It supports adding elements either at the end of the list or at a specified index, making it useful for merging collections or inserting multiple elements in a single operation.
- Preserves the order of elements in the source collection.
- Returns true if the ArrayList is modified.
- Throws an exception for an invalid insertion index.
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
// Creating an ArrayList and
// adding initial elements
ArrayList<String> l1 = new ArrayList<>();
l1.add("Java");
l1.add("C++");
// Creating another ArrayList
// with elements to be added
ArrayList<String> l2 = new ArrayList<>();
l2.add("C");
// Adding all elements
// from l2 to l1
l1.addAll(l2);
System.out.println("" + l1);
}
}
Output
[Java, C++, C]
Explanation: The addAll() method appends all elements of l2 to the end of l1 while preserving their order.
1. boolean addAll(Collection c)
This version appends all elements of the specified collection to the end of the ArrayList.
Syntax:
boolean addAll(Collection c)
- Parameters: c: The collection containing elements to be added to the ArrayList.
- Return Value: true: If the elements were successfully added.
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
ArrayList<Integer> l1 = new ArrayList<>();
l1.add(10);
l1.add(20);
ArrayList<Integer> l2 = new ArrayList<>();
l2.add(30);
l2.add(40);
// Adding all elements from
// l2 to l1
l1.addAll(l2);
System.out.println("Final ArrayList: " + l1);
}
}
Output
Final ArrayList: [10, 20, 30, 40]
Explanation: In this example, two ArrayList<Integer> objects are created. The first list (l1) contains 10 and 20, while the second list (l2) contains 30 and 40. Calling l1.addAll(l2) appends all elements of l2 to the end of l1, resulting in the combined list [10, 20, 30, 40].
2. boolean addAll(int index, Collection c)
This version inserts all elements from the specified collection into the ArrayList at the specified index. It shifts the current elements and subsequent elements to the right.
Syntax:
boolean addAll(int index, Collection c)
Parameters:
- index: The position at which the elements should be inserted.
- c: The collection containing elements to be added.
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
ArrayList<String> l1 = new ArrayList<>();
l1.add("Java");
l1.add("C++");
l1.add("Python");
ArrayList<String> l2 = new ArrayList<>();
l2.add("C");
l2.add("AI");
// Adding elements of
// l2 at index 2
l1.addAll(2, l2);
System.out.println("" + l1);
}
}
Output
[Java, C++, C, AI, Python]
Explanation: In this example, the first ArrayList (l1) contains "Java", "C++", and "Python", while the second list (l2) contains "C" and "AI". The addAll(2, l2) method inserts all elements of l2 starting at index 2. The existing element "Python" is shifted to the right, resulting in the list [Java, C++, C, AI, Python].
Advantages of ArrayList.addAll() Method
- Adds multiple elements in a single method call.
- Simplifies merging two collections.
- Preserves the order of the inserted elements.
- Supports inserting elements at a specified index.
- Reduces the need for manual loops.