Program to Convert a Vector to List in Java

Given a Vector, the task is to Convert Vector to List in Java

Examples:

Input: Vector: [1, 2, 3, 4, 5]
Output: List: [1, 2, 3, 4, 5]

Input : Vector = [a, b, c, d, e, f]
Output : List  = [a, b, c, d, e, f]
  • Using Collections.list() method

    Syntax:

    List list = Collections.list(vec.elements());
    

    Approach:

    1. Get the Vector
    2. Convert into list using Collections.list(vector.elements()) method, which returns a List of objects.
    3. Print the List

    Below is the implementation of the above approach:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to
    // convert vector to List
      
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create a Vector of String elements
            Vector<String> vec = new Vector<String>();
      
            // Adding values of Vector
            vec.add("1");
            vec.add("2");
            vec.add("3");
            vec.add("4");
            vec.add("5");
      
            // print Vector elements
            System.out.println("Vector: " + vec);
      
            // Convert Vector to List
            List<String>
                list = Collections.list(vec.elements());
      
            // print List Elements
            System.out.println("List:" + list);
        }
    }

    chevron_right

    
    

    Output:

    Vector: [1, 2, 3, 4, 5]
    List:[1, 2, 3, 4, 5]
    
  • Using Collection.unmodifiableList()

    Syntax:

    List list = Collections.unmodifiableList(vector);
    

    Approach:

    1. Get the Vector
    2. Convert into list using Collections.unmodifiableList(vector) method, which returns an immutable List of objects.
    3. Print the List

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to
    // convert vector to List
      
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create a Vector of String elements
            Vector<String> vec = new Vector<String>();
      
            // Adding values of Vector
            vec.add("1");
            vec.add("2");
            vec.add("3");
            vec.add("4");
            vec.add("5");
      
            // print Vector elements
            System.out.println("Vector: " + vec);
      
            // Convert Vector to List
            List<String>
                list = Collections.unmodifiableList(vec);
      
            // print List Elements
            System.out.println("List:" + list);
        }
    }

    chevron_right

    
    

    Output:

    Vector: [1, 2, 3, 4, 5]
    List:[1, 2, 3, 4, 5]
    
  • Using constructor

    Syntax:

    List list = new ArrayList(vector);
    

    Approach:

    1. Get the Vector
    2. Create a List from the Vector by passing the vector as the parameter.
    3. Print the List

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to
    // convert vector to List
      
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create a Vector of String elements
            Vector<String> vec = new Vector<String>();
      
            // Adding values of Vector
            vec.add("1");
            vec.add("2");
            vec.add("3");
            vec.add("4");
            vec.add("5");
      
            // print Vector elements
            System.out.println("Vector: " + vec);
      
            // Convert Vector to List
            List<String>
                list = new ArrayList<String>(vec);
      
            // print List Elements
            System.out.println("List:" + list);
        }
    }

    chevron_right

    
    

    Output:

    Vector: [1, 2, 3, 4, 5]
    List:[1, 2, 3, 4, 5]
    

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.