通过 Java 获取 HashSet 上的枚举


要通过 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

更新于: 30-7-2019

213 次浏览

开启你的 事业

通过完成课程获得认证

开始
广告