Open In App

Java Program to Copy Elements of ArrayList to Vector

Last Updated : 10 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Vector implements List Interface, like ArrayList it also maintains insertion order but it is rarely used in the non-thread environment as it is synchronized, and due to which it gives a poor performance in adding, searching, deleting, and updating of its elements. To copy elements from one collection to others, pass the object of ArrayList to the constructor of Vector while in initialization or copy element-wise using iteration.

Approach 1:

Vector<Integer> vector = new Vector<>(arrayList);
  1. Create a Vector object and while initialization passes the object of ArrayList in the constructor.
  2. Print the Vector.

Below is the implementation of the above approach:

Java




// Copy Elements of ArrayList to
// Java Vector using Constructor
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // create a list of string
        ArrayList<String> L = new ArrayList<String>();
 
        // add elements
        L.add("Rohan");
        L.add("Sangeeta");
        L.add("Ritik");
        L.add("Yogesh");
 
        // create a vector and pass Object
        // ArrayList into the constructor
        Vector<String> V = new Vector<String>(L);
 
        // print vector
        System.out.println(V);
    }
}


Output

[Rohan, Sangeeta, Ritik, Yogesh]

Time Complexity: O(n), where n is the size of ArrayList.

Approach 2:

  1. Create a Vector.
  2. Start iterating the ArrayList.
  3. Add each element of ArrayList into the Vector.
  4. After completion of iteration, print the Vector.

Below is the implementation of the above approach:

Java




// Copy Elements of ArrayList to
// Java Vector by iteration and adding
// each element to the vector
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // create a list of string
        ArrayList<String> L = new ArrayList<String>();
 
        // add elements
        L.add("Rohan");
        L.add("Sangeeta");
        L.add("Ritik");
        L.add("Yogesh");
 
        // create a vector
        Vector<String> V = new Vector<String>();
 
        for (String s : L)
            V.add(s);
 
        // print vector
        System.out.println(V);
    }
}


Output

[Rohan, Sangeeta, Ritik, Yogesh]

Time Complexity: O(n), where n is the size of ArrayList.

Approach 3 : Using addAll() method

addAll() method is used to add all the elements from ArrayList to Vector. To use this method, we have to import the package java.util.Vector. To create ArrayList and Vector, we have to import the package util. So, we can simply import all this by specifying java.util.*. 

Step 1: Declare the ArrayList and add the elements.

Step 2: Declare the Vector with the same type of ArrayList.

Step 3 : Now, specify the method Vector_Name.addAll(ArrayList_Name). It will simply add all the elements from ArrayList to the Vector.

Step 4: Print the Vector Elements.

Below is the implementation of the above approach:

Java




import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
        ArrayList<String> L = new ArrayList<String>();
 
        // add elements
        L.add("geeksforgeeks");
        L.add("learning");
        L.add("platform");
 
        System.out.println("ArrayList Elements : " + L);
        // create a vector
        Vector<String> V = new Vector<String>();
        V.addAll(L); // ArrayList to Vector
        // print vector
        System.out.println("Vector Elements : " + V);
    }
}


Output

ArrayList Elements : [geeksforgeeks, learning, platform]
Vector Elements : [geeksforgeeks, learning, platform]

Approach 4 : Using List.copyOf()

This method is used in copying/adding the elements from ArrayList to Vector. To use this method, we have to import the package java.util.List.copyOf(). To create ArrayList and Vector, we have to import the package util. So, we can simply import all this by specifying java.util.*. 

Step 1: Declare the ArrayList and add the elements.

Step 2: Declare the Vector with the same type of ArrayList.

Step 3 : Now, specify the method List.copyOf(ArrayList_Name) in the Vector constructor. It will simply add/copy all the elements from ArrayList to the Vector.

Step 4: Print the Vector Elements.

Below is the implementation of the above approach:

Java




import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
        ArrayList<String> L = new ArrayList<String>();
 
        // add elements
        L.add("geeksforgeeks");
        L.add("learning");
        L.add("platform");
        System.out.println("ArrayList Elements : " + L);
        // create a vector
        Vector<String> V
            = new Vector<String>(List.copyOf(L));
 
        // print vector
        System.out.println("Vector Elements : " + V);
    }
}


Output

ArrayList Elements : [geeksforgeeks, learning, platform]
Vector Elements : [geeksforgeeks, learning, platform]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads