英文:
Returning an empty string if the enum constant is not found
问题
假设我有一个枚举类:
public enum ValueType {
time("15"),
post("15"),
comment("F"),
private final String valueType;
ValueType(final String valueType) {
this.valueType = valueType;
};
public String getValueType() {
return this.valueType;
}
}
我使用以下方式获取常量值:
ValueType.valueOf(dynamicValueString).getValueType()
假设没有针对dynamicValueString
的枚举常量。
如果枚举未找到,我该如何将值设置为空字符串,而不使用 try-catch 呢?
英文:
Suppose I have an enum class of
public enum ValueType {
time("15"),
post("15"),
comment("F"),
private final String valueType;
ValueType(final String valueType) {
this.valueType = valueType;
};
public String getValueType() {
return this.valueType;
}
}
and Im getting the constant value using
ValueType.valueOf(dynamicValueString).getValueType()
Supposed there is no enum constant for that dynamicValueString.
How can I set the value to an empty string if the enum is not found? without using try catch?
答案1
得分: 2
public enum ValueType {
time("15"),
post("15"),
comment("F");
private final String valueType;
ValueType(final String valueType) {
this.valueType = valueType;
}
public String getValueType() {
return this.valueType;
}
public static String getValueType(String name) {
return Arrays.stream(ValueType.values())
.filter(v -> name.equalsIgnoreCase(v.valueType))
.findFirst()
.map(v -> v.valueType)
.orElse("");
}
}
英文:
public enum ValueType {
time("15"),
post("15"),
comment("F");
private final String valueType;
ValueType(final String valueType) {
this.valueType = valueType;
}
public String getValueType() {
return this.valueType;
}
public static String getValueType(String name) {
return Arrays.stream(ValueType.values())
.filter(v -> name.equalsIgnoreCase(v.valueType))
.findFirst()
.map(v -> v.valueType)
.orElse("");
}
}
答案2
得分: 2
public enum TestType {
ONE("1"),
TWO("2"),
THREE("3");
private final String str;
TestType(String str) {
this.str = str;
}
private static final Map<String, TestType> MAP = Arrays.stream(values())
.collect(Collectors.toMap(TestType::name, Function.identity()));
public static String typeByName(String name) {
TestType type = MAP.get(name);
return type == null ? "" : type.str;
}
}
英文:
I'd argue that you are right to avoid exceptions for normal program flow. Anyway, I use a map as suggested in comment:
public enum TestType {
ONE("1"),
TWO("2"),
THREE("3");
private final String str;
TestType(String str) {
this.str = str;
}
private static final Map<String, TestType> MAP = Arrays.stream(values())
.collect(Collectors.toMap(TestType::name, Function.identity()));
public static String typeByName(String name) {
TestType type = MAP.get(name);
return type == null ? "" : type.str;
}
}
答案3
得分: 0
你需要在枚举上实现自定义查找:
public enum ValueType {
//...
public static String lookupValueType(String constant) {
try {
return ValueType.valueOf(constant).valueType;
} catch (IllegalArgumentException e) {
return "";
}
}
}
然后只需使用 ValueType.lookupValueType(dynamicValueString)
(我假设 dynamicValueString
应该与枚举值名称匹配)。
英文:
You need to implement a custom lookup on your enum:
public enum ValueType {
//...
public static String lookupValueType(String constant) {
try {
return ValueType.valueOf(constant).valueType;
} catch (IllegalArgumentException e) {
return "";
}
}
}
And just use ValueType.lookupValueType(dynamicValueString)
(I'm assuming dynamicValueString
is meant to match enum value names)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论