Program to convert a Vector to List in Java

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

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

Now, convert the above Vector to List −

ListmyList = new ArrayList(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 v = new Vector();
      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);
      ListmyList = new ArrayList(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 v = new Vector();
      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);
      ListmyList = 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: 2019-09-25T07:44:26+05:30

369 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements