{"id":4014,"date":"2025-07-28T21:43:00","date_gmt":"2025-07-28T13:43:00","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=4014"},"modified":"2025-07-28T21:43:00","modified_gmt":"2025-07-28T13:43:00","slug":"%e5%a6%82%e4%bd%95%e8%ae%be%e8%ae%a1%e4%b8%80%e4%b8%aa%e5%85%81%e8%ae%b8-null-key-%e7%9a%84%e5%b9%b6%e5%8f%91-map%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/28\/%e5%a6%82%e4%bd%95%e8%ae%be%e8%ae%a1%e4%b8%80%e4%b8%aa%e5%85%81%e8%ae%b8-null-key-%e7%9a%84%e5%b9%b6%e5%8f%91-map%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u8bbe\u8ba1\u4e00\u4e2a\u5141\u8bb8 null key \u7684\u5e76\u53d1 Map\uff1f"},"content":{"rendered":"\n<p>\u8981\u8bbe\u8ba1\u4e00\u4e2a<strong>\u5141\u8bb8 <code>null<\/code> key \u7684\u5e76\u53d1 Map<\/strong>\uff08\u7c7b\u4f3c <code>HashMap<\/code> \u652f\u6301\u7684\u8bed\u4e49\uff09\uff0c\u4f46\u53c8\u5177\u5907 <code>ConcurrentHashMap<\/code> \u7684\u9ad8\u5e76\u53d1\u7279\u6027\uff0c\u4f60\u9700\u8981\u89e3\u51b3\u4ee5\u4e0b\u5173\u952e\u95ee\u9898\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u8bbe\u8ba1\u76ee\u6807<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u652f\u6301\u5e76\u53d1\u8bfb\u5199\uff08\u7ebf\u7a0b\u5b89\u5168\uff09<\/li>\n\n\n\n<li>\u652f\u6301 <code>null<\/code> \u4f5c\u4e3a key\uff08\u6700\u591a 1 \u4e2a\uff09<\/li>\n\n\n\n<li>\u4fdd\u8bc1\u6027\u80fd\u4e0d\u6bd4 <code>ConcurrentHashMap<\/code> \u5dee\u592a\u591a<\/li>\n\n\n\n<li>\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u6216\u6b7b\u9501<\/li>\n\n\n\n<li>\u517c\u5bb9 Java Map \u63a5\u53e3\u8bed\u4e49<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u95ee\u9898\u6838\u5fc3\u5206\u6790<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Java \u5b98\u65b9 <code>ConcurrentHashMap<\/code> \u4e0d\u652f\u6301 null key<\/h3>\n\n\n\n<p>\u539f\u56e0\u8be6\u89c1\u524d\u9762\u89e3\u7b54\uff0c\u4e3b\u8981\u662f\u4e3a\u907f\u514d\u8bed\u4e49\u6b67\u4e49\u548c\u5e76\u53d1\u9519\u8bef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ConcurrentHashMap&lt;String, String&gt; map = new ConcurrentHashMap&lt;&gt;();\nmap.put(null, \"abc\"); \/\/ \u274c \u629b NullPointerException<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5b9e\u73b0\u65b9\u6848\u8bbe\u8ba1<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6848\u4e00\uff1a\u4f7f\u7528\u7ec4\u5408\u6a21\u5f0f + \u7279\u6b8a\u5904\u7406 null key<\/h3>\n\n\n\n<p>\u6838\u5fc3\u601d\u60f3\uff1a<strong>\u5c06 null key \u5355\u72ec\u5b58\u653e\uff0c\u5176\u4ed6 key \u8d70 <code>ConcurrentHashMap<\/code><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class NullableConcurrentMap&lt;K, V&gt; implements Map&lt;K, V&gt; {\n\n    private final ConcurrentHashMap&lt;K, V&gt; internalMap = new ConcurrentHashMap&lt;&gt;();\n    private final Object nullKeyLock = new Object(); \/\/ \u9501 nullKey\n    private volatile V nullKeyValue = null;\n    private volatile boolean hasNullKey = false;\n\n    @Override\n    public V put(K key, V value) {\n        if (key == null) {\n            synchronized (nullKeyLock) {\n                V old = nullKeyValue;\n                nullKeyValue = value;\n                hasNullKey = true;\n                return old;\n            }\n        }\n        return internalMap.put(key, value);\n    }\n\n    @Override\n    public V get(Object key) {\n        if (key == null) {\n            return hasNullKey ? nullKeyValue : null;\n        }\n        return internalMap.get(key);\n    }\n\n    @Override\n    public V remove(Object key) {\n        if (key == null) {\n            synchronized (nullKeyLock) {\n                if (!hasNullKey) return null;\n                V old = nullKeyValue;\n                nullKeyValue = null;\n                hasNullKey = false;\n                return old;\n            }\n        }\n        return internalMap.remove(key);\n    }\n\n    @Override\n    public boolean containsKey(Object key) {\n        if (key == null) {\n            return hasNullKey;\n        }\n        return internalMap.containsKey(key);\n    }\n\n    @Override\n    public int size() {\n        return internalMap.size() + (hasNullKey ? 1 : 0);\n    }\n\n    @Override\n    public void clear() {\n        internalMap.clear();\n        synchronized (nullKeyLock) {\n            nullKeyValue = null;\n            hasNullKey = false;\n        }\n    }\n\n    \/\/ \u5176\u4ed6\u65b9\u6cd5\u6309 Map \u63a5\u53e3\u5b9e\u73b0\uff0c\u5982 entrySet(), keySet(), values() \u7b49\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u4f7f\u7528\u793a\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;String, String&gt; map = new NullableConcurrentMap&lt;&gt;();\n\nmap.put(null, \"NULL-KEY\");\nmap.put(\"a\", \"value\");\n\nSystem.out.println(map.get(null));   \/\/ \u8f93\u51fa NULL-KEY\nSystem.out.println(map.get(\"a\"));    \/\/ \u8f93\u51fa value\nSystem.out.println(map.containsKey(null)); \/\/ true<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u7ebf\u7a0b\u5b89\u5168\u8bf4\u660e<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u533a\u57df<\/th><th>\u5b9e\u73b0\u673a\u5236<\/th><\/tr><\/thead><tbody><tr><td>\u666e\u901a key \u7684\u5e76\u53d1<\/td><td>\u4f7f\u7528 ConcurrentHashMap \u4fdd\u8bc1<\/td><\/tr><tr><td>null key \u7684\u5e76\u53d1<\/td><td>\u4f7f\u7528\u663e\u5f0f\u540c\u6b65\u9501\u4fdd\u8bc1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6027\u80fd\u65b9\u9762\u5bf9 <code>null key<\/code> \u7684\u64cd\u4f5c\u52a0\u9501\uff0c\u5176\u5b83 key \u4f7f\u7528\u9ad8\u6027\u80fd\u5e76\u53d1 Map\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u6269\u5c55\u4f18\u5316\u5efa\u8bae<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u63d0\u5347\u6027\u80fd\uff1anull key \u7528 <code>AtomicReference&lt;V><\/code> + <code>AtomicBoolean<\/code> \u4ee3\u66ff\u9501<\/strong><\/li>\n\n\n\n<li><strong>\u652f\u6301 value \u4e3a null\uff1f<\/strong>\uff1a\u9700\u660e\u786e\u8bed\u4e49\uff0c\u6216\u7528 Optional<\/li>\n\n\n\n<li><strong>\u6cdb\u578b\u652f\u6301\u66f4\u5f3a\uff1f<\/strong>\uff1a\u53ef\u5b9e\u73b0\u5b8c\u6574 <code>ConcurrentMap&lt;K,V><\/code> \u63a5\u53e3<\/li>\n\n\n\n<li><strong>\u652f\u6301\u5e8f\u5217\u5316\uff1f<\/strong>\uff1a\u589e\u52a0 <code>Serializable<\/code> \u652f\u6301<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u7ee7\u627f <code>ConcurrentHashMap<\/code>\uff1f<\/h2>\n\n\n\n<p><code>ConcurrentHashMap<\/code> \u5185\u90e8\u903b\u8f91\u5927\u91cf\u4f9d\u8d56 key \u975e\u7a7a\uff0c\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int hash = spread(key.hashCode()); \/\/ key == null \u4f1a NPE<\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\u4e0d\u80fd\u901a\u8fc7\u7ee7\u627f\u590d\u7528\u5b83\u7684\u903b\u8f91\uff0c\u5fc5\u987b\u7528\u7ec4\u5408\uff08\u59d4\u6258\uff09\u65b9\u5f0f\u6784\u5efa\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\"><code>Map<\/code> \u63a5\u53e3 Javadoc<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/openjdk\/jdk\/blob\/master\/src\/java.base\/share\/classes\/java\/util\/concurrent\/ConcurrentHashMap.java\"><code>ConcurrentHashMap<\/code> \u6e90\u7801<\/a><\/li>\n\n\n\n<li>\u300aJava \u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300b\u2014\u2014 \u7b2c\u4e94\u7ae0<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3\u4e00\u53e5\u8bdd<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8bbe\u8ba1\u4e00\u4e2a\u652f\u6301 <code>null key<\/code> \u7684\u5e76\u53d1 Map\uff0c\u63a8\u8350\u65b9\u6848\u662f<strong>\u7528\u7ec4\u5408\u6a21\u5f0f+\u7ebf\u7a0b\u5b89\u5168\u7684\u5206\u79bb\u5b58\u50a8<\/strong>\u7b56\u7565\uff0c\u6027\u80fd\u4e0e\u8bed\u4e49\u517c\u987e\uff0c\u662f\u517c\u5bb9 Java Map \u884c\u4e3a\u7684\u7406\u60f3\u5b9e\u73b0\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u8bbe\u8ba1\u4e00\u4e2a\u5141\u8bb8 null key \u7684\u5e76\u53d1 Map\uff08\u7c7b\u4f3c HashMap \u652f\u6301\u7684\u8bed\u4e49\uff09\uff0c\u4f46\u53c8\u5177\u5907 Concurr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4015,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86,68],"tags":[],"class_list":["post-4014","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/4014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=4014"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/4014\/revisions"}],"predecessor-version":[{"id":4016,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/4014\/revisions\/4016"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/4015"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=4014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=4014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=4014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}