How to convert string result of enum with overridden toString() back to enum?

后端 未结 6 835
醉话见心
醉话见心 2021-01-31 10:30

Given the following java enum:

public enum AgeRange {

   A18TO23 {
        public String toString() {        
            return \"18 - 23\";
        }
    },
          


        
相关标签:
6条回答
  • 2021-01-31 10:51
    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.

    0 讨论(0)
  • 2021-01-31 10:56

    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

    0 讨论(0)
  • 2021-01-31 11:10

    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.

    0 讨论(0)
  • 2021-01-31 11:11

    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.

    0 讨论(0)
  • 2021-01-31 11:13

    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);
    }
    
    0 讨论(0)
  • 2021-01-31 11:14

    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);
           }
    }
    
    0 讨论(0)
提交回复
热议问题