如果找不到枚举常量,则返回空字符串。

huangapple go评论57阅读模式
英文:

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(&quot;1&quot;),
    TWO(&quot;2&quot;),
    THREE(&quot;3&quot;);

    private final String str;

    TestType(String str) {
        this.str = str;
    }

    private static final Map&lt;String, TestType&gt; 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 ? &quot;&quot; : 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 &quot;&quot;;
		}
	}
}

And just use ValueType.lookupValueType(dynamicValueString) (I'm assuming dynamicValueString is meant to match enum value names)

huangapple
  • 本文由 发表于 2020年8月24日 13:49:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63555366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定