Monday, October 6, 2025
HomeLanguagesJavaCollections enumeration() method in Java with Examples

Collections enumeration() method in Java with Examples

The enumeration() method of java.util.Collections class is used to return an enumeration over the specified collection. This provides interoperability with legacy APIs that require an enumeration as input.
Syntax: 
 

public static  Enumeration enumeration(Collection c)

Parameters: This method takes the collection c as a parameter for which an enumeration is to be returned.
Return Value: This method returns an enumeration over the specified collection.
Below are the examples to illustrate the enumeration() method
Example 1: 
 

Java




// Java program to demonstrate
// enumeration() method
// for String value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of List<String>
            List<String> arrlist = new ArrayList<String>();
 
            // Adding element to srclst
            arrlist.add("Ram");
            arrlist.add("Gopal");
            arrlist.add("Verma");
 
            // Print the list
            System.out.println("List: " + arrlist);
 
            // creating object of type Enumeration<String>
            Enumeration<String> e = Collections.enumeration(arrlist);
 
            // Print the Enumeration
            System.out.println("\nEnumeration over list: ");
 
            // print the enumeration
            while (e.hasMoreElements())
                System.out.println("Value is: " + e.nextElement());
        }
 
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

List: [Ram, Gopal, Verma]

Enumeration over list: 
Value is: Ram
Value is: Gopal
Value is: Verma

 

Example 2: 
 

Java




// Java program to demonstrate
// enumeration() method
// for Integer value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of List<Integer>
            List<Integer> arrlist = new ArrayList<Integer>();
 
            // Adding element to srclst
            arrlist.add(20);
            arrlist.add(30);
            arrlist.add(40);
 
            // Print the list
            System.out.println("List: " + arrlist);
 
            // creating object of type Enumeration<Integer>
            Enumeration<Integer> e = Collections.enumeration(arrlist);
 
            // Print the Enumeration
            System.out.println("\nEnumeration over list: ");
 
            // print the enumeration
            while (e.hasMoreElements())
                System.out.println("Value is: " + e.nextElement());
        }
 
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

List: [20, 30, 40]

Enumeration over list: 
Value is: 20
Value is: 30
Value is: 40

 

RELATED ARTICLES

Most Popular

Dominic
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6707 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6825 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6779 POSTS0 COMMENTS