Lookup enum by string value [java]


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

public enum Blah {

  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;


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 "A" which would be Blah.A. How would it be possible to do this?

Is the Enum.valueOf() the method I need? If so, how would I use this?

Convert from String to a Java enum with large amount of values

You want a Map<String, Code>, but how to populate it neatly? Enums don't allow you to initialize a static fields before the enum instances are initialized, but there's a neat little trick, called the Initialization-on-demand holder idiom, that makes using a statically initialized map needed for this functionality easy to implement:

public enum Code {
    // etc

    private static class Holder {
        static Map<String, Code> CODE_MAP = new HashMap<>();

    private final String code;

    private Code(String code) {
        this.code = code;
        Holder.CODE_MAP.put(code, this);

    public String getCode() {
        return this.code;

    public Code convertFromString(String code) {
        return CODE_MAP.get(code);

This works because the class loader initializes inner static classes before initializing the enum class, so the map is assigned ready to load during enum instance initialization.

No loops. No special code to load the map (done in constructor). Minimal code.

Your provided solution is proper implementation.

As you will have to expose only one method and it is more readable.

And it is always good to use Map instead of iterating it manually.

And also as you mentioned the complexity is O(1).

+1 to your question, as it gives a cleaner approach to use enum in some usecases.

Do you mean like ...

MyEnum e = MyEnum.valueOf(text);


MyEnum e = Enum.valueOf(MyEnum.class, text);

Java convert a string to list of enums

Your question doesn't really make much sense. If you want to send or store list of enums then you can simply serialize and deserialize it since ArrayList and each enum are Serializable.


List<Day> ofiginalList = new ArrayList<Day>();

ByteArrayOutputStream bout = new ByteArrayOutputStream();

ObjectOutput out = new ObjectOutputStream(bout);

ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInput in = new ObjectInputStream(bin);

List<Day> deserializedList = (List<Day>) in.readObject();

Output: [Monday, Tuesday].

You can try doing something like

List<Day> days = new ArrayList<>();
StringTokenizer tokenizer = new StringTokenizer("[Monday, Tuesday]", "[], ");
while (st.hasMoreTokens()) {
  String token = st.nextToken();
  days.add(Day.valueOf(Day.class, token));