I'm trying to lookup against an Enum set, knowing that there will often be a non-match which throws an exception: I would like to check the value exists before performing the lookup to avoid the exceptions. My enum looks something like this:
public enum Fruit {
APPLE("apple"),
ORANGE("orange");
;
private final String fruitname;
Fruit(String fruitname) {
this.fruitname = fruitname;
}
public String fruitname() {return fruitname;}
}
and I want to check if, say, "banana" is one of my enum values before attempting to use the relevant enum. I could iterate through the permissible values comparing my string to
Fruit.values()[i].fruitname
but I'd like to be able to do something like (pseduo-code):
if (Fruit.values().contains(myStringHere)) {...
Is that possible? Should I be using something else entirely (Arrays? Maps?)?
EDIT: in the end I've gone with NawaMan's suggestion, but thanks to everyone for all the helpful input.