Return enum value by additional value

public static enum MyEnum{ VALUE1(1), VALUE2(2), VALUE3(3); private int index; private MyEnum(int index){ this.index = index;} // Reverse-lookup map for getting a day from an abbreviation private static final Map<Integer, MyEnum> lookup = new HashMap<Integer, MyEnum>(); static { for (MyEnum d : MyEnum.values()) { lookup.put(d.getIndex(), d); } } private int getIndex(){ return Integer.valueOf(this.index); } public static MyEnum getByIndex(Integer index){ return lookup.get(index); } }
Get the enum of a additional value.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.