在 Java 中获取 HashSet 上的枚举

java 8object oriented programmingprogramming

要获取 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

相关文章