{"id":71190,"date":"2023-12-27T17:48:29","date_gmt":"2023-12-27T09:48:29","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=71190"},"modified":"2023-12-27T17:48:29","modified_gmt":"2023-12-27T09:48:29","slug":"java-enum-search","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-enum-search.html","title":{"rendered":"\u51e0\u79cdJava \u679a\u4e3e\u67e5\u627e\u4e14\u4e0d\u629b\u5f02\u5e38\u7684\u5b9e\u73b0\u53ca\u5176\u4f18\u52a3\u6bd4\u8f83"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nJava Enum\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f46\u5f88\u591a\u4eba\u901a\u5e38\u5e76\u4e0d\u80fd\u5145\u5206\u5229\u7528\uff0c\u8fd9\u662f\u56e0\u4e3a\u4e00\u4e9b\u5e93\u4e0d\u4f1a\u4f18\u5148\u62e9\u7528\u8be5\u529f\u80fd\u3002\u901a\u5e38\u6211\u4eec\u4e5f\u53ef\u4ee5\u6b63\u786e\u4f7f\u7528Java\u679a\u4e3e\u529f\u80fd\uff0c\u4f46\u5728\u8bb8\u591a\u4ee3\u7801\u5e93\u4e2d\u5f80\u5f80\u5b58\u5728\u7740\u8fd9\u6837\u4e00\u4e2a\u95ee\u9898\uff0c\u4e8e\u662f\u6709\u4e86\u8fd9\u7bc7\u6587\u7ae0\u3002\u95ee\u9898\u5f88\u7b80\u5355\uff1a\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u901a\u8fc7\u540d\u79f0\u6216\u503c\u83b7\u53d6\u679a\u4e3e\uff0c\u5e76\u5ffd\u7565\u4e0d\u5b58\u5728\u7684\u503c\uff1f<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u679a\u4e3e<\/strong><\/div>\n

\u8fd9\u662f\u6211\u4eec\u5c06\u5728\u793a\u4f8b\u4e2d\u4f7f\u7528\u7684\u679a\u4e3e\u3002 \u6311\u9009\u66f4\u590d\u6742\u7684\u679a\u4e3e\u662f\u4e3a\u4e86\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u5b57\u6bb5\u5c55\u793a\u67e5\u627e\u679a\u4e3e\u3002<\/p>\n

public enum CardColor {\r\n    RED,\r\n    BLACK,\r\n    ;\r\n}\r\n\/\/ Jackson annotation to print the enum as an Object instead of the default name.\r\n@JsonFormat(shape = JsonFormat.Shape.OBJECT)\r\npublic enum CardSuit {\r\n    \/\/ Unicode suits - https:\/\/en.wikipedia.org\/wiki\/Playing_cards_in_Unicode\r\n    SPADE(\"Spade\", String.valueOf((char) 0x2660), CardColor.BLACK),\r\n    HEART(\"Heart\", String.valueOf((char) 0x2665), CardColor.RED),\r\n    DIAMOND(\"Diamond\", String.valueOf((char) 0x2666), CardColor.RED),\r\n    CLUB(\"Club\", String.valueOf((char) 0x2663), CardColor.BLACK),\r\n    ;\r\n    private String displayName;\r\n    private String symbol;\r\n    private CardColor color;\r\n    private CardSuit(String displayName, String symbol, CardColor color) {\r\n        this.displayName = displayName;\r\n        this.symbol =  symbol;\r\n        this.color = color;\r\n    }\r\n    public String getDisplayName() {\r\n        return displayName;\r\n    }\r\n    public void setDisplayName(String displayName) {\r\n        this.displayName = displayName;\r\n    }\r\n    public String getSymbol() {\r\n        return symbol;\r\n    }\r\n    public void setSymbol(String symbol) {\r\n        this.symbol = symbol;\r\n    }\r\n    public CardColor getColor() {\r\n        return color;\r\n    }\r\n    public void setColor(CardColor color) {\r\n        this.color = color;\r\n    }<\/pre>\n

\u5728\u00a0GitHub<\/a>\u00a0\u4e0a\u67e5\u770b\u3002<\/p>\n

\u95ee\u9898<\/strong><\/div>\n

\u5728\u4f60\u77e5\u9053\u8f93\u5165\u6709\u6548\u7684\u65f6\u5019\uff0c\u4f7f\u7528Enum.valueOf\u975e\u5e38\u68d2\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f20\u5165\u65e0\u6548\u7684\u540d\u79f0\uff0c\u90a3\u4e48\u5c06\u629b\u51fa\u5f02\u5e38\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u5f88\u597d\u3002\u4e0d\u8fc7\uff0c\u5728\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5b81\u613f\u5ffd\u7565\u5f02\u5e38\u5e76\u8fd4\u56denull\u3002<\/p>\n

log.debug(\"Running valueOf\");\r\nfor (String name : names) {\r\n    try {\r\n        log.debug(\"looking up {} found {}\", name, Json.serializer().toString(CardSuit.valueOf(name)));\r\n    } catch (Exception ex) {\r\n        log.warn(\"Exception Thrown\", ex);\r\n    }\r\n}<\/pre>\n

 <\/p>\n

2017-02-22 14:46:38.556 [main] DEBUG c.s.examples.common.EnumLookup - Running valueOf\r\n2017-02-22 14:46:38.804 [main] DEBUG c.s.examples.common.EnumLookup - looking up SPADE found {\"displayName\":\"Spade\",\"symbol\":\"\u2660\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.806 [main] DEBUG c.s.examples.common.EnumLookup - looking up HEART found {\"displayName\":\"Heart\",\"symbol\":\"\u2665\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.806 [main] DEBUG c.s.examples.common.EnumLookup - looking up DIAMOND found {\"displayName\":\"Diamond\",\"symbol\":\"\u2666\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.806 [main] DEBUG c.s.examples.common.EnumLookup - looking up CLUB found {\"displayName\":\"Club\",\"symbol\":\"\u2663\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.808 [main] WARN  c.s.examples.common.EnumLookup - Exception Thrown\r\njava.lang.IllegalArgumentException: No enum constant com.stubbornjava.examples.common.EnumLookup.CardSuit.Missing\r\n  at java.lang.Enum.valueOf(Enum.java:238)\r\n  at com.stubbornjava.examples.common.EnumLookup$CardSuit.valueOf(EnumLookup.java:1)\r\n  at com.stubbornjava.examples.common.EnumLookup.main(EnumLookup.java:154)<\/pre>\n
\u62d9\u52a3\u7684\u5b9e\u73b0<\/strong><\/div>\n

\u5f88\u4e0d\u5e78\u7684\u662f\uff0c\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\u5728\u4ee3\u7801\u5e93\u4e2d\u51fa\u73b0\u5f97\u662f\u8fd9\u4e48\u7684\u9891\u7e41\u3002\u53cd\u9762\u4f8b\u5b50\uff0c\u52ff\u5b66\u3002<\/p>\n

Enum.valueOf With Try Catch\uff08\u52a3\uff09<\/strong><\/span><\/div>\n

\u8fd9\u79cd\u62d9\u52a3\u7684\u505a\u6cd5\u6700\u5e38\u89c1\u4e8e\u521d\u5b66\u8005\u3002\u5f02\u5e38\u4e0d\u5e94\u8be5\u7528\u4e8e\u63a7\u5236\u6d41\uff0c\u5e76\u4e14\u8fd9\u6837\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u6027\u80fd\u5f71\u54cd\u3002\u4e0d\u8981\u5077\u61d2\u3002\u4f60\u5fc5\u987b\u7528\u6b63\u786e\u7684\u65b9\u5f0f\u53bb\u505a\u3002<\/p>\n

\/*\r\n * Please don't do this! Using try \/ catch for\r\n * control flow is a bad practice.\r\n *\/\r\npublic static CardSuit trycatchValueOf(String name) {\r\n    try {\r\n        return CardSuit.valueOf(name);\r\n    } catch (Exception ex) {\r\n        log.warn(\"Exception Thrown\", ex);\r\n        return null;\r\n    }\r\n}<\/pre>\n

 <\/p>\n

log.debug(\"Running trycatchValueOf\");\r\nfor (String name : names) {\r\n    log.debug(\"looking up {} found {}\", name, Json.serializer().toString(CardSuit.trycatchValueOf(name)));\r\n}<\/pre>\n

 <\/p>\n

2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - Running trycatchValueOf\r\n2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - looking up SPADE found {\"displayName\":\"Spade\",\"symbol\":\"\u2660\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - looking up HEART found {\"displayName\":\"Heart\",\"symbol\":\"\u2665\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - looking up DIAMOND found {\"displayName\":\"Diamond\",\"symbol\":\"\u2666\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - looking up CLUB found {\"displayName\":\"Club\",\"symbol\":\"\u2663\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.809 [main] WARN  c.s.examples.common.EnumLookup - Exception Thrown\r\njava.lang.IllegalArgumentException: No enum constant com.stubbornjava.examples.common.EnumLookup.CardSuit.Missing\r\n  at java.lang.Enum.valueOf(Enum.java:238)\r\n  at com.stubbornjava.examples.common.EnumLookup$CardSuit.valueOf(EnumLookup.java:1)\r\n  at com.stubbornjava.examples.common.EnumLookup$CardSuit.trycatchValueOf(EnumLookup.java:89)\r\n  at com.stubbornjava.examples.common.EnumLookup.main(EnumLookup.java:171)\r\n2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - looking up Missing found null<\/pre>\n
\u901a\u8fc7\u8fed\u4ee3\u67e5\u627e\uff08\u52a3\uff09<\/strong><\/span><\/div>\n

\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u5f88\u5e38\u89c1\uff08\u89c1\u8fd9\u91cc<\/a>\uff09\uff0c\u4f46\u81f3\u5c11\u7a0b\u5e8f\u5458\u77e5\u9053\u4e0d\u80fd\u7528try\/catch\u6765\u6355\u83b7\u5f02\u5e38\u3002\u90a3\u4e48\uff0c\u8fd9\u79cd\u65b9\u6cd5\u6709\u4ec0\u4e48\u95ee\u9898\u5462\uff1f\u6ca1\u9519\uff0c\u5b83\u4f1a\u8fed\u4ee3\u6240\u6709\u679a\u4e3e\uff0c\u76f4\u5230\u627e\u5230\u5339\u914d\u7684\u679a\u4e3e\u6216\u8fd4\u56denull\u2014\u2014\u6700\u574f\u7684\u60c5\u51b5\u4e0b\u9700\u8981n\u6b21\uff0c\u5176\u4e2dn\u5c31\u662f\u679a\u4e3e\u503c\u7684\u6570\u91cf\u3002\u6709\u4e9b\u4eba\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u8fd9\u5fae\u4e0d\u8db3\u9053\uff0c\u8fd9\u662f\u8fc7\u65e9\u4f18\u5316\u4e86\u800c\u5df2\u3002\u4f46\u662f\uff0c\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u662fCS\u57fa\u7840\u3002\u4f7f\u7528Map\u800c\u4e0d\u662f\u8fed\u4ee3\u96c6\u5408\u8981\u7701\u529b\u5f97\u591a\u3002\u8fd9\u4f1a\u5927\u5927\u63d0\u9ad8\u6027\u80fd\u5417\uff1f\u4e0d\uff0c\u4f46\u5b83\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4e60\u60ef\u3002\u5728\u9762\u8bd5\u5019\u9009\u4eba\u65f6\uff0c\u4f60\u4f1a\u5bf9\u7ebf\u6027\u590d\u6742\u5ea6\u641c\u7d22\u7b97\u6cd5\u611f\u5230\u6ee1\u610f\u5417\uff1f\u6b64\u65f6\uff0c\u4f60\u4e0d\u5e94\u8be5\u8ba9\u8fd9\u6837\u7684\u4ee3\u7801\u5ba1\u67e5\u901a\u8fc7\u3002<\/p>\n

\/*\r\n * Please don't do this! It is inefficient and it's\r\n * not very hard to use Guava or a static Map as an index.\r\n *\/\r\npublic static CardSuit iterationFindByName(String name) {\r\n    for (CardSuit suit : CardSuit.values()) {\r\n        if (name.equals(suit.name())) {\r\n            return suit;\r\n        }\r\n    }\r\n    return null;\r\n}<\/pre>\n

 <\/p>\n

log.debug(\"Running iteration\");\r\nfor (String name : names) {\r\n    log.debug(\"looking up {} found {}\", name, Json.serializer().toString(CardSuit.iterationFindByName(name)));\r\n}<\/pre>\n

 <\/p>\n

2017-02-22 14:46:38.808 [main] DEBUG c.s.examples.common.EnumLookup - Running iteration\r\n2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - looking up SPADE found {\"displayName\":\"Spade\",\"symbol\":\"\u2660\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - looking up HEART found {\"displayName\":\"Heart\",\"symbol\":\"\u2665\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - looking up DIAMOND found {\"displayName\":\"Diamond\",\"symbol\":\"\u2666\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - looking up CLUB found {\"displayName\":\"Club\",\"symbol\":\"\u2663\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - looking up Missing found null<\/pre>\n
\u8f83\u4f18\u7684\u5b9e\u73b0<\/strong><\/div>\n

\u4ee5\u4e0b\u90fd\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Map\u5f62\u5f0f\u7684\u7d22\u5f15\u8d77\u4f5c\u7528\u3002\u4f46\u662f\uff0c\u5b83\u4eec\u4e4b\u95f4\u4e5f\u6709\u4e00\u4e9b\u7ec6\u5fae\u7684\u5dee\u522b\u3002<\/p>\n

\u9759\u6001Map\u7d22\u5f15\uff08\u8f83\u4f18\uff09<\/strong><\/span><\/div>\n

\u7528\u4e8e\u5feb\u901f\u67e5\u627e\u56fa\u5b9a\u5927\u5c0f\u7684\u6b63\u786e\u6570\u636e\u7ed3\u6784\u662f\u4ec0\u4e48\uff1f\u90a3\u5c31\u662fHashMap\u3002\u73b0\u5728\u901a\u8fc7\u4e00\u4e9b\u989d\u5916\u7684\u6837\u677f\uff0c\u53ea\u8981\u6211\u4eec\u6709\u5f88\u597d\u7684\u6563\u5217\u51fd\u6570\u7684\u8bdd\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u66f4\u6709\u6548\u7684\u67e5\u627e\u3002\u7a0d\u5fae\u66f4\u5197\u957f\uff0c\u4f46\u5982\u679c\u6709\u529e\u6cd5\u51cf\u5c11\u6837\u677f\u7684\u8bdd\uff0c\u8fd9\u5c06\u662f\u6781\u597d\u7684\u3002<\/p>\n

private static final Map<String, CardSuit> nameIndex =\r\n        Maps.newHashMapWithExpectedSize(CardSuit.values().length);\r\nstatic {\r\n    for (CardSuit suit : CardSuit.values()) {\r\n        nameIndex.put(suit.name(), suit);\r\n    }\r\n}\r\npublic static CardSuit lookupByName(String name) {\r\n    return nameIndex.get(name);\r\n}<\/pre>\n
log.debug(\"Running lookupByName\");\r\nfor (String name : names) {\r\n    log.debug(\"looking up {} found {}\", name, Json.serializer().toString(CardSuit.lookupByName(name)));\r\n}<\/pre>\n
2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - Running lookupByName\r\n2017-02-22 14:46:38.809 [main] DEBUG c.s.examples.common.EnumLookup - looking up SPADE found {\"displayName\":\"Spade\",\"symbol\":\"\u2660\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.810 [main] DEBUG c.s.examples.common.EnumLookup - looking up HEART found {\"displayName\":\"Heart\",\"symbol\":\"\u2665\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.810 [main] DEBUG c.s.examples.common.EnumLookup - looking up DIAMOND found {\"displayName\":\"Diamond\",\"symbol\":\"\u2666\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.813 [main] DEBUG c.s.examples.common.EnumLookup - looking up CLUB found {\"displayName\":\"Club\",\"symbol\":\"\u2663\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.813 [main] DEBUG c.s.examples.common.EnumLookup - looking up Missing found null<\/pre>\n
Guava Enums.getIfPresent\uff08\u63a8\u8350\uff09<\/strong><\/span><\/div>\n

\u8fd9\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7528\u4f8b\uff0c\u6211\u4eec\u5728Google\u7684\u670b\u53cb\u4e3a\u6b64\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u5e72\u51c0\u4e14\u65e0\u9700\u6837\u677f\u7684\u89e3\u51b3\u65b9\u6848\u3002\u63a2\u5176\u7a76\u7adf\uff0c\u5b83\u751a\u81f3\u4f7f\u7528\u4e86WeakReferences\u548cWeakHashMap<\/a>s\u3002\u57fa\u672c\u8bf4\u6765\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u5168\u5c40\u7684\u9759\u6001\u5730\u56fe\uff0c\u5e76\u4ee5Enum\u7684\u7c7b\u540d\u79f0\u952e\u5165\uff0c\u5e76\u5c06\u5176\u7528\u4e8e\u67e5\u627e\u3002<\/p>\n

public static CardSuit getIfPresent(String name) {\r\n    return Enums.getIfPresent(CardSuit.class, name).orNull();\r\n}<\/pre>\n
log.debug(\"Running Guava getIfPresent\");\r\nfor (String name : names) {\r\n    log.debug(\"looking up {} found {}\", name, Json.serializer().toString(CardSuit.getIfPresent(name)));\r\n}<\/pre>\n
2017-02-22 14:46:38.813 [main] DEBUG c.s.examples.common.EnumLookup - Running Guava getIfPresent\r\n2017-02-22 14:46:38.814 [main] DEBUG c.s.examples.common.EnumLookup - looking up SPADE found {\"displayName\":\"Spade\",\"symbol\":\"\u2660\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.814 [main] DEBUG c.s.examples.common.EnumLookup - looking up HEART found {\"displayName\":\"Heart\",\"symbol\":\"\u2665\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.815 [main] DEBUG c.s.examples.common.EnumLookup - looking up DIAMOND found {\"displayName\":\"Diamond\",\"symbol\":\"\u2666\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.815 [main] DEBUG c.s.examples.common.EnumLookup - looking up CLUB found {\"displayName\":\"Club\",\"symbol\":\"\u2663\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.815 [main] DEBUG c.s.examples.common.EnumLookup - looking up Missing found null<\/pre>\n
\u901a\u8fc7\u5b57\u6bb5\u7684\u8fdb\u4e00\u6b65\u7d22\u5f15<\/strong><\/div>\n

\u8fd9\u4e2a\u5b8c\u5168\u76f8\u540c\u7684\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u679a\u4e3e\u7684\u5176\u4ed6\u5b57\u6bb5\u3002\u60f3\u8981\u901a\u8fc7\u5176\u663e\u793a\u7684\u540d\u79f0\u6216\u5176\u4ed6\u5c5e\u6027\u6765\u67e5\u627e\u679a\u4e3e\u5e76\u4e0d\u5c11\u89c1\u3002<\/p>\n

\u901a\u8fc7\u5b57\u6bb5\u7d22\u5f15\u7684\u9759\u6001Map\uff08\u8f83\u4f18\uff09<\/strong><\/span><\/div>\n

\u4e0e\u4e0a\u8ff0\u76f8\u540c\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u5728\u663e\u793a\u540d\u79f0\u4e0a\u800c\u4e0d\u662f\u679a\u4e3e\u540d\u79f0\u4e0a\u8fdb\u884c\u7d22\u5f15\u3002<\/p>\n

private static final Map<String, CardSuit> displayNameIndex =\r\n        Maps.newHashMapWithExpectedSize(CardSuit.values().length);\r\nstatic {\r\n    for (CardSuit suit : CardSuit.values()) {\r\n        displayNameIndex.put(suit.getDisplayName(), suit);\r\n    }\r\n}\r\npublic static CardSuit lookupByDisplayName(String name) {\r\n    return displayNameIndex.get(name);\r\n}<\/pre>\n
log.debug(\"Running lookupByDisplayName\");\r\nfor (String displayName : displayNames) {\r\n    log.debug(\"looking up {} found {}\", displayName, Json.serializer().toString(CardSuit.lookupByDisplayName(displayName)));\r\n}<\/pre>\n

 <\/p>\n

2017-02-22 14:46:38.815 [main] DEBUG c.s.examples.common.EnumLookup - Running lookupByDisplayName\r\n2017-02-22 14:46:38.815 [main] DEBUG c.s.examples.common.EnumLookup - looking up Spade found {\"displayName\":\"Spade\",\"symbol\":\"\u2660\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.815 [main] DEBUG c.s.examples.common.EnumLookup - looking up Heart found {\"displayName\":\"Heart\",\"symbol\":\"\u2665\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.815 [main] DEBUG c.s.examples.common.EnumLookup - looking up Diamond found {\"displayName\":\"Diamond\",\"symbol\":\"\u2666\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.816 [main] DEBUG c.s.examples.common.EnumLookup - looking up Club found {\"displayName\":\"Club\",\"symbol\":\"\u2663\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.816 [main] DEBUG c.s.examples.common.EnumLookup - looking up Missing found null<\/pre>\n
\u901a\u8fc7\u5b57\u6bb5\u7d22\u5f15\u7684\u9759\u6001Map\uff08\u8f83\u4f18\uff09<\/strong><\/span><\/div>\n

\u6211\u4eec\u4e0d\u80fd\u5728\u8fd9\u91cc\u5229\u7528Guava\uff0c\u56e0\u4e3a\u5bf9\u4e8e\u9759\u6001\u7d22\u5f15\u521b\u5efa\u552f\u4e00\u7684\u5168\u5c40\u5bc6\u94a5\u5c06\u662f\u4e00\u4ef6\u56f0\u96be\u7684\u4e8b\u3002\u4f46\u662f\uff0c\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u6211\u4eec\u6ca1\u6709\u5e2e\u624b\uff01<\/p>\n

public class EnumUtils {\r\n    public static <T, E extends Enum<E>> Function<T, E> lookupMap(Class<E> clazz, Function<E, T> mapper) {\r\n        @SuppressWarnings(\"unchecked\")\r\n        E[] emptyArray = (E[]) Array.newInstance(clazz, 0);\r\n        return lookupMap(EnumSet.allOf(clazz).toArray(emptyArray), mapper);\r\n    }\r\n    public static <T, E extends Enum<E>> Function<T, E> lookupMap(E[] values, Function<E, T> mapper) {\r\n        Map<T, E> index = Maps.newHashMapWithExpectedSize(values.length);\r\n        for (E value : values) {\r\n            index.put(mapper.apply(value), value);\r\n        }\r\n        return (T key) -> index.get(key);\r\n    }\r\n}<\/pre>\n

 <\/p>\n

\u73b0\u5728\u6211\u4eec\u6709\u4e86\u4e00\u4e2a\u76f8\u4e0e\u6837\u677f\u6ca1\u591a\u5927\u5173\u8054\u7684\u901a\u7528\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n

private static final Function<String, CardSuit> func =\r\n        EnumUtils.lookupMap(CardSuit.class, e -> e.getDisplayName());\r\npublic static CardSuit lookupByDisplayNameUtil(String name) {\r\n    return func.apply(name);\r\n}<\/pre>\n
log.debug(\"Running lookupByDisplayNameUtil\");\r\nfor (String displayName : displayNames) {\r\n    log.debug(\"looking up {} found {}\", displayName, Json.serializer().toString(CardSuit.lookupByDisplayNameUtil(displayName)));\r\n}<\/pre>\n
2017-02-22 14:46:38.816 [main] DEBUG c.s.examples.common.EnumLookup - Running lookupByDisplayNameUtil\r\n2017-02-22 14:46:38.816 [main] DEBUG c.s.examples.common.EnumLookup - looking up Spade found {\"displayName\":\"Spade\",\"symbol\":\"\u2660\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.816 [main] DEBUG c.s.examples.common.EnumLookup - looking up Heart found {\"displayName\":\"Heart\",\"symbol\":\"\u2665\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.816 [main] DEBUG c.s.examples.common.EnumLookup - looking up Diamond found {\"displayName\":\"Diamond\",\"symbol\":\"\u2666\",\"color\":\"RED\"}\r\n2017-02-22 14:46:38.816 [main] DEBUG c.s.examples.common.EnumLookup - looking up Club found {\"displayName\":\"Club\",\"symbol\":\"\u2663\",\"color\":\"BLACK\"}\r\n2017-02-22 14:46:38.816 [main] DEBUG c.s.examples.common.EnumLookup - looking up Missing found null<\/pre>\n
\u7ed3\u8bba<\/strong><\/div>\n

\u8fd9\u91cc\u6709\u82e5\u5e72\u65b9\u6cd5\u90fd\u53ef\u7528\u4e8e\u89e3\u51b3\u540c\u4e00\u4e2a\u95ee\u9898\u3002\u6709\u4e9b\u62d9\u52a3\uff0c\u6709\u4e9b\u66f4\u4f18\u3002<\/p>\n

\n

\u82f1\u6587\u539f\u6587\uff1aJava Enum Lookup by Name or Field Without Throwing Exceptions<\/a><\/p>\n

\u7ffb\u8bd1\u4f5c\u8005\uff1a\u7801\u519c\u7f51<\/a>\u00a0\u2013 \u5c0f\u5cf0<\/p>\n<\/div>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.codeceo.com\/article\/java-enum-lookup-no-exception.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/java-enum-search.html<\/a>\u7f16\u8f91\u5458\uff1a\u534e\u4e16\u53d1\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb Java Enum\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f46\u5f88\u591a\u4eba\u901a\u5e38\u5e76\u4e0d\u80fd\u5145\u5206\u5229\u7528\uff0c\u8fd9\u662f\u56e0\u4e3a\u4e00\u4e9b\u5e93\u4e0d\u4f1a\u4f18\u5148\u62e9\u7528\u8be5\u529f\u80fd\u3002\u901a […]<\/p>\n","protected":false},"author":63,"featured_media":85860,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-71190","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/71190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=71190"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/71190\/revisions"}],"predecessor-version":[{"id":85921,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/71190\/revisions\/85921"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/85860"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=71190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=71190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=71190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}