Java Utility Library

Java Collections - list() Method



The java.util.Collections.list() method returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.

Syntax

public static <T> ArrayList<T> list(Enumeration<T> e)

Here, T is the type of element returned by the enumeration.


Parameters

e Specify the enumeration providing elements for the returned array list.

Return Value

Returns an array list containing the elements returned by the specified enumeration.

Exception

NA.

Example:

In the example below, the java.util.Collections.list() method is used to get an array list containing the elements returned by the specified enumeration.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a list object
    List<Integer> MyList = new ArrayList<Integer>();

    //creating a vector object
    Vector<Integer> Vec = new Vector<Integer>();

    //populating vector
    Vec.add(10);
    Vec.add(20);
    Vec.add(30);
    Vec.add(40);
    Vec.add(50);
    
    //creating enumeration from a vector
    Enumeration<Integer> e = Vec.elements();

    //get the list from enumeration
    MyList = Collections.list(e);

    //printing the list
    System.out.println("MyList contains: " + MyList); 
  }
}

The output of the above code will be:

MyList contains: [10, 20, 30, 40, 50]

❮ Java.util - Collections