I have an enum Foo
public enum Foo {
A("first"),
B("second"),
private final String value;
private Foo(String value) {
this.value = value;
}
public String value() {
return this.value;
}
}
Below I am trying to check if a string is contained in my enum.
public boolean isValidFoo(String fooStr) {
return EnumSet.allOf(Foo.class)
.contains(Foo.valueOf(fooStr.toUpperCase()));
}
This works when I send A or B for fooStr.
How to make it work when I send first or second for fooStr?
Arrays.stream(Foo.values()).map(Foo::value).collect(toSet()), store this Set in a static field somewhere, and usetheSet.contains(fooStr).HashMapcreated at start mapping values to enum; a staticSetalso created at start containing all values; or ...valuewould help in that? (containschecks for an enum instance)