Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I lookup a Java enum from its String value?

Tags:

java

enums

lookup

I would like to lookup an enum from its string value (or possibly any other value). I've tried the following code but it doesn't allow static in initialisers. Is there a simple way?

public enum Verbosity {      BRIEF, NORMAL, FULL;      private static Map<String, Verbosity> stringMap = new HashMap<String, Verbosity>();      private Verbosity() {         stringMap.put(this.toString(), this);     }      public static Verbosity getVerbosity(String key) {         return stringMap.get(key);     } }; 
like image 228
peter.murray.rust Avatar asked Jul 03 '09 21:07

peter.murray.rust


People also ask

How do you check if an enum contains a given string?

toList()). contains(aString); EventNames is the name of the enum while getEvent() is what returns the associated string value of each enum member.

Can we get an enum by value?

Get the value of an EnumTo get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.

Can an enum have a string value?

Since C# doesn't support enum with string value, in this blog post, we'll look at alternatives and examples that you can use in code to make your life easier. The most popular string enum alternatives are: Use a public static readonly string. Custom Enumeration Class.


2 Answers

You're close. For arbitrary values, try something like the following:

public enum Day {       MONDAY("M"), TUESDAY("T"), WEDNESDAY("W"),     THURSDAY("R"), FRIDAY("F"), SATURDAY("Sa"), SUNDAY("Su"), ;      private final String abbreviation;      // Reverse-lookup map for getting a day from an abbreviation     private static final Map<String, Day> lookup = new HashMap<String, Day>();      static {         for (Day d : Day.values()) {             lookup.put(d.getAbbreviation(), d);         }     }      private Day(String abbreviation) {         this.abbreviation = abbreviation;     }      public String getAbbreviation() {         return abbreviation;     }      public static Day get(String abbreviation) {         return lookup.get(abbreviation);     } } 
like image 30
Lyle Avatar answered Oct 05 '22 23:10

Lyle


Use the valueOf method which is automatically created for each Enum.

Verbosity.valueOf("BRIEF") == Verbosity.BRIEF 

For arbitrary values start with:

public static Verbosity findByAbbr(String abbr){     for(Verbosity v : values()){         if( v.abbr().equals(abbr)){             return v;         }     }     return null; } 

Only move on later to Map implementation if your profiler tells you to.

I know it's iterating over all the values, but with only 3 enum values it's hardly worth any other effort, in fact unless you have a lot of values I wouldn't bother with a Map it'll be fast enough.

like image 61
Gareth Davis Avatar answered Oct 05 '22 23:10

Gareth Davis