How to convert a Vector to a List in Java

In this tutorial, we'll see how to convert a Vector to a List.

Vector is a concrete class that implements the java.util.List, so technically it is always considered a List. You can write the following code to create a Vector:

List list = new Vector(); 

Or for a generic declaration, suppose a Vector of type String:

List< String> list = new Vector< String> (); 

In case you have already created a Vector and you want to convert it to a List which is a more concrete implementation, you can convert it by calling the method Collections.list(vector.elements()) which returns a List.

In this example, we assume that we have a Vector that contains elements of type String. However, if you want to have a different type then, you just have to change the generic type in the code.

import java.util.Collections; 
import java.util.List;
import java.util.Vector;

public class VectorToList {

public static void main(String[] args) {
Vector vec = new Vector();
//add objects
vec.add("obj1");
vec.add("obj2");
vec.add("obj3");

System.out.println("Vector Elements:");
for(String e:vec)
System.out.println(e);

//convert Vector to List
List list = Collections.list(vec.elements());
//display List
System.out.println("List Items:");
for(String e:list)
System.out.println(e);
}
}
Runtime:

Vector Elements:
obj1
obj2
obj3
List Elements:
obj1
obj2
obj3
If you If you want to convert Vector to an ArrayList, which is inherited from the List implementation, you can do this:

List newList = new ArrayList(vector); 

Or for a generic version:

List< String> newList = new ArrayList< String> (vector); 
I suggest you read the article that explains better converting a Vector to an ArrayList.