Linked List – addAll method in Java

In this post, we will be looking at the addAll() method of the Linked List class in Java. addAll() method is used to add the elements of a Collection into the linked list. There are two overloaded implementations of the addAll methods, which are listed below –

Let’s discuss the above methods one by one.

public boolean addAll(Collection c)

  • What does it do? – It will take a collection and append it to the end of our linked list in the order in which the specified collection iterator returns them.
  • Now, what is collection means here? The Collection is an interface in Java, and there are many classes like Vector, Stack, etc., which implement the Collection interface. So we can pass any of those class’s objects in the addAll() method’s argument.
  • What will it return? – It will return true after executing the function successfully.

Now, let’s take an empty linked list, and then we will take a vector and append all of its elements to our linked list. Please note that we are using Vector because it implements the Collection interface internally. You can use any class as long as it implements the Collection interface.

import java.util.LinkedList;
import java.util.Vector;

public class Codekru {

	public static void main(String[] args) {
		LinkedList<String> linkedList = new LinkedList<String>(); // making a linkedList object

		Vector<String> vector = new Vector<String>(); // making a vector object

		// adding elements to vector
		vector.add("hello");
		vector.add("codekru");

		linkedList.addAll(vector); // using addAll method

		System.out.println(linkedList);

	}
}

Output –

[hello, codekru]

Now, let’s try taking a pre-filled linked list and using the same vector. Let’s see what happens then.

import java.util.LinkedList;
import java.util.Vector;

public class Codekru {

	public static void main(String[] args) {
		LinkedList<String> linkedList = new LinkedList<String>(); // making a linkedList object

		// adding elements to linked list
		linkedList.add("Pre");
		linkedList.add("Filled");
		linkedList.add("Linked List");

		Vector<String> vector = new Vector<String>(); // making a vector object

		// adding elements to vector
		vector.add("hello");
		vector.add("codekru");

		linkedList.addAll(vector); // using addAll method

		System.out.println(linkedList);

	}
}

Output –

[Pre, Filled, Linked List, hello, codekru]

So, it is evident from here that it has appended all the elements to the linked list.

Time Complexity of addAll(Collection c)

The average time complexity of addAll(Collection c) is O(n), where n is the collection size that has to be appended at the end of the Linked List.

  • Best Case Scenario – Best case scenario would be when only one element is added at the end of the linked list.
  • Worst Case Scenario – Worst case scenario would be when we have to add multiple elements at the end of the linked list. Then the time complexity would depend on the number of elements that have to be added to the linked list.

So, the average complexity would be O(n), where n is the number of elements within the collection.

public boolean addAll(int index, Collection c)

  • What does it do? – It will insert all elements in the specified collection into the list, starting at the specified position, and will shift the subsequent elements to the right.
  • What will it return? – It will return true after executing the function successfully.

Here the indexing is 1-based means if we pass 3 as an index in the addAll method, it will start inserting the elements at that position itself. Let’s see this with an example –

import java.util.LinkedList;
import java.util.Vector;

public class Codekru {

	public static void main(String[] args) {
		LinkedList<String> linkedList = new LinkedList<String>(); // making a linkedList object

		// adding elements to linked list
		linkedList.add("Pre");
		linkedList.add("Filled");
		linkedList.add("Linked List");

		Vector<String> vector = new Vector<String>(); // making a vector object

		// adding elements to vector
		vector.add("hello");
		vector.add("codekru");

		linkedList.addAll(2, vector); // using addAll method

		System.out.println(linkedList);

	}
}

Output –

[Pre, Filled, hello, codekru, Linked List]

Do You Know? addAll(Collection c) method uses addAll(int index,Collection c) internally where index = size of linked list.

Time Complexity of addAll(int index, Collection c)

The average time complexity of addAll(int index, Collection c) is also O(n), where n is the number of elements added within the linked list at the specified position.

The What If scenarios

Q – What if we passed null as an argument in the addAll() method?

We will get a NullPointerException, as shown in the below program.

public class Codekru {

	public static void main(String[] args) {
		LinkedList<String> linkedList = new LinkedList<String>(); // making a linkedList object

		// adding elements to linked list
		linkedList.add("Pre");
		linkedList.add("Filled");
		linkedList.add("Linked List");

		linkedList.addAll(null); // using addAll method

		System.out.println(linkedList);

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Collection.toArray()" because "c" is null
	at java.base/java.util.LinkedList.addAll(LinkedList.java:412)
	at java.base/java.util.LinkedList.addAll(LinkedList.java:391)
Q – What if we passed index > size in the function’s argument?

In this scenario, we will get an IndexOutOfBoundsException. As we can see in the below example, the linked list’s size was 3, and we got an IndexOutOfBoundsException when we passed 4 as an index in the function’s argument.

public class Codekru {

	public static void main(String[] args) {
		LinkedList<String> linkedList = new LinkedList<String>(); // making a linkedList object

		// adding elements to linked list
		linkedList.add("Pre");
		linkedList.add("Filled");
		linkedList.add("Linked List");

		Vector<String> vector = new Vector<String>(); // making a vector object

		// adding elements to vector
		vector.add("hello");
		vector.add("codekru");

		linkedList.addAll(4, vector); // using addAll method

		System.out.println(linkedList);

	}
}

Output –

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 3
	at java.base/java.util.LinkedList.checkPositionIndex(LinkedList.java:564)
	at java.base/java.util.LinkedList.addAll(LinkedList.java:410)

Please visit this link if you want to know more about the Linked List class in java and its other functions or methods.

Hope you liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.