How to make key value like enum in java
You can hold multiple values in one enum
and even have getters to handle them. Here is an example I used once (I try to adapt it to your problem):
public enum Status{
ACTIVE(1, "Active"),
INACTIVE(2, "In Active");
private final Integer value;
private final String text;
/**
* A mapping between the integer code and its corresponding text to facilitate lookup by code.
*/
private static Map<Integer, Status> valueToTextMapping;
private Status(Integer value, String text){
this.value = value;
this.text = text;
}
public static Status getStatus(Integer i){
if(valueToTextMapping == null){
initMapping();
}
return valueToTextMapping.get(i);
}
private static void initMapping(){
valueToTextMapping = new HashMap<>();
for(Status s : values()){
valueToTextMapping.put(s.value, s);
}
}
public Integer getValue(){
return value;
}
public String getText(){
return text;
}
@Override
public String toString(){
final StringBuilder sb = new StringBuilder();
sb.append("Status");
sb.append("{value=").append(value);
sb.append(", text='").append(text).append('\'')
sb.append('}');
return sb.toString();
}
}
So in your code you can simply use Status.ACTIVE
and it will represent an instance of your Enum, that holds value
and text
the way you want it
You can not put space between strings. Instead of the you can use underscore as follows:
In_Active
You can use this way:
enum Status {
ACTIVE("Active", 1), IN_ACTIVE("In Active", 2);
private final String key;
private final Integer value;
Status(String key, Integer value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public Integer getValue() {
return value;
}
}
You can't put a space in the middle of an identifier.
Check out this link Is it possible to assign numeric value to an enum in Java? for assigning the value to an enum in java.