Collections list() method in Java with Examples
Last Updated :
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:
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
List<String> arrlist = new ArrayList<String>();
Vector<String> v = new Vector<String>();
v.add( "A" );
v.add( "B" );
v.add( "C" );
v.add( "D" );
v.add( "E" );
System.out.println( "Current list : " + arrlist);
Enumeration<String> e = v.elements();
arrlist = Collections.list(e);
System.out.println( "Returned list: " + arrlist);
}
catch (IllegalArgumentException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Current list : []
Returned list: [A, B, C, D, E]
Example 2:
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
List<Integer> arrlist = new ArrayList<Integer>();
Vector<Integer> v = new Vector<Integer>();
v.add( 10 );
v.add( 20 );
v.add( 30 );
v.add( 40 );
v.add( 50 );
System.out.println( "Current list : " + arrlist);
Enumeration<Integer> e = v.elements();
arrlist = Collections.list(e);
System.out.println( "Returned list: "
+ arrlist);
}
catch (IllegalArgumentException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Current list : []
Returned list: [10, 20, 30, 40, 50]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...