Int类型枚举
/**
* Int类型枚举
*/
public enum IntEnum {
//
RED(1, "红色"),
GREEN(2, "绿色"),
YELLOW(3, "黄色"),
;
private final Integer code;
private final String name;
IntEnum(Integer code, String name) {
this.code = code;
this.name = name;
}
public Integer getCode() {
return code;
}
public String getName() {
return name;
}
private static final Map<Integer, Object> codeMap = new HashMap<>();
static {
for (IntEnum e : IntEnum.values()) {
codeMap.put(e.getCode(), e);
}
}
/**
* 通过code获取对应枚举
*
* @param code
* @return
*/
public static IntEnum get(Integer code) {
return (IntEnum) codeMap.get(code);
}
/**
* 通过code获取对应枚举name
*
* @param code
* @return
*/
public static String getName(Integer code) {
IntEnum e = get(code);
return e == null ? "" : e.getName();
}
}
Str类型枚举
/**
* Str类型枚举
*/
public enum StrEnum {
//
RED("red", "红色"),
GREEN("green", "绿色"),
YELLOW("yellow", "黄色"),
;
private final String code;
private final String name;
StrEnum(String code, String name) {
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
private static final Map<String, Object> codeMap = new HashMap<>();
static {
for (StrEnum e : StrEnum.values()) {
codeMap.put(e.getCode(), e);
}
}
/**
* 通过code获取对应枚举
*
* @param code
* @return
*/
public static StrEnum get(String code) {
return (StrEnum) codeMap.get(code);
}
/**
* 通过code获取对应枚举name
*
* @param code
* @return
*/
public static String getName(String code) {
StrEnum e = get(code);
return e == null ? "" : e.getName();
}
}
复制代码后通过idea直接修改枚举类名及对应枚举值即可。