Skip to content
Related Articles

Related Articles

Java Program to Create ArrayList From Enumeration
  • Last Updated : 05 Feb, 2021
GeeksforGeeks - Summer Carnival Banner

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:

  1. Create a vector and add elements to it.
  2. Use the element() method of vector to get the enumeration of the vector element.
  3. Using the list(Enumeration e) method of the Collections to get the ArrayList.

Java




// Java program to Create Java ArrayList From Enumeration
  
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;
  
class GFG {
  
    public static void main(String[] arg)
    {
  
        // creating and adding elements to Vector
        Vector<String> v = new Vector<String>();
        v.add("Geeks");
        v.add("For");
        v.add("Geeks");
        v.add("2020");
        v.add("2021");
  
        // Displaying vector elements
        System.out.println("Elements in vector : " + v);
  
        // getting enumeration of the vector element
        Enumeration<String> elementsEnumeration = v.elements();
  
        //  list(Enumeration e) method returns an ArrayList
        //  containing the elements returned by the
        //  specified Enumeration
        ArrayList<String> arrayList
            = Collections.list(elementsEnumeration);
  
        // Displaying arraylist element
        System.out.println("Elements in arraylist : "
                           + arrayList);
    }
}

Output:

Elements in vector : [Geeks, For, Geeks, 2020, 2021]
Elements in arraylist : [Geeks, For, Geeks, 2020, 2021]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :