How to get an enum value from a string value in Java?

前端 未结 27 2168
旧巷少年郎
旧巷少年郎 2020-11-21 10:53

Say I have an enum which is just

public enum Blah {
    A, B, C, D
}

and I would like to find the enum value of a string, for example

相关标签:
27条回答
  • 2020-11-21 11:49

    What about?

    public enum MyEnum {
        FIRST,
        SECOND,
        THIRD;
    
        public static Optional<MyEnum> fromString(String value){
            try{
                return Optional.of(MyEnum.valueOf(value));
            }catch(Exception e){
                return Optional.empty();
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 11:50

    Here's a method that can do it for any Enum, and is case insensitive.

    /** 
     * Finds the value of the given enumeration by name, case-insensitive. 
     * Throws an IllegalArgumentException if no match is found.  
     **/
    public static <T extends Enum<T>> T valueOfIgnoreCase(
            Class<T> enumeration, String name) {
    
        for (T enumValue : enumeration.getEnumConstants()) {
            if (enumValue.name().equalsIgnoreCase(name)) {
                return enumValue;
            }
        }
    
        throw new IllegalArgumentException(String.format(
            "There is no value with name '%s' in Enum %s",
            name, enumeration.getName()
        ));
    }
    
    0 讨论(0)
  • 2020-11-21 11:52

    If you don't want to write your own utility use Google's guava library:

    Enums.getIfPresent(Blah.class, "A")
    

    Unlike the built in java function it let's you check if A is present in Blah and doesn't throw an exception.

    0 讨论(0)
  • 2020-11-21 11:53

    Kotlin Solution

    Create an extension and then call valueOf<MyEnum>("value"). If the type is invalid, you'll get null and have to handle it

    inline fun <reified T : Enum<T>> valueOf(type: String): T? {
        return try {
            java.lang.Enum.valueOf(T::class.java, type)
        } catch (e: Exception) {
            null
        }
    }
    

    Alternatively, you can set a default value, calling valueOf<MyEnum>("value", MyEnum.FALLBACK), and avoiding a null response. You can extend your specific enum to have the default be automatic

    inline fun <reified T : Enum<T>> valueOf(type: String, default: T): T {
        return try {
            java.lang.Enum.valueOf(T::class.java, type)
        } catch (e: Exception) {
            default
        }
    }
    

    Or if you want both, make the second:

    inline fun <reified T : Enum<T>> valueOf(type: String, default: T): T = valueOf<T>(type) ?: default
    
    0 讨论(0)
  • 2020-11-21 11:54

    Using Blah.valueOf(string) is best but you can use Enum.valueOf(Blah.class, string) as well.

    0 讨论(0)
  • 2020-11-21 11:54

    In Java 8 the static Map pattern is even easier and is my preffered method. If you want to use the Enum with Jackson you can override toString and use that instead of name, then annotate with @JsonValue

    public enum MyEnum {
        BAR,
        BAZ;
        private static final Map<String, MyEnum> MAP = Stream.of(MyEnum.values()).collect(Collectors.toMap(Enum::name, Function.identity()));
        public static MyEnum fromName(String name){
            return MAP.get(name);
        }
    }
    
    public enum MyEnumForJson {
        BAR("bar"),
        BAZ("baz");
        private static final Map<String, MyEnumForJson> MAP = Stream.of(MyEnumForJson.values()).collect(Collectors.toMap(Object::toString, Function.identity()));
        private final String value;
    
        MyEnumForJson(String value) {
            this.value = value;
        }
    
        @JsonValue
        @Override
        public String toString() {
            return value;
        }
    
        public static MyEnumForJson fromValue(String value){
            return MAP.get(value);
        }
    }
    
    0 讨论(0)
提交回复
热议问题