Java 如何将覆盖 toString() 的枚举的字符串结果转换回枚举?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/240122/
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
How to convert string result of enum with overridden toString() back to enum?
提问by Kevin
Given the following java enum:
给定以下 java 枚举:
public enum AgeRange {
A18TO23 {
public String toString() {
return "18 - 23";
}
},
A24TO29 {
public String toString() {
return "24 - 29";
}
},
A30TO35 {
public String toString() {
return "30 - 35";
}
},
}
Is there any way to convert a string value of "18 - 23" to the corresponding enum value i.e. AgeRange.A18TO23 ?
有没有办法将“18 - 23”的字符串值转换为相应的枚举值,即 AgeRange.A18TO23 ?
Thanks!
谢谢!
采纳答案by sakana
The best and simplest way to do it is like this:
最好和最简单的方法是这样的:
public enum AgeRange {
A18TO23 ("18-23"),
A24TO29 ("24-29"),
A30TO35("30-35");
private String value;
AgeRange(String value){
this.value = value;
}
public String toString(){
return value;
}
public static AgeRange getByValue(String value){
for (final AgeRange element : EnumSet.allOf(AgeRange.class)) {
if (element.toString().equals(value)) {
return element;
}
}
return null;
}
}
Then you just need to invoke the getByValue()
method with the String
input in it.
然后你只需要调用getByValue()
包含String
输入的方法。
回答by Jon Skeet
You could always create a map from string to value - do so statically so you only need to map it once, assuming that the returned string remains the same over time. There's nothing built-in as far as I'm aware.
你总是可以创建一个从字符串到值的映射——静态地这样做,所以你只需要映射一次,假设返回的字符串随着时间的推移保持不变。据我所知,没有任何内置的东西。
回答by John M
for (AgeRange ar: EnumSet.allOf(AgeRange)) {
if (ar.toString().equals(inString)) {
myAnswer = ar;
break;
}
}
Or something like that? Just typed in, haven't run through a compiler. Forgive (comment on) typos...
或类似的东西?刚刚输入,还没有通过编译器运行。原谅(评论)错别字...
Or use logic like this to build a map once. Avoid iteration at runtime. Good idea, Jon.
或者使用这样的逻辑来构建一次地图。避免在运行时迭代。好主意,乔恩。
回答by Ken Gentle
The class overrides "toString()" - so, to get the reverse operation, you need to override valueOf()
to translate the output of toString()
back to the Enum values.
该类覆盖“toString()” - 因此,要获得相反的操作,您需要覆盖valueOf()
以将输出转换toString()
回 Enum 值。
public enum AgeRange {
A18TO23 {
public String toString() {
return "18 - 23";
}
public AgeRange valueOf (Class enumClass, String name) {
return A18T023
}
},
.
.
.
}
Buyer beware - uncompiled and untested...
买家当心 - 未经编译和未经测试...
The mechanism for toString()
and valueOf()
is a documented part of the API
对于机制toString()
和valueOf()
是的记录部分API
回答by toolkit
You could try something like the following?
您可以尝试以下操作吗?
static AgeRange fromString(String range) {
for (AgeRange ageRange : values()) {
if (range.equals(ageRange.toString())) {
return ageRange;
}
}
return null;
}
Or, as others suggested, using a caching approach:
或者,正如其他人所建议的,使用缓存方法:
private static Map<String, AgeRange> map;
private static synchronized void registerAgeRange(AgeRange ageRange) {
if (map == null) {
map = new HashMap<String, AgeRange>();
}
map.put(ageRange.toString(), ageRange);
}
AgeRange() {
registerAgeRange(this);
}
static AgeRange fromString(String range) {
return map.get(range);
}
回答by wsu_cic
According to effective java (2nd ed) item 30, it can be (it is much faster than the loop)
根据 Effective java (2nd ed) item 30,它可以是(它比循环快得多)
public enum AgeRange {
A18TO23("18-23"),
A24TO29("24-29"),
A30TO35("30-35");
private final String value;
AgeRange(String value){
this.value = value;
}
@Override public String toString(){
return value;
}
private static final Map<String, AgeRange> stringToEnum =
new HashMap<String, AgeRange>();
static {
for (AgeRange r : values()) {
stringToEnum.put(r.toString(), r);
}
}
public static AgeRange getByValue(String value){
return stringToEnum.get(value);
}
}