The addAll()
method adds all the elements of a collection to the arraylist.
Example
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// create an arraylist
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
System.out.println("Languages: " + languages);
// create another arraylist
ArrayList<String> programmingLang = new ArrayList<>();
// add all elements from languages to programmingLang
programmingLang.addAll(languages);
System.out.println("Programming Languages: " + programmingLang);
}
}
// Output: Languages: [Java, Python]
// Programming Languages: [Java, Python]
Syntax of ArrayList addAll()
The syntax of the addAll()
method is:
arraylist.addAll(int index, Collection c)
Here, arraylist is an object of the ArrayList
class.
addAll() Parameters
The ArrayList addAll()
method can take two parameters:
- index (optional) - index at which all elements of a collection is inserted
- collection - collection that contains elements to be inserted
If the index
parameter is not passed the collection is appended at the end of the arraylist.
addAll() Return Value
- returns
true
if the collection is successfully inserted into the arraylist - raises
NullPointerException
if the specified collection is null - raises
IndexOutOfBoundsException
if theindex
is out of range
Example 1: Inserting Elements using ArrayList addAll()
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// create an arraylist
ArrayList<Integer> primeNumbers = new ArrayList<>();
// add elements to 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 from primeNumbers to numbers
numbers.addAll(primeNumbers);
System.out.println("Numbers: " + numbers);
}
}
Output
Prime Numbers: [3, 5] Numbers: [1, 2, 3, 5]
In the above example, we have created two arraylists named primeNumbers and numbers. Notice the line,
numbers.addAll(primeNumbers);
Here, the addAll()
method does not contain the optional index
parameter. Hence, all elements from the arraylist primeNumbers are added at the end of the arraylist numbers.
Note: We have used the add()
method to add single elements to arraylist. To learn more, visit Java ArrayList add().
Example 2: Inserting Elements to the Specified Position
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 elements from languages1 to languages2 at index 1
languages2.addAll(1, languages1);
System.out.println("Updated ArrayList 2: " + languages2);
}
}
Output
ArrayList 1: [Java, Python] ArrayList 2: [JavaScript, C] Updated ArrayList 2: [JavaScript, Java, Python, C]
In the above example, we have two arraylists named languages1 and languages2. Notice the line,
languages2.addAll(1, languages1);
Here, the addAll()
contains the optional index parameter. Hence, all elements from the arraylist languages1 are added to languages at index 0.
Example 3: Inserting Elements from Set to ArrayList
import java.util.ArrayList;
import java.util.HashSet;
class Main {
public static void main(String[] args) {
// create a hashset of String type
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 element to arraylist
list.add("English");
System.out.println("Initial ArrayList: " + list);
// Add all elements from hashset to arraylist
list.addAll(set);
System.out.println("Updated ArrayList: " + list);
}
}
Output
Set: [Java, JavaScript, Python] Initial ArrayList: [English] Updated ArrayList: [English, Java, JavaScript, Python]
In the above example, we have created a hashset named set and an arraylist named list. Notice the line,
list.addAll(set);
Here, we have used the addAll()
method to add all the elements of the hashset to the arraylist. The optional index parameter is not present in the method. Hence, all elements are added at the end of the arraylist.