21

Given the following java enum:

public enum AgeRange {

   A18TO23 {
        public String toString() {        
            return "18 - 23";
        }
    },
   A24TO29 {
        public String toString() {        
            return "24 - 29";
        }
    },
   A30TO35 {
        public String toString() {        
            return "30 - 35";
        }
    },

}

Is there any way to convert a string value of "18 - 23" to the corresponding enum value i.e. AgeRange.A18TO23 ?

Thanks!

4

6 回答 6

30

The best and simplest way to do it is like this:

public enum AgeRange {
    A18TO23 ("18-23"),
    A24TO29 ("24-29"),
    A30TO35("30-35");

    private String value;

    AgeRange(String value){
        this.value = value;
    }

    public String toString(){
        return value;
    }

    public static AgeRange getByValue(String value){
        for (final AgeRange element : EnumSet.allOf(AgeRange.class)) {
            if (element.toString().equals(value)) {
                return element;
            }
        }
        return null;
    }
}

Then you just need to invoke the getByValue() method with the String input in it.

于 2008-10-27T14:56:20.467 回答
7

You could always create a map from string to value - do so statically so you only need to map it once, assuming that the returned string remains the same over time. There's nothing built-in as far as I'm aware.

于 2008-10-27T14:48:14.947 回答
4

According to effective java (2nd ed) item 30, it can be (it is much faster than the loop)

public enum AgeRange {
       A18TO23("18-23"),
       A24TO29("24-29"),
       A30TO35("30-35");

       private final String value;

       AgeRange(String value){
          this.value = value;
       }

       @Override public String toString(){
           return value;
       }

       private static final Map<String, AgeRange> stringToEnum =
           new HashMap<String, AgeRange>();

       static {
           for (AgeRange r : values()) {
               stringToEnum.put(r.toString(), r);
           }
       }

       public static AgeRange getByValue(String value){
           return stringToEnum.get(value);
       }
}
于 2012-03-20T05:46:54.303 回答
2
for (AgeRange ar: EnumSet.allOf(AgeRange)) {
    if (ar.toString().equals(inString)) {
         myAnswer = ar;
         break;
    }
}

Or something like that? Just typed in, haven't run through a compiler. Forgive (comment on) typos...

Or use logic like this to build a map once. Avoid iteration at runtime. Good idea, Jon.

于 2008-10-27T14:50:44.350 回答
2

The class overrides "toString()" - so, to get the reverse operation, you need to override valueOf() to translate the output of toString() back to the Enum values.

public enum AgeRange {

   A18TO23 {
        public String toString() {        
                return "18 - 23";
        }
        public AgeRange valueOf (Class enumClass, String name) {
                return A18T023
        }
    },

    .
    .
    .
}

Buyer beware - uncompiled and untested...

The mechanism for toString() and valueOf() is a documented part of the API

于 2008-10-27T14:54:29.550 回答
0

You could try something like the following?

static AgeRange fromString(String range) {
    for (AgeRange ageRange : values()) {
        if (range.equals(ageRange.toString())) {
            return ageRange;
        }
    }
    return null;   
}

Or, as others suggested, using a caching approach:

private static Map<String, AgeRange> map;

private static synchronized void registerAgeRange(AgeRange ageRange) {
    if (map == null) {
        map = new HashMap<String, AgeRange>();
    }
    map.put(ageRange.toString(), ageRange);
}

AgeRange() {
    registerAgeRange(this);
}

static AgeRange fromString(String range) {
    return map.get(range);
}
于 2008-10-27T15:06:31.510 回答