Last active
April 5, 2023 15:14
-
-
Save zencd/89ad3c62c765fe69be042f3af6408c45 to your computer and use it in GitHub Desktop.
Find an enum instance by any property of it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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