{"id":4006,"date":"2025-07-28T21:36:49","date_gmt":"2025-07-28T13:36:49","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=4006"},"modified":"2025-07-28T21:36:49","modified_gmt":"2025-07-28T13:36:49","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-hashtable-%e6%94%af%e6%8c%81-null-%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/28\/%e4%b8%ba%e4%bb%80%e4%b9%88-hashtable-%e6%94%af%e6%8c%81-null-%e5%80%bc%ef%bc%9f\/","title":{"rendered":"\u4e3a\u4ec0\u4e48 HashTable \u652f\u6301 null \u503c\uff1f"},"content":{"rendered":"\n<p><code>Hashtable<\/code> \u5b9e\u9645\u4e0a <strong>\u4e0d\u652f\u6301 <code>null<\/code> \u7684 key \u548c value<\/strong>\uff0c<strong>\u8fd9\u662f\u4e00\u79cd\u5e38\u89c1\u8bef\u89e3<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u53e5\u8bdd\u56de\u7b54\uff1a<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong><code>Hashtable<\/code> \u4e0d\u5141\u8bb8 key \u6216 value \u4e3a <code>null<\/code>\u3002<\/strong><br>\u5c1d\u8bd5 put \u6216 get \u65f6\u4f7f\u7528 null\uff0c\u4f1a\u629b\u51fa <code>NullPointerException<\/code>\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5b98\u65b9\u6587\u6863\u8bf4\u660e<\/h2>\n\n\n\n<p>\u6765\u81ea Java \u5b98\u65b9\u6587\u6863\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Neither the key nor the value can be null.<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>\u5b98\u65b9\u94fe\u63a5\uff1a<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Hashtable.html\">Hashtable (Java SE 8)<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u9a8c\u8bc1\u4ee3\u7801\u793a\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Hashtable;\n\npublic class TestHashtable {\n    public static void main(String&#91;] args) {\n        Hashtable&lt;String, String&gt; table = new Hashtable&lt;&gt;();\n\n        table.put(\"name\", \"Alice\");  \/\/ \u2705 \u6b63\u5e38\n        \/\/ table.put(null, \"value\"); \/\/ \u274c \u629b\u51fa NullPointerException\n        \/\/ table.put(\"key\", null);   \/\/ \u274c \u629b\u51fa NullPointerException\n    }\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8f93\u51fa\uff08\u8fd0\u884c\u65f6\u5f02\u5e38\uff09\uff1a<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>Exception in thread \"main\" java.lang.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\u4e3a\u4ec0\u4e48\u4e0d\u652f\u6301 null\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Hashtable \u662f\u65e9\u671f\u5e76\u53d1\u5b89\u5168\u7c7b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Hashtable<\/code> \u662f Java 1.0 \u5c31\u5b58\u5728\u7684\u7c7b\uff0c\u5176\u8bbe\u8ba1\u76ee\u6807\u662f<strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\u3002<\/li>\n\n\n\n<li>\u5185\u90e8\u65b9\u6cd5\u666e\u904d\u52a0\u4e86 <code>synchronized<\/code> \u4fee\u9970\uff0c\u610f\u5473\u7740\u591a\u4e2a\u7ebf\u7a0b\u53ef\u5e76\u53d1\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong>\u5728\u591a\u7ebf\u7a0b\u4e2d\u5904\u7406 null \u7684\u884c\u4e3a\u5bb9\u6613\u4ea7\u751f\u8bed\u4e49\u6b67\u4e49\u548c\u5e76\u53d1\u98ce\u9669\u3002<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String value = table.get(\"key\"); \/\/ \u8fd4\u56de null<\/code><\/pre>\n\n\n\n<p>\u4f60\u5f88\u96be\u5224\u65ad\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>key \u5b58\u5728\u4f46 value \u662f null\uff1f<\/li>\n\n\n\n<li>\u8fd8\u662f key \u6839\u672c\u4e0d\u5b58\u5728\uff1f<\/li>\n<\/ul>\n\n\n\n<p>\u56e0\u6b64\uff1a<strong>\u4e3a\u907f\u514d\u6b67\u4e49\uff0c\u76f4\u63a5\u7981\u6b62 null<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u6e90\u4ee3\u7801\u9a8c\u8bc1<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public synchronized V put(K key, V value) {\n    if (key == null || value == null) {\n        throw new NullPointerException();\n    }\n    ...\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Hashtable<\/code> \u660e\u786e\u629b\u51fa <code>NullPointerException<\/code><\/li>\n\n\n\n<li>\u76ee\u7684\u662f\u63d0\u524d\u5931\u8d25\uff08fail-fast\uff09\uff0c\u907f\u514d\u540e\u7eed\u590d\u6742\u903b\u8f91\u9519\u8bef<\/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\">\ud83d\udd0d \u56db\u3001\u4e0e HashMap \u7684\u5bf9\u6bd4<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th><code>Hashtable<\/code><\/th><th><code>HashMap<\/code><\/th><\/tr><\/thead><tbody><tr><td>\u662f\u5426\u7ebf\u7a0b\u5b89\u5168<\/td><td>\u662f<\/td><td>\u5426\uff08\u9700\u624b\u52a8\u540c\u6b65\uff09<\/td><\/tr><tr><td>\u662f\u5426\u5141\u8bb8 null key<\/td><td>\u4e0d\u5141\u8bb8<\/td><td>\u5141\u8bb8\u4e00\u4e2a null key<\/td><\/tr><tr><td>\u662f\u5426\u5141\u8bb8 null value<\/td><td>\u4e0d\u5141\u8bb8<\/td><td>\u5141\u8bb8\u591a\u4e2a null value<\/td><\/tr><tr><td>\u9002\u7528\u573a\u666f<\/td><td>\u8001\u65e7\u7ebf\u7a0b\u5b89\u5168\u9700\u6c42<\/td><td>\u901a\u7528 + \u9ad8\u6027\u80fd\u9700\u6c42<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\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><code>Hashtable<\/code> \u51fa\u4e8e<strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\u548c<strong>\u8bed\u4e49\u6e05\u6670<\/strong>\u7684\u8003\u8651\uff0c\u4e0d\u5141\u8bb8 key \u6216 value \u4e3a null\u3002\u63a8\u8350\u7528 <code>ConcurrentHashMap<\/code> \u66ff\u4ee3\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u63a8\u8350\u66ff\u4ee3\u65b9\u6848<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u66ff\u4ee3\u65b9\u6848<\/th><\/tr><\/thead><tbody><tr><td>\u591a\u7ebf\u7a0b\u5b89\u5168 Map<\/td><td><code>ConcurrentHashMap<\/code><\/td><\/tr><tr><td>\u5355\u7ebf\u7a0b\u6216\u81ea\u5df1\u63a7\u5236\u5e76\u53d1<\/td><td><code>HashMap<\/code><\/td><\/tr><tr><td>\u5141\u8bb8 null \u4e14\u9700\u7ebf\u7a0b\u5b89\u5168<\/td><td><code>Collections.synchronizedMap()<\/code> \u5305\u88c5 HashMap<\/td><\/tr><\/tbody><\/table><\/figure>\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>Hashtable \u5b9e\u9645\u4e0a \u4e0d\u652f\u6301 null \u7684 key \u548c value\uff0c\u8fd9\u662f\u4e00\u79cd\u5e38\u89c1\u8bef\u89e3\u3002 \u4e00\u53e5\u8bdd\u56de\u7b54\uff1a H [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4009,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68,86],"tags":[],"class_list":["post-4006","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\/4006","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=4006"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/4006\/revisions"}],"predecessor-version":[{"id":4010,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/4006\/revisions\/4010"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/4009"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=4006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=4006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=4006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}