Collections list() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Current list : []
Returned list: [A, B, C, D, E]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Current list : []
Returned list: [10, 20, 30, 40, 50]


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.



Improved By : Akanksha_Rai