Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Get Enumeration Over Java ArrayList

  • Last Updated : 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 package
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
  
// Class
public 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());
    }
}
Output
Perfume
Clothes
Sandal
Jewellery
Watch

Example 2

Java




// Getting Enumeration over Java ArrayList
  
// Importing ArrayList,Collection and Enumeration class
// from java.util package
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
  
// Class
public 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());
    }
}
Output
Java
C++
Kotlin
Scala
Ruby
Python
C#




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!