beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – Convert Vector to List example

By Chaitanya Singh | Filed Under: Java Collections

Earlier we shared Vector to ArrayList and Vector to Array conversion. In this tutorial we are gonna see how to convert a Vector to List. The steps are as follows:
1) Create a Vector and populate it
2) Convert it to a List by calling Collections.list(vector.elements()) which returns a List object.

Example

To explain the logic we are assuming that Vector is having elements of String type(). However if you want to have a different type then just change the generics in the below code.

import java.util.Vector;
import java.util.List;
import java.util.Collections;
public class VectorToList {
 
  public static void main(String[] args) {
 
     // Step1: Creating a Vector of String elements
     Vector<String> vector = new Vector<String>();
 
     // Step2: Populating Vector
     vector.add("Tim");
     vector.add("Rock");
     vector.add("Hulk");
     vector.add("Rick");
     vector.add("James");
 
    // Step3: Displaying Vector elements
    System.out.println("Vector Elements :");
    for (String str : vector){
       System.out.println(str);
    }
 
    // Step4: Converting Vector to List
    List<String> list = Collections.list(vector.elements());
 
    // Step 5: Displaying List Elements
    System.out.println("\nList Elements :");
    for (String str2 : list){
       System.out.println(str2);
    }
 }
}

Output:

Vector Elements :
Tim
Rock
Hulk
Rick
James

List Elements :
Tim
Rock
Hulk
Rick
James

As you can see both Vector and List are having same elements after conversion.

References

  • Vector Javadoc
  • Collections Javadoc

Enjoyed this post? Try these related posts

  1. Comparator Interface in Java
  2. How to join/combine two ArrayLists in java
  3. How to copy and add all list elements to ArrayList in Java
  4. Java ArrayList get() Method example
  5. Java ArrayList ensureCapacity() Method example
  6. Java – LinkedList Iterator example

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap