Program to Convert a Vector to List in Java

11 Dec, 2018
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() methodSyntax:List list = Collections.list(vec.elements());
Approach:Get the VectorConvert into list using Collections.list(vector.elements()) method, which returns a List of objects.Print the ListBelow is the implementation of the above approach:// 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);    }}Output:Vector: [1, 2, 3, 4, 5]
List:[1, 2, 3, 4, 5]

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

Approach:
Get the VectorConvert into list using Collections.list(vector.elements()) method, which returns a List of objects.Print the List
Get the Vector
Convert into list using Collections.list(vector.elements()) method, which returns a List of objects.
Print the List
Below is the implementation of the above approach:
// 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);    }}
Vector: [1, 2, 3, 4, 5]
List:[1, 2, 3, 4, 5]

Using Collection.unmodifiableList()Syntax:List list = Collections.unmodifiableList(vector);
Approach:Get the VectorConvert into list using Collections.unmodifiableList(vector) method, which returns an immutable List of objects.Print the ListBelow is the implementation of the above approach:// 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);    }}Output:Vector: [1, 2, 3, 4, 5]
List:[1, 2, 3, 4, 5]

Syntax:
List list = Collections.unmodifiableList(vector);

Approach:
Get the VectorConvert into list using Collections.unmodifiableList(vector) method, which returns an immutable List of objects.Print the List
Get the Vector
Convert into list using Collections.unmodifiableList(vector) method, which returns an immutable List of objects.
Print the List
Below is the implementation of the above approach:
// 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);    }}
Vector: [1, 2, 3, 4, 5]
List:[1, 2, 3, 4, 5]

Using constructorSyntax:List list = new ArrayList(vector);
Approach:Get the VectorCreate a List from the Vector by passing the vector as the parameter.Print the ListBelow is the implementation of the above approach:// 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);    }}Output:Vector: [1, 2, 3, 4, 5]
List:[1, 2, 3, 4, 5]

Syntax:
List list = new ArrayList(vector);

Approach:
Get the VectorCreate a List from the Vector by passing the vector as the parameter.Print the List
Get the Vector
Create a List from the Vector by passing the vector as the parameter.
Print the List
Below is the implementation of the above approach:
// 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);    }}
Vector: [1, 2, 3, 4, 5]
List:[1, 2, 3, 4, 5]

java-list
Java-List-Programs
Java-Vector
Technical Scripter 2018
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Convert Double to Integer in Java
Factory method design pattern in Java