Since there was a purpose to reverse Enum itself from the value of Enum, I will leave it as a memorandum.
Implementation example in Enum with String value as value
public enum Mode {
MODEA("a"),
MODEB("b");
private String mode;
private Mode(String mode) {
this.mode = mode;
}
/**Create a reverse map in advance and bring it in the field*/
private static final Map<String, Mode> modeMap = new HashMap<String, Mode>() {
private static final long serialVersionUID = 1L;
{
for (Mode mode : Mode.values()) {
put(mode.mode, mode);
}
}
};
/**Get Enum from value*/
public static Mode getMode(String mode) {
return modeMap.get(mode);
}
}
User side
Mode modeA = Mode.getMode("a");
You can create a reverse map in advance and get an Enum using String as a key. Since it is a map, it can be accessed directly and processing efficiency is good.
Recommended Posts