要获得对HashSet的枚举,请首先声明HashSet并添加元素-
HashSet<String> hs = new HashSet<String>();
hs.add("P");
hs.add("Q");
hs.add("R");获得枚举-
Enumeration e = Collections.enumeration(hs);
以下是通过HashSet获取枚举的示例-
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());
}
}输出结果
P Q R