Skip to content
Related Articles

Related Articles

Get Enumeration over Java HashSet
  • Difficulty Level : Hard
  • Last Updated : 03 Mar, 2021

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :