Vector addAll() method in Java

In this post, we are going to look at the addAll() method of the Vector class in detail. We have two overloaded implementations of the addAll() method in the Vector class.

Now, let’s look at both of them

public boolean addAll(Collection c)

  • Method declaration – public boolean addAll(Collection<? extends E> c)
  • What does it do? – It appends all of the elements present in the collection ( passed in the argument ) at the end of the vector.
  • What does it return? – It will return true after successful insertion of the collection at the end of the vector.

Here we can pass any class object in the argument that extends the collection interface and that class’s elements will be added at the end of the vector. But make sure that the class’s object contains the same type of elements as that of the vector, otherwise, it will throw you a compilation error.

We will be taking a linked list as it implements the collection interface and will try to add its elements at the end of the vector.

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();

		v.add("first");
		v.add("second");

		System.out.println("Vector before: " + v.toString());

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

		// using addAll method to add all of
		// the linked list elements into the vector
		v.addAll(li);

		System.out.println("Vector after: " + v.toString());
	}
}

Output –

Vector before: [first, second]
Vector after: [first, second, link1, link2]
What if we try to use the collection of a different type than that of vector?

It will throw a compilation error as shown below

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();

		v.add("first");
		v.add("second");

		System.out.println("Vector before: " + v.toString());

		// making a linked list and adding elements to it
		LinkedList<Integer> li = new LinkedList<Integer>();
		li.add(1);
		li.add(2);

		v.addAll(li);

		System.out.println("Vector after: " + v.toString());
	}
}

Output –

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The method addAll(Collection<? extends String>) in the type Vector<String> is not applicable for the arguments (LinkedList<Integer>)
What if we try to use the method on a null vector?

It will throw a NullPointerException in this scenario

public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();

		v = null;  
		
		// making a linked list and adding elements to it
		LinkedList<String> li = new LinkedList<String>();
		li.add("link1");
		li.add("link2");

		v.addAll(li);

		System.out.println("Vector after: " + v.toString());
	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Vector.addAll(java.util.Collection)" because "v" is null

public synchronized boolean addAll(int index, Collection c)

  • Method declaration – public synchronized boolean addAll(int index, Collection<? extends E> c)
  • What does it do? – It adds all of the elements of the collection at the specified index passed in the arguments. Remember, the indexing used here is 0-index, which means, if we have to insert the element at the 3rd position, then we have to pass 2 as an index in the function.
  • What does it return? It will return true on the successful insertion of the elements in the collection.
public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();

		v.add("first");
		v.add("second");

		System.out.println("Vector before: " + v.toString());

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

		// using addAll method to add all of the linked list
		// elements into the vector at index 1 ( or location 2)
		v.addAll(1, li);

		System.out.println("Vector after: " + v.toString());
	}
}

Output –

Vector before: [first, second]
Vector after: [first, link1, link2, second]
What if we passed an index < 0 or an index > size of the vector?
public class Codekru {

	public static void main(String[] args) {

		Vector<String> v = new Vector<String>();

		v.add("first");
		v.add("second");

		System.out.println("Vector before: " + v.toString());

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

		// passing a negative index 
		v.addAll(-1, li);

		System.out.println("Vector after: " + v.toString());
	}
}

Output –

Vector before: [first, second]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
	at java.base/java.util.Vector.addAll(Vector.java:1033)

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

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

Liked the article? Share this on

Leave a Comment

Your email address will not be published.