0

Given a string i want to get the enum equivalent of it in constant time. I have a enum defined like the one shown in the question. Best way to create enum of strings?

public enum Strings {
STRING_ONE("ONE"),
STRING_TWO("TWO")
;

private final String text;

/**
 * @param text
 */
private Strings(final String text) {
    this.text = text;
}

/* (non-Javadoc)
 * @see java.lang.Enum#toString()
 */
@Override
public String toString() {
    return text;
}
}

If i now get a string (say "TWO"), is there a way to see if it exists and if it exists then is there a way i can define a method that will return the enum value (for example if i pass "TWO", i should be getting back "Strings.STRING_TWO"?

Community
  • 1
  • 1
maverick
  • 549
  • 2
  • 10
  • 18
  • 1
    Is naming them `ONE`, `TWO` and using `Strings.valueOf(String)` an option? It seems repetitive to have `Strings.STRING_*`. – Beau Grantham Jul 11 '15 at 19:30

3 Answers3

3

Is Enum.valueOf() not sufficient? How would you imagine being more efficient than that? There's (usually) no need to have an enum STRING_ONE("ONE") - just call enum value ONE and you get .valueOf() lookup for free.

Otherwise, just create a private static Map<String, YourEnum> and provide a similar valueOf() method that looks up against the Map.

dimo414
  • 47,227
  • 18
  • 148
  • 244
1

Since Enum.valueOf operates on the built-in name of the enum (i.e. "STRING_ONE" and "STRING_TWO") you would need to roll your own "registry" of name-to-enum, like this:

public enum Strings {
    STRING_ONE("ONE"),
    STRING_TWO("TWO")
    ;
    private static final Map<String,Strings> byName = new HashMap<String,Strings>();
    private final String text;

    private Strings(final String text) {
        this.text = text;
    }
    static {
        for (Strings s : Strings.values()) {
            byName.put(s.toString(), s);
        }
    }
    @Override
    public String toString() {
        return text;
    }
    public static Strings forName(String name) {
        return byName.get(name);
    }
}

Demo.

Above, a map from string name to enum Strings is used to do the translation. If the name is not there, null would be returned from the Strings.forName method.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

I think you need to change the code to be :

public enum Strings {
        STRING_ONE("ONE"), STRING_TWO("TWO");

        private String text;

        /**
         * @param text
         */
        private Strings(final String text) {
            this.text = text;
        }

        public String getText() {
            return this.text;
        }

        public static Strings getByTextValue(String text) {
            for (Strings str : Strings.values()) {
                if (str.getText().equals(text)) {
                    return str;
                }
            }
            return null;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.lang.Enum#toString()
         */
        @Override
        public String toString() {
            return text;
        }
    }

Example :

String test = "ONE";
Strings testEnum = Strings.getByTextValue(test);

now you have testEnum which is enum reference

Ali Helmy
  • 784
  • 6
  • 18