{"id":4284,"date":"2023-07-19T13:54:32","date_gmt":"2023-07-19T05:54:32","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4284"},"modified":"2023-07-19T14:17:09","modified_gmt":"2023-07-19T06:17:09","slug":"hashmap%e7%ae%80%e5%8d%95%e8%ae%b0%e5%bd%952","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4284","title":{"rendered":"hashmap\u7b80\u5355\u8bb0\u5f552"},"content":{"rendered":"\n<p>hashmap\u7684key\u53ea\u80fd\u662fstring\u5417?<\/p>\n\n\n\n<p>\u5b9e\u9645\u4e0a\uff0cJava\u7684HashMap\u7684\u952e\uff08key\uff09\u5e76\u4e0d\u4ec5\u9650\u4e8e<code>String<\/code>\u7c7b\u578b\uff0c\u5b83\u53ef\u4ee5\u662f\u4efb\u4f55\u5bf9\u8c61\u3002\u5b83\u8981\u6c42\u7684\u662f\u952e\u5bf9\u8c61\u5fc5\u987b\u6b63\u786e\u5730\u5b9e\u73b0\u4e86<code>equals()<\/code>\u548c<code>hashCode()<\/code>\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u5177\u4f53\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u4e00\u4e2a\u5bf9\u8c61\u7528\u4f5c<code>HashMap<\/code>\u7684\u952e\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u4ee5\u4e0b\u4e24\u4e2a\u8981\u70b9\uff1a<\/p>\n\n\n\n<ol>\n<li><code>equals()<\/code>\u65b9\u6cd5\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u4f60\u5c06\u4e00\u4e2a\u5bf9\u8c61\u4f5c\u4e3a\u952e\u5b58\u50a8\u5728<code>HashMap<\/code>\u4e2d\uff0c\u7136\u540e\u4f7f\u7528\u53e6\u4e00\u4e2a\u76f8\u7b49\u7684\u5bf9\u8c61\uff08\u6309<code>equals()<\/code>\u65b9\u6cd5\u5224\u65ad\uff09\u6765\u83b7\u53d6\u503c\uff0c\u4f60\u5e94\u8be5\u5f97\u5230\u540c\u6837\u7684\u503c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u6309<code>equals()<\/code>\u65b9\u6cd5\u5224\u65ad\u76f8\u7b49\uff0c\u90a3\u4e48\u5bf9\u4e8e\u540c\u4e00\u4e2a<code>HashMap<\/code>\uff0c\u5b83\u4eec\u5e94\u8be5\u6709\u76f8\u540c\u7684\u503c\u3002<\/li>\n\n\n\n<li><code>hashCode()<\/code>\u65b9\u6cd5\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u54c8\u5e0c\u7801\uff0c\u8fd9\u4e2a\u54c8\u5e0c\u7801\u88ab\u7528\u4e8e\u786e\u5b9a\u5bf9\u8c61\u5728<code>HashMap<\/code>\u4e2d\u7684\u5b58\u50a8\u4f4d\u7f6e\u3002\u4e3a\u4e86\u4f7f<code>HashMap<\/code>\u7684\u6027\u80fd\u6700\u4f18\u5316\uff0c\u76f8\u7b49\u7684\u5bf9\u8c61\uff08\u6309<code>equals()<\/code>\u65b9\u6cd5\u5224\u65ad\uff09\u5e94\u8be5\u6709\u76f8\u540c\u7684\u54c8\u5e0c\u7801\u3002<\/li>\n<\/ol>\n\n\n\n<p><code>String<\/code>\u7c7b\u5df2\u7ecf\u5b9e\u73b0\u4e86<code>equals()<\/code>\u548c<code>hashCode()<\/code>\u65b9\u6cd5\uff0c\u8fd9\u4f7f\u5f97\u5b83\u5f88\u9002\u5408\u4f5c\u4e3a<code>HashMap<\/code>\u7684\u952e\u3002\u4f46\u662f\uff0c\u5176\u4ed6\u7c7b\uff0c\u53ea\u8981\u6b63\u786e\u5730\u5b9e\u73b0\u4e86\u8fd9\u4e24\u4e2a\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u4f5c\u4e3a<code>HashMap<\/code>\u7684\u952e\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>hashmap key\u53ef\u4ee5\u4e3anull\u5417\uff1f<\/p>\n\n\n\n<p>\u662f\u7684\uff0cJava\u7684HashMap\u5141\u8bb8\u4f7f\u7528null\u4f5c\u4e3a\u952e\u503c\uff08key\uff09\u3002HashMap\u4e2d\u53ef\u4ee5\u6709\u4e00\u4e2a\u952e\u4e3anull\u7684\u952e\u503c\u5bf9\uff08key-value pair\uff09\u3002\u5f53\u4f60\u8bd5\u56fe\u901a\u8fc7null\u952e\u6765\u83b7\u53d6\u503c\uff0cHashMap\u4f1a\u76f4\u63a5\u8fd4\u56de\u4e0enull\u952e\u5173\u8054\u7684\u503c\uff0c\u800c\u4e0d\u662f\u901a\u8fc7\u54c8\u5e0c\u7b97\u6cd5\u6765\u67e5\u627e\u3002\u8fd9\u662fHashMap\u4e0eHashtable\u7684\u4e00\u4e2a\u91cd\u8981\u533a\u522b\uff0c\u56e0\u4e3aHashtable\u4e0d\u5141\u8bb8null\u952e\u6216null\u503c\u3002<\/p>\n\n\n\n<p>\u8981\u6ce8\u610f\u7684\u662f\uff0c\u867d\u7136HashMap\u5141\u8bb8null\u952e\uff0c\u4f46\u5e76\u4e0d\u662f\u6240\u6709\u7684Map\u5b9e\u73b0\u90fd\u5141\u8bb8null\u952e\u3002\u4f8b\u5982\uff0cTreeMap\u5c31\u4e0d\u5141\u8bb8null\u952e\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u8c03\u7528\u952e\u7684compareTo()\u6216compare()\u65b9\u6cd5\u6765\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5c3d\u7ba1HashMap\u5141\u8bb8null\u952e\uff0c\u4f46\u662f\u5426\u5e94\u8be5\u4f7f\u7528null\u952e\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u5e94\u7528\u3002\u5728\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u907f\u514d\u4f7f\u7528null\u952e\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u6216\u8005\u5728\u5904\u7406Map\u65f6\u7684NullPointerException\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>hashmap\u7684key\u53ea\u80fd\u662fstring\u5417? \u5b9e\u9645\u4e0a\uff0cJava\u7684HashMap\u7684\u952e\uff08key\uff09\u5e76\u4e0d\u4ec5\u9650\u4e8eStri [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[46],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4284"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4284"}],"version-history":[{"count":7,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4284\/revisions"}],"predecessor-version":[{"id":4291,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4284\/revisions\/4291"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}