我有以下枚举。我需要通过代码获取描述。我有以下代码。它正在工作,但仍可以改进吗?
public enum Maps {
COLOR_RED("ABC", "abc description");
private final String code;
private final String description;
private static Map<String, String> mMap;
private Maps(String code, String description) {
this.code = code;
this.description = description;
}
public String getCode() {
return code;
}
public String getDescription() {
return description;
}
public static String getDescriptionByCode(String code) {
if (mMap == null) {
initializeMapping();
}
if (mMap.containsKey(code)) {
return mMap.get(code);
}
return null;
}
private static void initializeMapping() {
mMap = new HashMap<String, String>();
for (Maps s : Maps.values()) {
mMap.put(s.code, s.description);
}
}
}