The addAll()
method is a powerful and convenient feature of Java ArrayList
class that allows you to add all elements from a specified collection to an existing ArrayList
. Whether you want to append multiple elements at the end or insert them at a specific position, addAll()
simplifies these operations with just one method call.
What is the addAll() Method?
The addAll() method is used to add every element from another collection (such as another ArrayList
, LinkedList
, or any class that implements the Collection
interface) into the current ArrayList
.
It comes in two common variants:
addAll(Collection<? extends E> c)
addAll(int index, Collection<? extends E> c)
Where E
is the type of elements stored in the list.
Syntax
boolean addAll(Collection<? extends E> c)
boolean addAll(int index, Collection<? extends E> c)
Parameters
Parameter | Description |
---|---|
Collection<? extends E> c | The collection whose elements are to be added. |
int index | (optional) The position at which to insert the first element from the specified collection. |
How does addAll() Works?
- When called without an index,
addAll()
appends all elements of the provided collection to the end of the list. - When called with an index, it inserts all elements starting at the given position, shifting existing elements to the right.
- The method returns
true
if theArrayList
changed as a result of the call.
Important Notes
- If the specified
index
is out of bounds (less than 0 or greater than list size), it throws anIndexOutOfBoundsException
. - If the specified collection is
null
, aNullPointerException
is thrown. - The order of elements in the appended collection is preserved.
Examples of the addAll() Method
Example 1: Append Elements at the End
import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList<String> list1 = new ArrayList<>(); list1.add("A"); list1.add("B"); list1.add("C"); ArrayList<String> list2 = new ArrayList<>(); list2.add("D"); list2.add("E"); // Append all elements of list2 to list1 list1.addAll(list2); System.out.println(list1); } }
Output:
[A, B, C, D, E]
Example 2: Insert Elements at a Specific Position
import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList<String> list1 = new ArrayList<>(); list1.add("Red"); list1.add("Green"); list1.add("Blue"); ArrayList<String> newColors = new ArrayList<>(); newColors.add("Yellow"); newColors.add("Orange"); // Insert newColors starting at index 1 list1.addAll(1, newColors); System.out.println(list1); } }
Output:
[Red, Yellow, Orange, Green, Blue]
When to Use addAll() Method?
- Merging two lists efficiently without looping manually.
- Adding multiple elements at once, improving performance and code readability.
- Inserting entire collections at a specific point in a list for flexible data structures.
Summary
Aspect | Details |
---|---|
Purpose | To add a collection of elements to an ArrayList |
Overloaded Methods | addAll(Collection c) , addAll(int index, Collection c) |
Returns | true if the list was modified |
Throws | IndexOutOfBoundsException , NullPointerException |
Use Case | Combine lists, batch insertions |
Conclusion
The ArrayList
‘s addAll()
method is an essential tool for handling bulk additions to lists in Java. It saves developers the trouble of writing manual loops and simplifies code while maintaining performance and order. Whether appending at the end or inserting at a specific index, addAll()
lets you manage collections efficiently and cleanly.