Java Utility Library

Java Vector - addAll() Method



The java.util.Vector.addAll() method is used to append all elements of the specified collection at the end of the vector. The order of the appended element will be the same as returned by the specified collection's Iterator. This method should not be called while the specified collection is modified. This may cause undefined behavior and return wrong result.

Syntax

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

Here, E is the type of element maintained by the container.


Parameters

c Specify the collection containing all elements which need to be added in the vector.

Return Value

Returns true if the vector changed as a result of the call, else returns false.

Exception

Throws NullPointerException, if the specified collection is null.

Example:

In the example below, the java.util.Vector.addAll() method is used to append all elements of the vector vec at the end of vector MyVector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();
    Vector<Integer> vec = new Vector<Integer>();

    //populating MyVector 
    MyVector.add(10);
    MyVector.add(20);
    MyVector.add(30);

    //populating vec 
    vec.add(100);
    vec.add(200);

    //printing vector
    System.out.println("Before method call, MyVector contains: " + MyVector); 

    MyVector.addAll(vec);
    
    //printing vector
    System.out.println("After method call, MyVector contains: " + MyVector);      
  }
}

The output of the above code will be:

Before method call, MyVector contains: [10, 20, 30]
After method call, MyVector contains: [10, 20, 30, 100, 200]

❮ Java.util - Vector