Enumerations serve the purpose of representing a group of named constants in a programming language. Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay fixed for all time.
Steps To create an ArrayList from an Enumeration:
- Create a vector and add elements to it.
- Use the element() method of vector to get the enumeration of the vector element.
- Using the list(Enumeration e) method of the Collections to get the ArrayList.
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;
class GFG {
public static void main(String[] arg)
{
Vector<String> v = new Vector<String>();
v.add( "Geeks" );
v.add( "For" );
v.add( "Geeks" );
v.add( "2020" );
v.add( "2021" );
System.out.println( "Elements in vector : " + v);
Enumeration<String> elementsEnumeration = v.elements();
ArrayList<String> arrayList
= Collections.list(elementsEnumeration);
System.out.println( "Elements in arraylist : "
+ arrayList);
}
}
|
Output:
Elements in vector : [Geeks, For, Geeks, 2020, 2021]
Elements in arraylist : [Geeks, For, Geeks, 2020, 2021]