This example shows how to convert Vector to an ArrayList and ArrayList to a Vector in Java. This example also shows how to convert using the constructor and addAll method.
The ArrayList and Vector classes are implementations of the List interface.
How to convert Vector to ArrayList in Java?
The ArrayList class in Java provides a constructor that accepts a Collection object.
1 |
public ArrayList(Collection<? extends E> collection) |
This constructor creates a new array list object containing all the elements of the specified collection object. We can use this constructor to convert a vector object to an ArrayList object as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.ArrayList; import java.util.Vector; public class ConvertVectorArrayList { public static void main(String[] args) { Vector<String> vColors = new Vector<String>(); vColors.add("Red"); vColors.add("Green"); vColors.add("Blue"); /* * To convert a vector object to an ArrayList, * use the constructor and pass the vector object */ ArrayList<String> listColors = new ArrayList<String>( vColors ); System.out.println("ArrayList contains: " + listColors); } } |
Output
1 |
ArrayList contains: [Red, Green, Blue] |
We can also create an empty array list object and then use the addAll
method of the ArrayList class to add all elements of the vector to the empty array list object.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Vector<String> vColors = new Vector<String>(); vColors.add("Red"); vColors.add("Green"); vColors.add("Blue"); //new empty array list object ArrayList<String> listColors = new ArrayList<String>(); //use the addAll method listColors.addAll(vColors); System.out.println("ArrayList contains: " + listColors); |
Output
1 |
ArrayList contains: [Red, Green, Blue] |
How to convert ArrayList to Vector in Java?
The Vector class also provides a constructor that accepts a Collection object as a parameter.
1 |
public Vector(Collection<? extends E> collection) |
It creates a new vector object containing all the elements of the specified collection object. We can use this constructor to convert an ArrayList to a Vector object as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ArrayList<Integer> listNumbers = new ArrayList<Integer>(); listNumbers.add(100); listNumbers.add(101); listNumbers.add(102); /* * To convert ArrayList to Vector, use * the constructor and pass the * arraylist object */ Vector<Integer> vNumbers = new Vector<Integer>( listNumbers ); System.out.println("Vector contains: " + vNumbers); |
Output
1 |
Vector contains: [100, 101, 102] |
We can also create a new and empty object of the Vector class first and then add elements of the ArrayList to the new vector object using the addAll
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
ArrayList<Integer> listNumbers = new ArrayList<Integer>(); listNumbers.add(100); listNumbers.add(101); listNumbers.add(102); //create empty vector object Vector<Integer> vNumbers = new Vector<Integer>(); //use the addAll method vNumbers.addAll(listNumbers); System.out.println("Vector contains: " + vNumbers); |
Output
1 |
Vector contains: [100, 101, 102] |
This example is a part of the Java Vector Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation