Java 将 Enum 加入键值对的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2401134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easiest way to get Enum in to Key Value pair
提问by vijay.shad
I have defined my Enums like this.
我已经像这样定义了我的枚举。
public enum UserType {
RESELLER("Reseller"),
SERVICE_MANAGER("Manager"),
HOST("Host");
private String name;
private UserType(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
What should be the easiest way to get a key-value pair form the enum values?
从枚举值中获取键值对的最简单方法应该是什么?
The output map i want to create should be like this
我要创建的输出地图应该是这样的
key = Enum(example:- HOST)
value = Host
The map I want do define is
我想要定义的地图是
Map<String, String> constansts = new HashMap<String, String>();
Ans: What I Did
答:我做了什么
I have created a Generic Method to access any enum and change values from that to a Map. I got this IDEA, form a code fragment found at here in any other thread.
我创建了一个通用方法来访问任何枚举并将值从该枚举更改为映射。我得到了这个想法,形成了一个在任何其他线程中找到的代码片段。
public static <T extends Enum<T>> Map<String, String> getConstantMap(
Class<T> klass) {
Map<String, String> vals = new HashMap<String, String>(0);
try {
Method m = klass.getMethod("values", null);
Object obj = m.invoke(null, null);
for (Enum<T> enumval : (Enum<T>[]) obj) {
vals.put(enumval.name(), enumval.toString());
}
} catch (Exception ex) {
// shouldn't happen...
}
return vals;
}
Now, After this all I have to do is call this method with all the enum constructs and i am Done.
现在,在此之后,我所要做的就是使用所有枚举构造调用此方法,我就完成了。
One More thing
还有一件事
To get This done i have to orverride the toString Method like this
要完成此操作,我必须像这样重写 toString 方法
public String toString() {
return name;
}
Thanks.
谢谢。
采纳答案by Péter T?r?k
Provided you need to map from the textual values to the enum instances:
假设您需要从文本值映射到枚举实例:
Map<String, UserType> map = new HashMap<String, UserType>();
map.put(RESELLER.getName(), RESELLER);
map.put(SERVICE_MANAGER.getName(), SERVICE_MANAGER);
map.put(HOST.getName(), HOST);
or a more generic approach:
或更通用的方法:
for (UserType userType : UserType.values()) {
map.put(userType.getName(), userType);
}
回答by extraneon
You could use the values() method on the enum, giving you all possible combinations, and put that in a map using the iterator.
您可以在枚举上使用 values() 方法,为您提供所有可能的组合,并使用迭代器将其放入映射中。
Map<String, UserType> map = new HashMap<String, UserType>();
for (UserType userType : UserType.values()) {
map.put(userType.name(), userType);
}
回答by Lior Bornshtain
I think that the eaiest method for using an Enum as a key in a map is using an EnumMap http://download.oracle.com/javase/1,5.0/docs/api/java/util/EnumMap.html
我认为在地图中使用 Enum 作为键的最简单方法是使用 EnumMap http://download.oracle.com/javase/1,5.0/docs/api/java/util/EnumMap.html
回答by Rinor
Java 8 way:
Java 8 方式:
Arrays.stream(UserType.values())
.collect(Collectors.toMap(UserType::getName, Function.identity()))
Understandably store it in a variable :)
可以理解地将它存储在一个变量中:)