Java ArrayList.addAll() – Add Multiple Items to a List

Java ArrayList.addAll(collection) appends all of the elements of the specified collection at the end of the current ArrayList. The order of appended elements is the same as they are returned by the argument collection’s Iterator. To add a single item to the list, it is preferred to use the ArrayList.add().

Please note that we can add elements of any type in ArrayList, but make the application code more predictable, we should add elements of a certain type only using generics for compile-time type safety. Failing to do so can cause ClassCastException in runtime.

//The existing arraylist
ArrayList<String> arraylist = new ArrayList<>();

//Collection of elements to add into arraylist
List<String> newElements = Arrays.asList("e", "f");

//Appending at the end of arraylist
arraylist.addAll(newElements);			//adding multiple elements

//Appending at the specified index
arraylist.addAll(2, newElements);	//appending the new elements at specified index

1. ArrayList addAll() Method

The addAll() method first ensures that there is sufficient space in the list. If the list does not have space, then it grows the list by adding more spaces in the backing array.

Then addAll() appends new elements to the end of the list or at the specified index position.

public boolean addAll(Collection<? extends E> c);

public boolean addAll(int fromIndex, Collection<? extends E> c);
  • Method Argument – a Collection containing elements to be added to this list. when the fromIndex argument is present, the collection items are inserted at the specified index position.
  • Returns true if this list changed as a result.
  • Throws NullPointerException if the specified collection is null.

2. Examples of Adding a Collection to ArrayList

For demo purposes, we have created two ArrayLists containing strings. The first arraylist contains 4 elements, and the second arraylist contains 2 strings.

ArrayList<String> list1 = new ArrayList<>();    //list 1

list1.add("A");
list1.add("B");
list1.add("C");
list1.add("D");

ArrayList<String> list2 = new ArrayList<>();    //list 2

list2.add("E");
list2.add("F");

2.1. Appending Items to End of ArrayList

By default, the addAll() method appends the elements from the argument collection at the end of this arraylist on which the method is invoked.

For example, the following Java program adds the elements of another list to the current arraylist using addAll(). We are declaring generic list types to ensure type safety in runtime.

list1.addAll(list2);

System.out.println(list1);      //combined list

Program output.

[A, B, C, D, E, F]

2.2. Appending Items at the Specified Position

When we want to append the list items at a specified index, we can pass that index location in the method argument. It can also help in adding the list items at the beginning of this arraylist i.e. prepending the elements.

For example, let us pass the'fromIndex' at which location the method will insert the elements from the specified collection. We are inserting the elements at the index position 2, i.e. into the middle of the current list.

list1.addAll(2, list2);

System.out.println(list1);      //combined list

Program output.

[A, B, E, F, C, D]

3. Conclusion

The ArrayList class is very flexible and provides many convenient methods for adding or removing elements from it. The addAll() is one such method to add multiple elements in a single statement.

Although, if generics are not used, it is the programmer’s responsibility to ensure that the argument collection has the same type of elements as in the current arraylist.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode