In this article, we will look at the ArrayList addAll method. ArrayList addAll()
method is used to append all the elements of argument collection to the list at the end.
ArrayList addAll() Method
Similar to the ArrayList add() method, the addAll()
method in ArrayList will take care of number of things automatically for us.
- It will make sure that it has sufficient space. Remember, ArrayList is a dynamic array.
- In case there is no sufficient space, it will add more space to the underlying array by creating a new array and copying old array to the new one.
- Finally, it will add the new collection to the end of the list.
Here is the signature for the ArrayList add method in JDK
public boolean addAll(Collection << ? extends E > c) {
return addAll(this.size, c);
}
public boolean addAll(int index, Collection << ? extends E > c) {
rangeCheckForAdd(index);
int cSize = c.size();
if (cSize == 0)
return false;
checkForComodification();
root.addAll(offset + index, c);
updateSizeAndModCount(cSize);
return true;
}
JDK internally use the size of the List to find the index and add the collection to the end of the existing list.
1. ArrayList addAll(Collection <? extends E> c) Example
The ArrayList addAll(Collection<? extends E> c) method appends all the elements of the collection at the end of the list, in the order that they are returned by the * specified collection’s Iterator. Let’s look at the following example for better understanding.
List < String > list = new ArrayList < > ();
list.add("Sunday");
list.add("Monday");
list.add("Tuesday");
<strong>//output</strong>
<strong>[Sunday, Monday, Tuesday]</strong>
List < String > newList = new ArrayList < > ();
newList.add("Wednesday");
newList.add("Thursday");
newList.add("Friday");
newList.add("Saturday");
//merge both list
list.addAll(newList);
<strong>//output</strong>
<strong>[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]</strong>
Please note that the behavior of this operation is undefined if we changed the specified collection while the operation is in progress
2. Arrays.asList and addAll() Method
The ArrayList addAll()
method will throw an UnsupportedOperationException
if we try to add element to the list generated by Arrays.asList
method. Let’s look at the following example:
List < String > list = Arrays.asList("Sunday", "Monday", "Tuesday");
List < String > newList = new ArrayList < > ();
newList.add("Wednesday");
newList.add("Thursday");
newList.add("Friday");
newList.add("Saturday");
list.addAll(newList);
If we run the above example, you will get the java.lang.UnsupportedOperationException
.This happens because of the following reasons
<a href="https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/Arrays.html#asList(T...)" target="_blank" data-type="URL" data-id="https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/Arrays.html#asList(T...)" rel="noreferrer noopener">Arrays.asList</a>
: Returns a fixed-size list backed by the specified array.- We can’t change the structure, i.e. we can’t add/ change/remove element from the list.
3. ArrayList addAll(int index, Collection<? extends E> c) Example
In case we want to add the collection at a specified location in the ArrayList, we can use the addAll(int index, Collection c)
to add the input to the specific location of the list. This method will perform the following tasks.
- Shifts the element currently at that position (if any).
- Shifts any subsequent elements to the right (increases their indices).
List < String > list = new ArrayList < > ();
list.add("Sunday");
list.add("Monday");
list.add("Tuesday");
<strong>//output</strong>
<strong>[Sunday, Monday, Tuesday]
</strong>
List < String > newList = new ArrayList < > ();
newList.add("Wednesday");
newList.add("Thursday");
newList.add("Friday");
newList.add("Saturday");
//merge both list
list.addAll(1, newList);
<strong>//output
[Sunday, Wednesday, Thursday, Friday, Saturday, Monday, Tuesday]</strong>
4. Complete Example
Let’s see the complete example of both the use cases we discussed in this article:
public class ArraylistAddAllExample {
public static void main(String[] args) {
List < String > list = new ArrayList < > ();
list.add("Sunday");
list.add("Monday");
list.add("Tuesday");
//new list
List < String > newList = new ArrayList < > ();
newList.add("Wednesday");
newList.add("Thursday");
newList.add("Friday");
newList.add("Saturday");
//add list at the end of current list
list.addAll(newList);
//output
//[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
//Add All example at a certain index
List < Integer > integers = new ArrayList < > ();
integers.add(1);
integers.add(100);
integers.add(20);
//create new list
List < Integer > integerList = new ArrayList < > ();
integerList.add(1000);
integerList.add(10);
integers.addAll(1, integerList);
//output
[1, 1000, 10, 100, 20]
}
}
Summary
In this article, we learned about the ArrayList addAll method. We saw how to use the addAll()
method to append all the elements of argument collection to the end of the list. We also saw how to append the collection element to a specific index in the list.