Saturday, September 6, 2025
HomeLanguagesJavaGet Enumeration over Java HashSet

Get Enumeration over Java HashSet

The HashSet class implements the Set interface, backed by a hash table which is a HashMap instance. There is no assurance as to the iteration order of the set, which implies that over time, the class does not guarantee the constant order of elements. The null element is allowed by this class. The java.util.Collections class enumeration method is used to return an enumeration of the specified collection. 

To return enumeration over HashSet:

Syntax:

public static Enumeration enumeration(Collection c)

Method Used: hasMoreElements() Method.

An object that implements the Enumeration interface creates one at a time, a set of objects. hasMoreElements() method of enumeration used to tests if this enumeration contains more elements. If enumeration contains more elements, then it will return true else false.

Syntax:

boolean hasMoreElements()

Return value: This method returns true if there is at least one additional element to be given in this enumeration object, otherwise return false.

Below is the full implementation of the above approach:

Java




// Getting Enumeration over Java HashSet
import java.util.*;
import java.util.Enumeration;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of HashSet
        // String type here- name
        HashSet<String> name = new HashSet<>();
  
        // Adding element to HashSet
        // Custom inputs
        name.add("Nikhil");
        name.add("Akshay");
        name.add("Bina");
        name.add("Chintu");
        name.add("Dhruv");
  
        // Creating object of type Enumeration<String>
        Enumeration e = Collections.enumeration(name);
  
        // Condition check using hasMoreElements() method
        while (e.hasMoreElements())
  
            // print the enumeration
            System.out.println(e.nextElement());
    }
}


Output

Dhruv
Akshay
Chintu
Bina
Nikhil

Example 2: 

Java




// Getting Enumeration over Java HashSet
import java.util.*;
import java.util.Enumeration;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of HashSet
        // String type here- name
        HashSet<String> gfg = new HashSet<>();
  
        // Adding element to HashSet
        // Custom inputs
        gfg.add("Welcome");
        gfg.add("On");
        gfg.add("GFG");
  
        // Creating object of type Enumeration<String>
        Enumeration e = Collections.enumeration(gfg);
  
        // Condition check using hasMoreElements() method
        while (e.hasMoreElements())
  
            // print the enumeration
            System.out.println(e.nextElement());
    }
}


Output

GFG
Welcome
On
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS