Skip to content
Related Articles

Related Articles

Convert Vector to ArrayList in Java
  • Difficulty Level : Hard
  • Last Updated : 11 Dec, 2020

There are multiple ways to convert vector to ArrayList, using passing the Vector in ArrayList constructor and by using simple vector traversal and adding values to ArrayList.

Approach 1:

  1. Create a Vector.
  2. Add some values in Vector.
  3. Create new ArrayList.
  4. Traverse vector from the left side to the right side.
  5. Start adding each element in ArrayList.

Below is the implementation of the above approach:

Java




// Convert Vector to ArrayList in Java
import java.util.Vector;
import java.util.ArrayList;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a Vector that contain strings
  
        Vector<String> v = new Vector<String>();
  
        // add values in vector
  
        v.add("a");
        v.add("b");
        v.add("c");
        v.add("d");
        v.add("e");
  
        // Display the Vector
  
        System.out.println(" Vector :  " + v);
  
        ArrayList<String> Arrlist = new ArrayList<String>();
  
        // Convert Vector to ArrayList
        for (int i = 0; i < v.size(); i++)
            Arrlist.add(v.get(i));
        // Display ArrayList
        System.out.println("\n ArrayList : " + Arrlist);
    }
}

Time Complexity: O(n)

Approach 2:



  • Create a Vector.
  • Add some values in Vector.
  • Create an ArrayList and pass the Vector in ArrayList Constructor.

Syntax:

ArrayList<String> ArrList = new ArrayList<String>(vector);

Below is the implementation of the above approach:

Java




// Convert Vector to ArrayList in Java
import java.util.Vector;
import java.util.ArrayList;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a Vector that contain strings
  
        Vector<String> v = new Vector<String>();
  
        // add values in vector
  
        v.add("a");
        v.add("b");
        v.add("c");
        v.add("d");
        v.add("e");
  
        // Display the Vector
  
        System.out.println(" Vector :  " + v);
  
        // Convert Vector to ArrayList
        ArrayList<String> Arrlist
            = new ArrayList<String>(v);
  
        // Display ArrayList
        System.out.println("\n ArrayList : " + Arrlist);
    }
}
Output
 Vector :  [a, b, c, d, e]

 ArrayList : [a, b, c, d, e]

Time Complexity: O(n)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :