Java ArrayList addAll() Method
The addAll() method adds all elements of the given collection to the arraylist.
The syntax for the addAll() method is:
arraylist.addAll(int index, Collection c)
Note: arraylist is an object of the ArrayList class.
Parameter Description:
index (optional parameter) - represents the index value where the collection elements are to be inserted
c - the collection elements to be inserted
If the index is not passed as an actual parameter, the elements will be appended to the end of the array.
Return Value
Returns true if the elements are successfully inserted.
If the given collection is null, it throws a NullPointerException.
Note: If the index is out of range, the method throws an IndexOutOfBoundsException.
Example
Using the ArrayList addAll() method to insert elements:
Example
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// Create an arraylist
ArrayList<Integer> primeNumbers = new ArrayList<>();
// Add elements to the arraylist
primeNumbers.add(3);
primeNumbers.add(5);
System.out.println("Prime Numbers: " + primeNumbers);
// Create another arraylist
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
// Add all elements of primeNumbers to numbers
numbers.addAll(primeNumbers);
System.out.println("Numbers: " + numbers);
}
}
Executing the above program outputs:
Prime Numbers: [3, 5]
Numbers: [1, 2, 3, 5]
In the above example, we created two arraylists, primeNumbers and numbers.
Notice this line:
numbers.addAll(primeNumbers);
The addAll() method here does not include the optional parameter index. Therefore, all elements of primeNumbers are added to the end of numbers.
Inserting elements at a specified position:
Example
import java.util.ArrayList;
class Main {
public static void main(String[] args){
ArrayList<String> languages1 = new ArrayList<>();
languages1.add("Java");
languages1.add("Python");
System.out.println("ArrayList 1: " + languages1);
// Create another arraylist
ArrayList<String> languages2 = new ArrayList<>();
languages2.add("JavaScript");
languages2.add("C");
System.out.println("ArrayList 2: " + languages2);
// Add all elements of languages1 to languages2 at index 1
languages2.addAll(1, languages1);
System.out.println("Updated ArrayList 2: " + languages2);
}
}
Executing the above program outputs:
ArrayList 1: [Java, Python]
ArrayList 2: [JavaScript, C]
Updated ArrayList 2: [JavaScript, Java, Python, C]
Notice this line:
languages2.addAll(1, languages1);
The addAll()
method here includes an optional parameter index
. Therefore, all elements from array languages1
are added to array languages
at the index position of 1.
Inserting elements from a Set into a dynamic array:
Example
import java.util.ArrayList;
import java.util.HashSet;
class Main {
public static void main(String[] args){
// Create a HashSet of type String
HashSet<String> set = new HashSet<>();
// Add elements to the HashSet
set.add("Java");
set.add("Python");
set.add("JavaScript");
System.out.println("HashSet: " + set);
// Create an ArrayList
ArrayList<String> list = new ArrayList<>();
// Add elements to the ArrayList
list.add("English");
System.out.println("Initial ArrayList: " + list);
// Add all elements from the HashSet to the ArrayList
list.addAll(set);
System.out.println("Updated ArrayList: " + list);
}
}
Executing the above program produces the following output:
HashSet: [Java, JavaScript, Python]
Initial ArrayList: [English]
Updated ArrayList: [English, Java, JavaScript, Python]
In the above example, we created a HashSet named set
and a dynamic array named list
. Note this line:
list.addAll(set);
We used the addAll()
method to add all elements from the HashSet to the dynamic array. Since the optional parameter index
was not provided, all elements are added at the end of the dynamic array.