Get Enumeration Over Java ArrayList - GeeksforGeeks

13 Jan, 2021
ArrayList class is a re-sizeable array that is present in java.util package. Unlike the built-in arrays, ArrayList can change their size dynamically where elements can be added and removed from an ArrayList. 
In java version 1, enum was not present. With advancement to version, 1.5 enum was introduced in java which is not as same as seen in C/C++ so do the functionality differs. Enum is basically used to constrict the range for example moths, age, or be it a string (considering name of a company) which can not be changed as final values initially itself. In java version 1, enumeration was carried out by interfaces. After version 1.5 it was introduced in java as discussed so simply importing enum class makes working with enum easy simply by importing classes.
ArrayList can be of any type as shown below. The enumeration method of java.util.Collections class is used to return an enumeration over the specified collection.
An ArrayList of integer, string, double, etc.
ArrayList of ArrayLists, or simply an ArrayList of HashMaps.
An ArrayList of any user-defined objects.
Syntax: For declaring an enumeration
public static  Enumeration enumeration(Collection c) ;
Method Used: hasMoreElements() Method
An object that implements the Enumeration interface generates a series of elements, one at a time. hasMoreElements() method of enumeration used to tests if this enumeration contains more elements. If enumeration contains more elements than it will return true else false.
Syntax:
boolean hasMoreElements() ;
Parameters: This method accepts nothing.
Return value: This method returns true if and only if this enumeration object contains at least one more element to provide; false otherwise.
Implementation:
Example 1
Java
// Getting Enumeration over Java ArrayList  // Importing ArrayList,Collection and Enumeration class// from java.util packageimport java.util.ArrayList;import java.util.Collections;import java.util.Enumeration;  // Classpublic class GFG {      // Main driver method    public static void main(String[] args)    {        // Creating object of ArrayList        // String type here- shoppingList        ArrayList<String> shoppingList = new ArrayList<>();          // Adding element to ArrayList        // Custom inputs        shoppingList.add("Perfume");        shoppingList.add("Clothes");        shoppingList.add("Sandal");        shoppingList.add("Jewellery");        shoppingList.add("Watch");          // Creating object of type Enumeration<String>        Enumeration e            = Collections.enumeration(shoppingList);          // Condition check using hasMoreElements() method        while (e.hasMoreElements())              // print the enumeration            System.out.println(e.nextElement());    }}
Perfume
Clothes
Sandal
Jewellery
Watch
Example 2
Java
// Getting Enumeration over Java ArrayList  // Importing ArrayList,Collection and Enumeration class// from java.util packageimport java.util.ArrayList;import java.util.Collections;import java.util.Enumeration;  // Classpublic class GFG {      // Main driver method    public static void main(String[] args)    {        // Creating object of ArrayList<String        ArrayList<String> LanguageList = new ArrayList<>();                // Adding element to LanguageList        // Custom inputs        LanguageList.add("Java");        LanguageList.add("C++");        LanguageList.add("Kotlin");        LanguageList.add("Scala");        LanguageList.add("Ruby");        LanguageList.add("Python");        LanguageList.add("C#");          // Creating object of type Enumeration<String>        Enumeration e            = Collections.enumeration(LanguageList);          // Condition check using hasMoreElements()        while (e.hasMoreElements())              // Print the enumeration            System.out.println(e.nextElement());    }}
Java
C++
Kotlin
Scala
Ruby
Python
C#
Java-ArrayList
Java-Collections
Picked
Java
Java Programs
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Program to print ASCII Value of a character