The Java Programming Language

Page Contents

Convert String To Enum

Don't have to define your own map, just use The enum class' valueOf() function.

The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. [Ref].

public enum Speeds {
   SONIC_SPEED, LIGHT_SPEED, RIDICULOUS_SPEED, LUDICROUS_SPEED
}

public class Test {
   public static void main(String[] args) {
      System.out.println(Speeds.valueof("LUDICOUS_SPEED"));
   }
}