Program to convert a Vector to List in Java


Let’s say the following is our vector with values −

Vector<String> v = new Vector<String>();
v.add("20");
v.add("40");
v.add("60");
v.add("80");
v.add("100");

Now, convert the above Vector to List −

List<String>myList = new ArrayList<String>(v);

Example

Following is the program to convert a Vector to List in Java −

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      Vector<String> v = new Vector<String>();
      v.add("20");
      v.add("40");
      v.add("60");
      v.add("80");
      v.add("100");
      v.add("120");
      v.add("140");
      v.add("160");
      v.add("200");
      System.out.println("Vector elements = " + v);
      List<String>myList = new ArrayList<String>(v);
      System.out.println("List (Vector to List) = " + myList);
   }
}

Output

Vector elements = [20, 40, 60, 80, 100, 120, 140, 160, 200]
List (Vector to List) = [20, 40, 60, 80, 100, 120, 140, 160, 200]

Example

Let us see another example −

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      Vector<String> v = new Vector<String>();
      v.add("20");
      v.add("40");
      v.add("60");
      v.add("80");
      v.add("100");
      v.add("120");
      v.add("140");
      v.add("160");
      v.add("200");
      System.out.println("Vector elements = " + v);
      List<String>myList = Collections.list(v.elements());
      System.out.println("List (Vector to List) = " + myList);
   }
}

Output

Vector elements = [20, 40, 60, 80, 100, 120, 140, 160, 200]
List (Vector to List) = [20, 40, 60, 80, 100, 120, 140, 160, 200]

Updated on: 25-Sep-2019

178 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements