Collections list() method in Java with Examples - GeeksforGeeks

15 May, 2019
The list() method of java.util.Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that require collections.
Syntax:
public static ArrayList list(Enumeration e)
Parameters: This method takes enumeration e as a parameter providing elements for the returned array list.
Return Value: This method returns an array list containing the elements returned by the specified enumeration.
Below are the examples to illustrate the list() method
Example 1:
// Java program to demonstrate// list() method// for String value  import java.util.*;  public class GFG1 {    public static void main(String[] argv) throws Exception    {        try {              // creating object of List<String>            List<String> arrlist = new ArrayList<String>();              // creating object of Vector<String>            Vector<String> v = new Vector<String>();              // Adding element to Vector v            v.add("A");            v.add("B");            v.add("C");            v.add("D");            v.add("E");              // printing the list            System.out.println("Current list : " + arrlist);              // creating Enumeration            Enumeration<String> e = v.elements();              // getting arrlist of specified Enumeration            // using list() method            arrlist = Collections.list(e);              // printing the arrlist            System.out.println("Returned list: " + arrlist);        }          catch (IllegalArgumentException e) {            System.out.println("Exception thrown : " + e);        }    }}
Current list : []
Returned list: [A, B, C, D, E]

Example 2:
// Java program to demonstrate// list() method// for Integer value  import java.util.*;  public class GFG1 {    public static void main(String[] argv) throws Exception    {        try {              // creating object of List<String>            List<Integer> arrlist = new ArrayList<Integer>();              // creating object of Vector<String>            Vector<Integer> v = new Vector<Integer>();              // Adding element to Vector v            v.add(10);            v.add(20);            v.add(30);            v.add(40);            v.add(50);              // printing the list            System.out.println("Current list : " + arrlist);              // creating Enumeration            Enumeration<Integer> e = v.elements();              // getting arrlist of specified Enumeration            // using list() method            arrlist = Collections.list(e);              // printing the arrlist            System.out.println("Returned list: "                               + arrlist);        }          catch (IllegalArgumentException e) {            System.out.println("Exception thrown : " + e);        }    }}
Current list : []
Returned list: [10, 20, 30, 40, 50]

Akanksha_Rai
Java - util package
Java-Collections
Java-Functions
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Initialize an ArrayList in Java
Interfaces in Java
Multidimensional Arrays in Java
Stream In Java
Stack Class in Java
Singleton Class in Java
Set in Java
Overriding in Java
Initializing a List in Java
Collections in Java