Read Also: Find length/size of an ArrayList in Java
According to Oracle docs, the addAll(Collection c) method appends all of the elements in the specified collection to the end of the list, in the order that they are returned by the specified collection's iterator.
Syntax:
The syntax for the method is:
public boolean addAll(Collection c)
Parameters:
c: This method takes a single parameter c i.e. collection containing elements to be added to the given ArrayList.
Returns:
It returns true if the list changed as a result of the call.
Exception:
addAll(Collection c) method throws NullPointerException if the specified collection is null.
Append Collection elements to ArrayList with Examples
1. Append Collection elements to the String ArrayList
1. In the below example, we have created an ArrayList of type String and added elements to it by using add(element) method.
2. After that, get the collection elements that are to be added to the ArrayList of type String.
3. Add all the elements of the collection into this ArrayList using the ArrayList.addAll(Collection c) method.
4. That's it, ArrayList with all the elements of the collection has been created.
import java.util.*;
public class AddAllMethodExample {
public static void main(String args[]) {
// ArrayList1 of String type
ArrayList<String> al = new ArrayList<String>();
al.add("California");
al.add("Illinois");
al.add("New York");
al.add("Texas");
System.out.println("ArrayList1 before addAll:"+al);
//ArrayList2 of String Type
ArrayList<String> al2 = new ArrayList<String>();
al2.add("Text1");
al2.add("Text2");
al2.add("Text3");
al2.add("Text4");
//Adding ArrayList2 into ArrayList1
al.addAll(al2);
System.out.println("ArrayList1 after addAll:"+al);
}
}
Output:
ArrayList1 before addAll:[California, Illinois, New York, Texas]
ArrayList1 after addAll:[California, Illinois, New York, Texas, Text1, Text2, Text3, Text4]
2. Append Collection elements to the Integer ArrayList
1. In the below example, we have created an ArrayList of type Integer and added elements to it by using add(element) method.
2. After that, get the collection elements that are to be added to the ArrayList of type Integer.
3. ArrayList with all the elements of the collection has been created after adding all the elements of the collection into this ArrayList using the ArrayList.addAll(Collection c) method.
import java.util.*;
public class AddAllMethodExample2 {
public static void main(String args[]) {
// ArrayList1 of Integer type
ArrayList<Integer> al = new ArrayList<>();
al.add(1111);
al.add(2222);
al.add(3333);
al.add(4444);
System.out.println("ArrayList1 before addAll:"+al);
//ArrayList2 of Integer Type
ArrayList<Integer> al2 = new ArrayList<>();
al2.add(5555);
al2.add(6666);
al2.add(7777);
al2.add(8888);
//Adding ArrayList2 into ArrayList1
al.addAll(al2);
System.out.println("ArrayList1 after addAll:"+al);
}
}
Output:
ArrayList1 before addAll:[1111, 2222, 3333, 4444]
ArrayList1 after addAll:[1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888]
That's all for today. Please mention in the comments if you have any questions related to how to append collection elements to ArrayList in Java with examples.