我正在尝试抽象一种方法,该方法从枚举中加载带有整数、字符串值的静态哈希图。我的具体方法看起来像这样
public static Map<Integer, String> myMap = new HashMap<Integer, String>;
static{
Enumeration<MyEnum> enumTokens = MyEnum.getTokens(); //returns an enumeration of 'MyEnum'
//like to abstract the following into a method
while (enumTokens.hasMoreElements()){
MyEnum element = (MyEnum) enumTokens.nextElement();
myMap.put(element.intValue(), element.toString());
}
}