public class EnumTest {
public static
> boolean contains(Class _enumClass, String value) {
try {
return EnumSet.allOf(_enumClass).contains(Enum.valueOf(_enumClass, value));
} catch (Exception e) {
return false;
}
}
public static void main(String[] args) {
for (String value : new String[]{"Y", "a3", null}) {
System.out.println(contains(YesNo.class, value));
}
}
}
public enum YesNo {
Y("Yes"),
N("No");
private String label;
YesNo(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
}
source: http://stackoverflow.com/questions/4936819/java-check-if-enum-contains-a-given-string