This article is participating in the Java Topic Month – Java Debug Notes event. See the event link for details

Question: How do I get enumeration values from string values in Java?

Say I have an enumeration

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

And I want to find an enumeration value of A string, such as “A”, which would be Blah.A. How is that possible?

Is Enum. ValueOf () the method I need? If so, how will I use it?

High score answer:


A lot of knowledge, really need to write out will master ! ! !   \color{purple} {~}

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 illegalArgumentExceptions.

Static methods valueOf() and values() are created at compile time and do not appear in the source code. However, they do appear in Javadoc. For example, dialog.modalityType displays two methods.

High score answer:

Using Joshua Bloch’s schema from Valid 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) { this.name = name; } public String getName() { return this.name; } // 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()); }Copy the code

}

Also see:

Example Enumeration and mapping of Oracle Java examples

Enum Execution sequence of static blocks

How do I find a Java enumeration from its String value

The article translated from am2dgbqfb6mk75jcyanzabc67y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 6…

The authors suggest that static blocks should be initialized and put into the map. Enumerations can be nested, which can be used to implement more extensible content

Give a simple example of an interface enumeration

Public enum SaasApiEnum {/** * Enterprise creation */ ORG_CREATE(" enterprise creation ", saasapi.org_create),; SaasApiEnum(String name, String api) { this.name = name; this.api = api; } /** * Interface name */ Private final String name; /** * private final String API; / / private static final Map<String, SaasApiEnum> MAPPINGS = new HashMap<>(64); / / Private static final Map<String, SaasApiEnum> MAPPINGS = new HashMap<>(64); / / Private static final Map<String, SaasApiEnum> MAPPINGS = new HashMap<>(64); static { for (SaasApiEnum anEnum : values()) { MAPPINGS.put(anEnum.api, anEnum); }} /** * parse the given interface address to the current enumeration type ** @param API address * @return the corresponding enumeration, or null if not found */ public static SaasApiEnum resolve(String api) { return (api ! = null ? MAPPINGS.get(api) : null); } public String getName() { return name; } public String getApi() { return api; }}Copy the code

Thank you very much for reading here, if this article is good, feel something

Please like 👍 follow ❤️ share 👥 is really very useful for me with 8 pack abs!!

If there are any mistakes in this blog, please comment and comment. Thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️