Wednesday, July 3, 2024
HomeLanguagesJavaConvert Vector to ArrayList in Java

Convert Vector to ArrayList in Java

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 a 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)

Approach 3 : Using addAll()

  1. Declare and Initialize the Vector object with values.
  2. Now, declare the ArrayList.
  3. By using addAll() method, we can simply add all elements from Vector to ArrayList. Declare the vector object in the addAll() method i.e ArrayList_object.addAll(Vector_object).
  4. Print the ArrayList.

Java




// Java Program to Convert Vector to ArrayList
 
import java.util.ArrayList;
import java.util.Vector;
 
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);
 
        // Converting vector to ArrayList
        ArrayList<String> Arrlist = new ArrayList<String>();
        Arrlist.addAll(v);
 
        // Displaying the above ArrayList
        System.out.println("\n ArrayList : " + Arrlist);
    }
}


Output

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

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments