If you need to add all the elements of one ArrayList() collection to another, you can use the addAll() method of the Java ArrayList class. In this article, I’m going to explain how this method works and provide some code examples.
ArrayList addAll() method signature
There are two variants of java.util.ArrayList.addAll method. Here they are.boolean addAll(Collection<? extends E> c)
This method returns true if the list was changed as a result of the call, but most importantly, it adds all elements of the specified collection into the end of this list, in the order that they are returned by the specified collection's Iterator. Element c is the list that will be added into your ArrayList.
Also the method throws NullPointerException if the specified collection is null.
boolean addAll(int index, Collection<? extends E> c)
This method inserts all of the elements in the specified collection c into this list, starting at the specified position index.
ArrayList addAll() syntax example
This is what the call to the ArrayList addAll() method looks like in a Java program:myList.addAll(myNewList))
Where myList is the original ArrayList and myNewList is a list, all values of which will be added to myList if they exist.
myList.addAll(2, myNewList))
A variant of the ArrayList addAll() method with two parameters. All the elements from myNewList will be added to myList, and the first element myNewList[0] will become myList[2], myNewList[1] now is myList[3], and so on.The elements from the original list, starting from number 2, are carried over to the tail of the new combo list.
ArrayList addAll() code examples
Now it’s time for some code examples. Let's create a list of our friends, and call it ‘friends’, and add their names there. Say, we started college and made a few new friends at once. It’s a good moment to create a newFriends list for them. Time passed, you made sure that your new friends are real once, so you decided to move them to the main friends list. You can do it using the addAll() method. In the program, we will print to the console the initial list of friends, the list of new friends, the updated list of friends, as well as the boolean value returned by the addAll() method. It returns true if the original list was changed due to the operation of the method and false if this didn’t happen.import java.util.ArrayList;
import java.util.List;
public class AddAllDemo {
public static void main(String[] args) {
List<String> friends = new ArrayList<>();
friends.add("Johnny");
friends.add("Ivy");
friends.add("Rick");
System.out.println(friends);
List<String> newFriends = new ArrayList<>();
newFriends.add("Andrew");
newFriends.add("Alex");
newFriends.add("Neil");
System.out.println(newFriends);
//let's print out the value addAll() method returns
//here it's true because list friends was changed
System.out.println(friends.addAll(newFriends));
System.out.println("My new list with new friends added: ");
System.out.println(friends);
}
}
the output is:
[Johnny, Ivy, Rick]
[Andrew, Alex, Neil]
true
My new list with new friends added:
[Johnny, Andrew, Alex, Neil, Ivy, Rick]
If we do the same operation but try to add an empty list to friends, the method will work, but it will return false, not true since the original list has not changed.
import java.util.ArrayList;
import java.util.List;
public class AddAllDemo3 {
public static void main(String[] args) {
List<String> friends = new ArrayList<>();
friends.add("Johnny");
friends.add("Ivy");
friends.add("Rick");
System.out.println(friends);
List<String> newFriends = new ArrayList<>();
System.out.println(newFriends);
System.out.println(friends.addAll(newFriends));
System.out.println("My new list with new friends added: ");
System.out.println(friends);
}
}
The output is:
[Johnny, Ivy, Rick]
[]
false
My new list with new friends added:
[Johnny, Ivy, Rick]
Now let's try the addAll() method with two parameters.
boolean addAll(int index, Collection<? extends E> c)
Let's add new friends to the list again, not in the tail, but in the middle. Let's say right after Johnny, which in our case is element numbered zero.
import java.util.ArrayList;
import java.util.List;
public class AddAllDemo2 {
public static void main(String[] args) {
List<String> friends = new ArrayList<>();
friends.add("Johnny");
friends.add("Ivy");
friends.add("Rick");
System.out.println(friends);
List<String> newFriends = new ArrayList<>();
newFriends.add("Andrew");
newFriends.add("Alex");
newFriends.add("Neil");
System.out.println(newFriends);
System.out.println(friends.addAll(1, newFriends));
System.out.println("My new list with new friends added: ");
System.out.println(friends);
}
}
The expected result of the program's work: Andrew is now number 1 in the combined list, Alex - 2, Neil - 3, and Ivy, which was # 1 before, has moved to 4th position.
[Johnny, Ivy, Rick]
[Andrew, Alex, Neil]
true
My new list with new friends added:
[Johnny, Andrew, Alex, Neil, Ivy, Rick]