If I have something like this:
public enum Collection {
Name, Type, All
}
And I would like to use the enumkey for the string too. I can say Collection.Name.toString - thats fine. But I would like to do this without .toString Is there an easy solution for this? I saw a lot of stuff but they were too big with switch or with a lot of if. No other way? Thank you!
Edit: the solution with Collection.something.name() is nice. But is there another way to get the string with Collection.something ?
Use Enum's name() method:
Collection.Name.name()
Collection.Type.name()
// etc
I don't know if this is a valid solution, but this would work too:
Collection.Name + ""
It's looks like you are searching smt like
enum Collection {
Name("name"), Type("type"), All("all");
String id;
Collection(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
or
enum Collection2 {
Name, Type, All;
private static final Map<Collection2, String> ids;
static {
ids = new HashMap<Collection2, String>();
ids.put(Name, "name");
ids.put(Type, "type");
ids.put(All, "all");
//here can be sam validation on id's uniq
}
public String getId() {
return ids.get(this);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With