Get Enumeration over HashSet in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 16-Jan-2019 07:24:46
Advertisements