/**
* xx枚举
*/
public enum ColorEnum {
/**
*
*/
RED(1, "红色"),
GREEN(2, "绿色"),
YELLOW(3, "黄色"),
;
private final Integer index;
private final String name;
ColorEnum(Integer index, String name) {
this.index = index;
this.name = name;
}
public Integer getIndex() {
return index;
}
public String getName() {
return name;
}
//枚举转化成Map,方便获取枚举值
private static final Map<Integer, Object> indexMap = new HashMap<>();
static {
for (ColorEnum e : ColorEnum.values()) {
indexMap.put(e.getIndex(), e);
}
}
/**
* 通过index获取对应枚举name,index无效返回空字符串
*
* @param index
* @return
*/
public static String getName(Integer index) {
ColorEnum e = get(index);
return e == null ? "" : e.getName();
}
/**
* 通过index获取对应枚举
*
* @param index
* @return
*/
public static ColorEnum get(Integer index) {
return (ColorEnum) indexMap.get(index);
}
}
复制代码后通过idea直接修改枚举类名,并增加对应枚举即可。