This java example shows how to get Enumeration over HashSet using enumeration method of Collections class.
import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; public class EnumerationOverHashSet { public static void main(String[] args) { /** Create a HashSet object **/ HashSet< string > hashSet = new HashSet< string >(); /** Add elements to HashSet **/ hashSet.add("A"); hashSet.add("B"); hashSet.add("D"); hashSet.add("E"); hashSet.add("F"); /** * Enumeration enumeration(Collection c) - Returns an enumeration over * the specified collection. This provides interoperatbility with legacy * APIs that require an enumeration as input. **/ Enumeration e = Collections.enumeration(hashSet); System.out.println("The set contains..."); while (e.hasMoreElements()) System.out.println(e.nextElement()); } }
The output is:
The set contains...
D
E
F
A
B
No comments:
Post a Comment