Java ArrayList.addAll()

To add all elements of a collection to an ArrayList in Java, we can use addAll() method of ArrayList class. addAll() method has two variations based on the number of arguments.

In this tutorial, we will learn about the Java ArrayList.addAll(index, collection) and ArrayList.addAll(collection) methods, with the help of examples.

addAll(index, collection)

ArrayList.addAll(index, collection) inserts all of the elements in the specified collection into this ArrayList, starting at the specified position index.

Syntax

The syntax of addAll() method is

</>
Copy
ArrayList.addAll(int index, Collection<? extends E> collection)

where

ParameterDescription
indexThe position in this ArrayList at which the collection has to be inserted.
collectionThe collection containing elements to be added to this ArrayList.

Returns

The method returns boolean value. If the elements of the collection are added to this ArrayList, then addAll() returns true, else it returns false.

Example 1 – addAll(index, collection)

In this example, we will create an ArrayList arrayList1 with some String elements in it. We will take another ArrayList as collection, collection with three elements in it. We will insert all elements collection in arrayList1 at index 2.

Java Program

</>
Copy
import java.util.ArrayList;
import java.util.Arrays;

public class Example {  
	public static void main(String[] args) {
		ArrayList<String> arrayList1 = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
		System.out.println("ArrayList before addAll : " + arrayList1);		

		int index = 2;
		ArrayList<String> collection = new ArrayList<String>(Arrays.asList("e", "f", "g"));
		
		boolean result = arrayList1.addAll(index, collection);
		System.out.println("Are the elements added? " + result);
		System.out.println("ArrayList after addAll  : " + arrayList1);
    }
}

Output

ArrayList before addAll : [a, b, c, d]
Are the elements added? true
ArrayList after addAll  : [a, b, e, f, g, c, d]

addAll(collection)

ArrayList.addAll() appends all of the elements in the specified collection to the end of this ArrayList, in the order that they are returned by the specified collection’s Iterator.

Syntax

The syntax of addAll() method is

ArrayList.addAll(Collection<? extends E> collection)

where

ParameterDescription
collectionThe collection containing elements to be added to this ArrayList.

Returns

The method returns boolean value. If the elements of the collection are added to this ArrayList, then addAll() returns true, else it returns false.

Example 2 – addAll(collection)

In this example, we will create an ArrayList arrayList1 with some String elements in it. We will take another ArrayList as collection, collection with three elements in it. We will the call addAll() method on arrayList1 and pass collection as argument to addAll() method to add all the elements present in the collection collection to arrayList1.

Java Program

</>
Copy
import java.util.ArrayList;
import java.util.Arrays;

public class Example {  
	public static void main(String[] args) {
		ArrayList<String> arrayList1 = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
		System.out.println("ArrayList before addAll : " + arrayList1);		

		ArrayList<String> collection = new ArrayList<String>(Arrays.asList("e", "f", "g"));
		
		boolean result = arrayList1.addAll(collection);
		System.out.println("Are the elements added? " + result);
		System.out.println("ArrayList after addAll  : " + arrayList1);
    }
}

Output

ArrayList before addAll : [a, b, c, d]
Are the elements added? true
ArrayList after addAll  : [a, b, c, d, e, f, g]

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList.addAll() method, and also learnt how to use this method with the help of examples.