Collections enumeration() method in Java With Examples

The method enumeration() is part of the Collections utility class in Java that provides a mechanism to generate a legacy Enumeration object from a collection and this method is particularly useful when dealing with legacy APIs or classes that require an Enumeration instead of more recent iterators.

Method Signature

public static<T> Enumeration<T> enumeration(Collection<T> c) 

 <T>: It represents a generic type parameter. It extends the use of the method to those collections that have elements of any type.

Enumeration<T>: It declares the return type of the method. It signifies that the method returns an instance of the Enumeration class, which is a legacy interface in Java for iterating over a sequence of elements.

Collection<T> c: The parameter is responsible for the instruction which will be used to create an enumeration from a particular collection. It authorizes any object that implements the Collection interface.

Various examples for Collections.enumeration() method

1. Converting ArrayList to Enumeration

Converting an ArrayList to an Enumeration in Java involves using the Collections.enumeration() method and this process is particularly useful when you need to adapt a modern ArrayList to legacy APIs or classes that expect an Enumeration for iteration.

Algorithm

Step 1: Create an empty ArrayList named arrayList to store elements of type String and add elements to the ArrayList using the add() method. For example, add elements like "Apple", "Banana", and "Orange" to the ArrayList.

Step 2: Use the Collections.enumeration() method to convert the ArrayList to an Enumeration.

Step 3: Pass the ArrayList as an argument to the Collections.enumeration() method. Store the returned Enumeration object in a variable named enumeration.

Step 4: Iterate through Enumeration: Initialize a loop to iterate over the elements of the ArrayList using the Enumeration.

Step 5: Use the hasMoreElements() method of the Enumeration to check if there are more elements to iterate over. If there are more elements, retrieve the next element using the nextElement() method.

Step 6: Inside the loop: Retrieve the next element from the Enumeration. Store the retrieved element in a variable named element.

Step 7: Inside the loop, print each element of the ArrayList to the console. Print the value of the element variable.

Filename: ArrayListToEnumerationExample.java

import java.util.ArrayList;

import java.util.Collections;

import java.util.Enumeration;

public class ArrayListToEnumerationExample {

    public static void main(String[] args) {

        // Step 1: Create an ArrayList

        ArrayList<String> arrayList = new ArrayList<>();

        // Step 2: Add elements to the ArrayList

        arrayList.add("Apple");

        arrayList.add("Banana");

        arrayList.add("Orange");

        // Step 3: Convert ArrayList to Enumeration

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

        // Step 4: Iterate through elements using Enumeration

        System.out.println("Elements in the ArrayList:");

        while (enumeration.hasMoreElements()) {

            String element = enumeration.nextElement();

            System.out.println(element);

        }

    }

}

Output:

Elements in the ArrayList:

Apple

Banana

Orange

2. Converting a HashSet to an Enumeration

Converting a HashSet to an Enumeration in Java involves adapting a modern collection to a legacy interface for iteration and this process is useful when you need to interface with older APIs or classes that expect an Enumeration instead of an iterator.

Algorithm

Step 1: Create an empty HashSet named hashSet to store elements of type String and add elements ("Apple", "Banana", "Orange") to the HashSet using the add() method.

Step 2: Use the Collections.enumeration() method to convert the HashSet to an Enumeration.

Step 3: Pass the HashSet as an argument to the Collections.enumeration() method. Store the returned Enumeration object in a variable named enumeration.

Step 4: Initialize a loop to iterate over the elements of the HashSet using the Enumeration. Use the hasMoreElements() method of the Enumeration to check if there are more elements to iterate over.

Step 5: If there are more elements, retrieve the next element using the nextElement() method.

Step 6: Inside the loop: Retrieve the next element from the Enumeration. Store the retrieved element in a variable named element. Print the value of the element variable.

Filename: HashSetToEnumerationExample.java

import java.util.Collections;

import java.util.Enumeration;

import java.util.HashSet;

public class HashSetToEnumerationExample {

    public static void main(String[] args) {

        // Step 1: Create a HashSet

        HashSet<String> hashSet = new HashSet<>();

        // Step 2: Add elements to the HashSet

        hashSet.add("Apple");

        hashSet.add("Banana");

        hashSet.add("Orange");

        // Step 3: Convert HashSet to Enumeration

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

        // Step 4: Iterate through elements using Enumeration

        System.out.println("Elements in the HashSet:");

        while (enumeration.hasMoreElements()) {

            String element = enumeration.nextElement();

            System.out.println(element);

        }

    }

}

Output:

Elements in the HashSet:

Apple

Orange

Banana

3. Using Enumeration in legacy code

Using Enumeration in legacy code is a common practice in Java, especially when dealing with older APIs or classes that were introduced before the collection’s framework was enhanced with iterators. The Enumeration interface provides a way to iterate over elements in a collection sequentially.

Algorithm

Step 1: Create a legacy Hashtable named hashtable to store key-value pairs and add key-value pairs to the Hashtable using the put() method.

Step 2: Obtain Enumeration from Hashtable: Use the keys() method of the Hashtable to obtain an Enumeration of its keys. Store the returned Enumeration object in a variable named enumeration.

