ArrayList addAll() method in Java

In this post, we will look at the addAll() method of the ArrayList class in detail. There are two overloaded implementations of the addAll() method.

Let’s look at the above methods one by one.

public boolean addAll(Collection c)

  • Method declaration – public boolean addAll(Collection c)
  • What does it do? – It will add all of the elements of the specified collection at the end of the ArrayList
  • What does it return? – It will return true after successfully inserting all the elements into the list. Otherwise, it will return false

Here we can pass any class’s object that implements the Collection interface into the addAll() function’s argument but ensure that the collection contains the element of the same type as that of the list. Otherwise, you will encounter a compilation error.

Now, let’s try to add the elements of a LinkedList at the end of our ArrayList using the addAll() method.

public class Codekru {

	public static void main(String[] args) {

		// making a linked list
		LinkedList<String> li = new LinkedList<String>();
		li.add("link1");
		li.add("link2");

		ArrayList<String> al = new ArrayList<String>();

		// appending the elements at the end of the list
		al.add("First");
		al.add("Second");

		// appending the linked list at the end of the arraylist
		al.addAll(li);

		// printing the linked list
		System.out.println("ArrayList content: " + al.toString());

	}
}

Output –

ArrayList content: [First, Second, link1, link2]
Time complexity of the ArrayList addAll(Collection c) method

The time complexity of the addAll() method is O(n+m), where n is the size of the ArrayList and m is the size of the collection to be added.

Q – What if we try to add a null collection using the addAll() method?

Here, we will get the NullPointerException as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) {

		LinkedList<String> li = new LinkedList<String>();
		li = null;

		ArrayList<String> al = new ArrayList<String>();

		// appending the elements at the end of the list
		al.add("First");
		al.add("Second");

		// appending the linked list at the end of the arraylist
		al.addAll(li);

		// printing the linked list
		System.out.println("ArrayList content: " + al.toString());

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Collection.toArray()" because "c" is null
	at java.base/java.util.ArrayList.addAll(ArrayList.java:670)
Q – What if we try to add a collection of a different type?

It will throw us a compilation error.

public class Codekru {

	public static void main(String[] args) {

		LinkedList<Integer> li = new LinkedList<Integer>();
		li.add(1);
		li.add(2);

		ArrayList<String> al = new ArrayList<String>();

		// appending the elements at the end of the list
		al.add("First");
		al.add("Second");

		// appending the linked list at the end of the arraylist
		al.addAll(li);

		// printing the linked list
		System.out.println("ArrayList content: " + al.toString());

	}
}

Output –

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The method addAll(Collection<? extends String>) in the type ArrayList<String> is not applicable for the arguments (LinkedList<Integer>)

public boolean addAll(int index, Collection c)

  • Method declaration – public boolean addAll(int index, Collection c)
  • What does it do? – It will help add all elements of the collection in the ArrayList at the specified index. Remember. here, the indexing starts from 0, so if we want to add the element at the 3rd position, then we have to pass 2 as an index into the arguments.
  • What does it return? – It will return true after the successful insertion of the elements; otherwise, it will return false.

Now, let’s again try to add the linked list into our ArrayList but at a specified index this time.

public class Codekru {

	public static void main(String[] args) {

		// making a linked list
		LinkedList<String> li = new LinkedList<String>();
		li.add("link1");
		li.add("link2");

		ArrayList<String> al = new ArrayList<String>();

		// appending the elements at the end of the list
		al.add("First");
		al.add("Second");

		// inserting the linked list at the 2nd position,
		// by passing 1 as an index into the arguments
		al.addAll(1, li);

		// printing the linked list
		System.out.println("ArrayList content: " + al.toString());

	}
}

Output –

ArrayList content: [First, link1, link2, Second]
Time complexity of the ArrayList addAll(int index, Collection c) method

Its time complexity is also O(n+m), where n is the size of the ArrayList and m is the size of the collection to be added.

Q – What if we pass the index as < 0 or an index > size of the ArrayList?

It will give IndexOutOfBoundsException as illustrated by the below program

public class Codekru {

	public static void main(String[] args) {

		// making a linked list
		LinkedList<String> li = new LinkedList<String>();
		li.add("link1");
		li.add("link2");

		ArrayList<String> al = new ArrayList<String>();

		// appending the elements at the end of the list
		al.add("First");
		al.add("Second");

		// passing index as 3 which is greater than the size of the arraylist
		al.addAll(3, li);

		// printing the linked list
		System.out.println("ArrayList content: " + al.toString());

	}
}

Output –

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
	at java.base/java.util.ArrayList.rangeCheckForAdd(ArrayList.java:756)
	at java.base/java.util.ArrayList.addAll(ArrayList.java:700)

Please visit this link to learn more about the ArrayList class of java and its other functions or methods.

We hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Related Articles –

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *