Skip to content

Instantly share code, notes, and snippets.

@zencd
Last active April 5, 2023 15:14
Show Gist options
  • Save zencd/89ad3c62c765fe69be042f3af6408c45 to your computer and use it in GitHub Desktop.
Save zencd/89ad3c62c765fe69be042f3af6408c45 to your computer and use it in GitHub Desktop.
Find an enum instance by any property of it
enum Foo {
ONE, TWO;
private static class Helper {
public static Map<String, Foo> enumByName = new HashMap<>();
static {
Arrays.stream(Foo.values()).forEach(it -> enumByName.put(it.name(), it));
assert Foo.Helper.enumByName.get("ONE") == Foo.ONE; // usage
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment