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

ID : 372

viewed : 115

Tags : javaenumsguavajava

Top 5 Answer for How to get an enum value from a string value in Java

vote vote


Yes, Blah.valueOf("A") will give you Blah.A.

Note that the name must be an exact match, including case: Blah.valueOf("a") and Blah.valueOf("A ") both throw an IllegalArgumentException.

The static methods valueOf() and values() are created at compile time and do not appear in source code. They do appear in Javadoc, though; for example, Dialog.ModalityType shows both methods.

vote vote


Another solution if the text is not the same as the enumeration value:

public enum Blah {     A("text1"),     B("text2"),     C("text3"),     D("text4");      private String text;      Blah(String text) {         this.text = text;     }      public String getText() {         return this.text;     }      public static Blah fromString(String text) {         for (Blah b : Blah.values()) {             if (b.text.equalsIgnoreCase(text)) {                 return b;             }         }         return null;     } } 
vote vote


Use the pattern from Joshua Bloch, Effective Java:

(simplified for brevity)

enum MyEnum {     ENUM_1("A"),     ENUM_2("B");      private String name;      private static final Map<String,MyEnum> ENUM_MAP;      MyEnum (String name) { = name;     }      public String getName() {         return;     }      // Build an immutable map of String name to enum pairs.     // Any Map impl can be used.      static {         Map<String,MyEnum> map = new ConcurrentHashMap<String, MyEnum>();         for (MyEnum instance : MyEnum.values()) {             map.put(instance.getName().toLowerCase(),instance);         }         ENUM_MAP = Collections.unmodifiableMap(map);     }      public static MyEnum get (String name) {         return ENUM_MAP.get(name.toLowerCase());     } } 

Also see:

Oracle Java Example using Enum and Map of instances

Execution order of of static blocks in an Enum type

How can I lookup a Java enum from its String value

vote vote


Here's a nifty utility I use:

/**  * A common method for all enums since they can't have another base class  * @param <T> Enum type  * @param c enum type. All enums must be all caps.  * @param string case insensitive  * @return corresponding enum, or null  */ public static <T extends Enum<T>> T getEnumFromString(Class<T> c, String string) {     if( c != null && string != null ) {         try {             return Enum.valueOf(c, string.trim().toUpperCase());         } catch(IllegalArgumentException ex) {         }     }     return null; } 

Then in my enum class I usually have this to save some typing:

public static MyEnum fromString(String name) {     return getEnumFromString(MyEnum.class, name); } 

If your enums are not all caps, just change the Enum.valueOf line.

It is too bad I can't use T.class for Enum.valueOf as T is erased.

vote vote


You should also be careful with your case. Let me explain: doing Blah.valueOf("A") works, but Blah.valueOf("a") will not work. Then again Blah.valueOf("a".toUpperCase(Locale.ENGLISH)) would work.

On Android you should use Locale.US, as sulai points out.

Top 3 video Explaining How to get an enum value from a string value in Java