Java: Check if enum contains a given string?

Jared picture Jared · Feb 8, 2011 · Viewed 345.5k times · Source

Here's my problem - I'm looking for (if it even exists) the enum equivalent of ArrayList.contains();.

Here's a sample of my code problem:

enum choices {a1, a2, b1, b2};

if(choices.???(a1)}{
//do this
} 

Now, I realize that an ArrayList of Strings would be the better route here but I have to run my enum contents through a switch/case elsewhere. Hence my problem.

Assuming something like this doesn't exist, how could I go about doing it?

Answer

RaphC picture RaphC · Feb 12, 2014

Use the Apache commons lang3 lib instead

 EnumUtils.isValidEnum(MyEnum.class, myValue)