Java Collections emptyEnumeration()​ Method with Examples

Last Updated : 4 Aug, 2026

The Collections.emptyEnumeration() method in Java returns an immutable empty Enumeration that contains no elements. It is useful when a method needs to return an Enumeration but there are no elements to provide, avoiding the need to return null.

  • Returns a reusable empty Enumeration.
  • Safe to use when no elements are available.
  • Helps avoid NullPointerException by returning an empty object instead of null.
Java
import java.util.*;

public class EmptyEnumerationExample {
    public static void main(String[] args) {

        Enumeration<String> enumeration =
                Collections.emptyEnumeration();

        System.out.println(enumeration.hasMoreElements());
    }
}

Output
false

Explanation: The enumeration contains no elements, so hasMoreElements() returns false.

Syntax

public static <T> Enumeration<T> emptyEnumeration()

  • Parameters: This method does not accept any parameters.
  • Return Value: Returns an empty Enumeration containing no elements.
  • Exceptions: This method does not throw any exceptions.

Example: Checking Whether an Empty Enumeration Has Elements

Java
import java.util.*;

public class GFG {
    // main method  
    public static void main(String[] args)
    {    
        // create an empty enumeration
        Enumeration<String> obj
            = Collections.emptyEnumeration();
      
        // check more elements or not
        System.out.println(obj.hasMoreElements());
    }
}

Output
false

Explanation: Since the enumeration is empty, hasMoreElements() returns false.

Example: Iterating Over an Empty Enumeration

Java
import java.util.*;

public class GFG {
  
    // main method
    public static void main(String[] args)
    {
        // create an array list
        List<String> data = new ArrayList<String>();
      
        // add elements to the list
        data.add("java");
        data.add("python");
        data.add("php");
        data.add("html/css");
      
        // create enumeration object
        Enumeration<String> enm
            = Collections.emptyEnumeration();
      
        // get the elements
        while (enm.hasMoreElements()) {
            System.out.println(enm.nextElement());
        }
      
        // display
        System.out.println("Empty");
    }
}

Output
Empty

Explanation: The loop does not execute because the enumeration contains no elements. The program directly prints the message indicating that the enumeration is empty.

MethodDescription
Collections.emptyIterator()Returns an empty Iterator.
Collections.emptyList()Returns an immutable empty List.
Collections.emptySet()Returns an immutable empty Set.
Collections.emptyMap()Returns an immutable empty Map.

Advantages

  • Returns a safe empty Enumeration instead of null.
  • Helps avoid NullPointerException.
  • Reusable and memory efficient.
  • Useful when implementing APIs that return an Enumeration.
Comment