Step 3: Iterate through Enumeration: Initialize a loop to iterate over the keys of the Hashtable using the Enumeration.

Step 4: Use the hasMoreElements() method of the Enumeration to check if there are more keys to iterate over. If there are more keys, retrieve the next key using the nextElement() method.

Step 5: Inside the loop: Retrieve the next key from the Enumeration. Store the retrieved key in a variable named key.

Step 6: Use the retrieved key to get the corresponding value from the Hashtable. Retrieve the value associated with the key using the get() method of the Hashtable. Inside the loop, print each key-value pair to the console.

Filename: EnumerationLegacyExample.java

import java.util.Enumeration;

import java.util.Hashtable;

public class EnumerationLegacyExample {

    public static void main(String[] args) {

        // Step 1: Create a legacy Hashtable

        Hashtable<String, Integer> hashtable = new Hashtable<>();

        hashtable.put("One", 1);

        hashtable.put("Two", 2);

        hashtable.put("Three", 3);

        // Step 2: Obtain Enumeration from Hashtable

        Enumeration<String> enumeration = hashtable.keys();

        // Step 3: Iterate through elements using Enumeration

        while (enumeration.hasMoreElements()) {

            String key = enumeration.nextElement();

            System.out.println("Key: " + key + ", Value: " + hashtable.get(key));

        }

    }

}

Output:

Key: One, Value: 1

Key: Three, Value: 3

Key: Two, Value: 2

4. Using Enumeration in Legacy Code with Vector

Using Enumeration with the Vector class is a common practice in Java, especially when working with legacy code or APIs. Vector is a legacy class that was part of the original Java Collections Framework and provides methods to enumerate its elements. The Enumeration interface, which is also part of the legacy Java Collections Framework, allows sequential access to the elements of a collection.

Algorithm

Step 1: Create a legacy Vector named vector to store elements of type String and add elements ("Red", "Green", "Blue") to the Vector using the add() method.

Step 2: Obtain Enumeration from Vector: Use the elements() method of the Vector to obtain an Enumeration of its elements. Store the returned Enumeration object in a variable named enumeration.

Step 3: Iterate through Enumeration: Initialize a loop to iterate over the elements of the Vector using the Enumeration.

Step 4: Use the hasMoreElements() method of the Enumeration to check if there are more elements to iterate over. If there are more elements, retrieve the next element using the nextElement() method.

Step 5: Inside the loop: Retrieve the next element from the Enumeration. Store the retrieved element in a variable named element.

Step 6: Inside the loop, print each element of the Vector to the console. Print the value of the element variable.

Filename: EnumerationLegacyWithVector.java

import java.util.Enumeration;

import java.util.Vector;

public class EnumerationLegacyWithVector {

    public static void main(String[] args) {

        // Step 1: Create a legacy Vector

        Vector<String> vector = new Vector<>();

        vector.add("Red");

        vector.add("Green");

        vector.add("Blue");

        // Step 2: Obtain Enumeration from Vector

        Enumeration<String> enumeration = vector.elements();

        // Step 3: Iterate through elements using Enumeration

        System.out.println("Elements in the Vector:");

        while (enumeration.hasMoreElements()) {

            String element = enumeration.nextElement();

            System.out.println(element);

        }

    }

}

Output:

Elements in the Vector:

Red

Green

Blue

5. Using Enumeration with Legacy Stack

Java's Stack class is a legacy data structure that extends the Vector class to represent a last-in, first-out (LIFO) stack of objects. While Stack implements the List interface, it's typically used as a stack. The Enumeration interface provides a way to iterate over elements of a Stack in legacy Java code.

Algorithm

Step 1: Create a legacy Stack named stack to store elements of type String and push elements ("One", "Two", "Three") onto the Stack using the push() method.

Step 2: Use the elements() method of the Stack to obtain an Enumeration of its elements. Store the returned Enumeration object in a variable named enumeration.

Step 3: Initialize a loop to iterate over the elements of the Stack using the Enumeration. Use the hasMoreElements() method of the Enumeration to check if there are more elements to iterate over.

Step 4: If there are more elements, retrieve the next element using the nextElement() method. Inside the loop: Retrieve the next element from the Enumeration. Store the retrieved element in a variable named element.

Step 5: Inside the loop, print each element of the Stack to the console. Print the value of the element variable.

Filename: EnumerationWithStackExample.java

import java.util.Enumeration;

import java.util.Stack;

public class EnumerationWithStackExample {

    public static void main(String[] args) {

        // Step 1: Create a legacy Stack

        Stack<String> stack = new Stack<>();

        stack.push("One");

        stack.push("Two");

        stack.push("Three");

        // Step 2: Obtain Enumeration from Stack

        Enumeration<String> enumeration = stack.elements();

        // Step 3: Iterate through elements using Enumeration

        System.out.println("Elements in Stack:");

        while (enumeration.hasMoreElements()) {

            String element = enumeration.nextElement();

            System.out.println(element);

        }

    }

}

Output:

Elements in Stack:

One

Two

Three