Get Enumeration over HashSet in Java


To get enumeration over HashSet, first declare the HashSet and add elements −

HashSet<String> hs = new HashSet<String>();
hs.add("P");
hs.add("Q");
hs.add("R");

To get enumeration −

Enumeration e = Collections.enumeration(hs);

The following is an example to get Enumeration over HashSet −

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      HashSet<String> hs = new HashSet<String>();
      hs.add("P");
      hs.add("Q");
      hs.add("R");
      Enumeration e = Collections.enumeration(hs);
      while (e.hasMoreElements())
      System.out.println(e.nextElement());
   }
}

Output

P
Q
R

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements