{"id":37402,"date":"2023-12-11T14:41:19","date_gmt":"2023-12-11T06:41:19","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=37402"},"modified":"2023-12-11T14:41:19","modified_gmt":"2023-12-11T06:41:19","slug":"hashmap-%e5%92%8c-hashtable-%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/hashmap-%e5%92%8c-hashtable-%e7%9a%84%e5%8c%ba%e5%88%ab\/","title":{"rendered":"HashMap \u548c Hashtable \u7684\u533a\u522b"},"content":{"rendered":"<p>&#8220;`&#8221;<\/p>\n<p>&lt;ol&gt;&lt;li&gt;&lt;strong&gt;\u7ebf\u7a0b\u662f\u5426\u5b89\u5168\uff1a&lt;\/strong&gt; HashMap \u662f\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\uff0cHashTable \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff1bHashTable \u5185\u90e8\u7684\u65b9\u6cd5\u57fa\u672c\u90fd\u7ecf\u8fc7&lt;code&gt;synchronized&lt;\/code&gt; \u4fee\u9970\u3002\uff08\u5982\u679c\u4f60\u8981\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u7684\u8bdd\u5c31\u4f7f\u7528 ConcurrentHashMap \u5427\uff01\uff09\uff1b&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;\u6548\u7387\uff1a&lt;\/strong&gt; \u56e0\u4e3a\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\uff0cHashMap \u8981\u6bd4 HashTable \u6548\u7387\u9ad8\u4e00\u70b9\u3002\u53e6\u5916\uff0cHashTable \u57fa\u672c\u88ab\u6dd8\u6c70\uff0c\u4e0d\u8981\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b83\uff1b&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;\u5bf9Null key \u548cNull value\u7684\u652f\u6301\uff1a&lt;\/strong&gt; HashMap \u4e2d\uff0cnull \u53ef\u4ee5\u4f5c\u4e3a\u952e\uff0c\u8fd9\u6837\u7684\u952e\u53ea\u6709\u4e00\u4e2a\uff0c\u53ef\u4ee5\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u952e\u6240\u5bf9\u5e94\u7684\u503c\u4e3a null\u3002\u3002\u4f46\u662f\u5728 HashTable \u4e2d put \u8fdb\u7684\u952e\u503c\u53ea\u8981\u6709\u4e00\u4e2a null\uff0c\u76f4\u63a5\u629b\u51fa NullPointerException\u3002&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;\u521d\u59cb\u5bb9\u91cf\u5927\u5c0f\u548c\u6bcf\u6b21\u6269\u5145\u5bb9\u91cf\u5927\u5c0f\u7684\u4e0d\u540c \uff1a&lt;\/strong&gt; \u2460\u521b\u5efa\u65f6\u5982\u679c\u4e0d\u6307\u5b9a\u5bb9\u91cf\u521d\u59cb\u503c\uff0cHashtable \u9ed8\u8ba4\u7684\u521d\u59cb\u5927\u5c0f\u4e3a11\uff0c\u4e4b\u540e\u6bcf\u6b21\u6269\u5145\uff0c\u5bb9\u91cf\u53d8\u4e3a\u539f\u6765\u76842n+1\u3002HashMap \u9ed8\u8ba4\u7684\u521d\u59cb\u5316\u5927\u5c0f\u4e3a16\u3002\u4e4b\u540e\u6bcf\u6b21\u6269\u5145\uff0c\u5bb9\u91cf\u53d8\u4e3a\u539f\u6765\u76842\u500d\u3002\u2461\u521b\u5efa\u65f6\u5982\u679c\u7ed9\u5b9a\u4e86\u5bb9\u91cf\u521d\u59cb\u503c\uff0c\u90a3\u4e48 Hashtable \u4f1a\u76f4\u63a5\u4f7f\u7528\u4f60\u7ed9\u5b9a\u7684\u5927\u5c0f\uff0c\u800c HashMap \u4f1a\u5c06\u5176\u6269\u5145\u4e3a2\u7684\u5e42\u6b21\u65b9\u5927\u5c0f\uff08HashMap \u4e2d\u7684&lt;code&gt;tableSizeFor()&lt;\/code&gt;\u65b9\u6cd5\u4fdd\u8bc1\uff0c\u4e0b\u9762\u7ed9\u51fa\u4e86\u6e90\u4ee3\u7801\uff09\u3002\u4e5f\u5c31\u662f\u8bf4 HashMap \u603b\u662f\u4f7f\u75282\u7684\u5e42\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684\u5927\u5c0f,\u540e\u9762\u4f1a\u4ecb\u7ecd\u5230\u4e3a\u4ec0\u4e48\u662f2\u7684\u5e42\u6b21\u65b9\u3002&lt;\/li&gt;&lt;li&gt;&lt;strong&gt;\u5e95\u5c42\u6570\u636e\u7ed3\u6784\uff1a&lt;\/strong&gt; JDK1.8 \u4ee5\u540e\u7684 HashMap \u5728\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u65f6\u6709\u4e86\u8f83\u5927\u7684\u53d8\u5316\uff0c\u5f53\u94fe\u8868\u957f\u5ea6\u5927\u4e8e\u9608\u503c\uff08\u9ed8\u8ba4\u4e3a8\uff09\u65f6\uff0c\u5c06\u94fe\u8868\u8f6c\u5316\u4e3a\u7ea2\u9ed1\u6811\uff0c\u4ee5\u51cf\u5c11\u641c\u7d22\u65f6\u95f4\u3002Hashtable \u6ca1\u6709\u8fd9\u6837\u7684\u673a\u5236\u3002&lt;\/li&gt;&lt;\/ol&gt;<\/p>\n<p>&lt;strong&gt;HashMap \u4e2d\u5e26\u6709\u521d\u59cb\u5bb9\u91cf\u7684\u6784\u9020\u51fd\u6570\uff1a&lt;\/strong&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;java    public HashMap(int initialCapacity, float loadFactor) {        if (initialCapacity &lt; 0)            throw new IllegalArgumentException(&quot;&quot;Illegal initial capacity: &quot;&quot; +                                               initialCapacity);        if (initialCapacity &gt; MAXIMUM_CAPACITY)            initialCapacity = MAXIMUM_CAPACITY;        if (loadFactor &lt;= 0 || Float.isNaN(loadFactor))            throw new IllegalArgumentException(&quot;&quot;Illegal load factor: &quot;&quot; +                                               loadFactor);        this.loadFactor = loadFactor;        this.threshold = tableSizeFor(initialCapacity);    }     public HashMap(int initialCapacity) {        this(initialCapacity, DEFAULT_LOAD_FACTOR);    }&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u65b9\u6cd5\u4fdd\u8bc1\u4e86 HashMap \u603b\u662f\u4f7f\u75282\u7684\u5e42\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684\u5927\u5c0f\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;java    \/**     * Returns a power of two size for the given target capacity.     *\/    static final int tableSizeFor(int cap) {        int n = cap &#8211; 1;        n |= n &gt;&gt;&gt; 1;        n |= n &gt;&gt;&gt; 2;        n |= n &gt;&gt;&gt; 4;        n |= n &gt;&gt;&gt; 8;        n |= n &gt;&gt;&gt; 16;        return (n &lt; 0) ? 1 : (n &gt;= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;    }&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; &lt;ol&gt;&lt;li&gt;&lt;strong&gt;\u7ebf\u7a0b\u662f\u5426\u5b89 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-37402","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=37402"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37402\/revisions"}],"predecessor-version":[{"id":37403,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37402\/revisions\/37403"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=37402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=37402"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=37402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}