{"id":217,"date":"2020-11-11T23:53:55","date_gmt":"2020-11-11T15:53:55","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=217"},"modified":"2021-10-13T09:55:00","modified_gmt":"2021-10-13T01:55:00","slug":"es262-map%e7%9a%84%e7%ae%80%e5%8d%95%e7%90%86%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2020\/11\/11\/es262-map%e7%9a%84%e7%ae%80%e5%8d%95%e7%90%86%e8%a7%a3\/","title":{"rendered":"ES262 Map\u7684\u7b80\u5355\u7406\u89e3"},"content":{"rendered":"<hr \/>\n<p>\u65b0\u7248ES262\u7684Map\u5e94\u8be5\u548cHasMap\u7c7b\u4f3c\uff0c\u7406\u89e3HashMap\u57fa\u672c\u5c31\u80fd\u7406\u89e3Map\uff0c\u4ee5\u4e0b\u6240\u6709\u4ee3\u7801\u5747\u4e3a\u903b\u8f91\u4ee3\u7801\u3002<\/p>\n<h3>Map\u6570\u636e\u7ed3\u6784<\/h3>\n<p>\u6570\u636e\u7ed3\u6784\u4e2d\u6709\u6570\u7ec4\u548c\u94fe\u8868\u6765\u5b9e\u73b0\u5bf9\u6570\u636e\u7684\u5b58\u50a8\uff0c\u4f46\u8fd9\u4e24\u8005\u57fa\u672c\u4e0a\u662f\u4e24\u4e2a\u6781\u7aef\u3002<\/p>\n<h4>\u6570\u7ec4\u65b9\u5f0f<\/h4>\n<p>\u6570\u7ec4\u5b58\u50a8\u533a\u95f4\u662f\u8fde\u7eed\u7684\uff0c\u5360\u7528\u5185\u5b58\u4e25\u91cd\uff0c\u6545\u7a7a\u95f4\u590d\u6742\u7684\u5f88\u5927\u3002\u4f46\u6570\u7ec4\u7684\u4e8c\u5206\u67e5\u627e\u65f6\u95f4\u590d\u6742\u5ea6\u5c0f\uff0c\u4e3aO(1)\uff1b\u6570\u7ec4\u7684\u7279\u70b9\u662f\uff1a\u5bfb\u5740\u5bb9\u6613\uff0c\u63d2\u5165\u548c\u5220\u9664\u56f0\u96be\uff1b<\/p>\n<h4>\u94fe\u8868\u65b9\u5f0f<\/h4>\n<p>\u94fe\u8868\u5b58\u50a8\u533a\u95f4\u79bb\u6563\uff0c\u5360\u7528\u5185\u5b58\u6bd4\u8f83\u5bbd\u677e\uff0c\u6545\u7a7a\u95f4\u590d\u6742\u5ea6\u5f88\u5c0f\uff0c\u4f46\u65f6\u95f4\u590d\u6742\u5ea6\u5f88\u5927\uff0c\u8fbeO\uff08N\uff09\u3002\u94fe\u8868\u7684\u7279\u70b9\u662f\uff1a\u5bfb\u5740\u56f0\u96be\uff0c\u63d2\u5165\u548c\u5220\u9664\u5bb9\u6613\u3002<\/p>\n<h4>\u54c8\u5e0c\u8868\u65b9\u5f0f<\/h4>\n<p>\u76f4\u63a5\u7efc\u5408\u4e24\u8005\u7684\u7279\u6027\uff0c\u505a\u51fa\u4e00\u79cd\u5bfb\u5740\u5bb9\u6613\uff0c\u63d2\u5165\u5220\u9664\u4e5f\u5bb9\u6613\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5c31\u662f\u54c8\u5e0c\u8868\u3002\u54c8\u5e0c\u8868\uff08(Hash table\uff09\u65e2\u6ee1\u8db3\u4e86\u6570\u636e\u7684\u67e5\u627e\u65b9\u4fbf\uff0c\u540c\u65f6\u4e0d\u5360\u7528\u592a\u591a\u7684\u5185\u5bb9\u7a7a\u95f4\uff0c\u4f7f\u7528\u4e5f\u5341\u5206\u65b9\u4fbf\u3002<\/p>\n<p>\u54c8\u5e0c\u8868\u6709\u591a\u79cd\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u6cd5\u2014\u2014 \u62c9\u94fe\u6cd5\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u201c\u94fe\u8868\u7684\u6570\u7ec4\u201d \uff0c\u5982\u56fe\uff1a<\/p>\n<p><img src=\"https:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/Es262Map1.png\" alt=\"ES262 Map\u7684\u7b80\u5355\u7406\u89e3\" \/><\/p>\n<p>\u56e0\u6b64\uff0c\u54c8\u5e0c\u8868\u662f\u7531\u6570\u7ec4+\u94fe\u8868\u7ec4\u6210\u7684\uff0c\u4e00\u4e2a\u957f\u5ea6\u4e3a16\u7684\u6570\u7ec4\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u5b58\u50a8\u7684\u662f\u4e00\u4e2a\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u3002\u90a3\u4e48\u8fd9\u4e9b\u5143\u7d20\u662f\u6309\u7167\u4ec0\u4e48\u6837\u7684\u89c4\u5219\u5b58\u50a8\u5230\u6570\u7ec4\u4e2d\u5462\u3002\u4e00\u822c\u60c5\u51b5\u662f\u901a\u8fc7hash(key)%len\u83b7\u5f97\uff0c\u4e5f\u5c31\u662f\u5143\u7d20\u7684key\u7684\u54c8\u5e0c\u503c\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\u5f97\u5230\u3002\u6bd4\u5982\u4e0a\u8ff0\u54c8\u5e0c\u8868\u4e2d\uff0c12%16=12,28%16=12,108%16=12,140%16=12\u3002\u6240\u4ee512\u300128\u3001108\u4ee5\u53ca140\u90fd\u5b58\u50a8\u5728\u6570\u7ec4\u4e0b\u6807\u4e3a12\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>HashMap\u5176\u5b9e\u4e5f\u662f\u4e00\u4e2a\u7ebf\u6027\u7684\u6570\u7ec4\u5b9e\u73b0\u7684,\u6240\u4ee5\u53ef\u4ee5\u7406\u89e3\u4e3a\u5176\u5b58\u50a8\u6570\u636e\u7684\u5bb9\u5668\u5c31\u662f\u4e00\u4e2a\u7ebf\u6027\u6570\u7ec4\u3002\u8fd9\u53ef\u80fd\u8ba9\u6211\u4eec\u5f88\u4e0d\u89e3\uff0c\u4e00\u4e2a\u7ebf\u6027\u7684\u6570\u7ec4\u600e\u4e48\u5b9e\u73b0\u6309\u952e\u503c\u5bf9\u6765\u5b58\u53d6\u6570\u636e\u5462\uff1f\u8fd9\u91ccHashMap\u6709\u505a\u4e00\u4e9b\u5904\u7406\u3002<\/p>\n<p>\u9996\u5148HashMap\u91cc\u9762\u5b9e\u73b0\u4e00\u4e2a\u9759\u6001\u5185\u90e8\u7c7bEntry\uff0c\u5176\u91cd\u8981\u7684\u5c5e\u6027\u6709 key , value, next\uff0c\u4ece\u5c5e\u6027key,value\u6211\u4eec\u5c31\u80fd\u5f88\u660e\u663e\u7684\u770b\u51fa\u6765Entry\u5c31\u662fHashMap\u952e\u503c\u5bf9\u5b9e\u73b0\u7684\u4e00\u4e2a\u57fa\u7840bean\uff0c\u6211\u4eec\u4e0a\u9762\u8bf4\u5230HashMap\u7684\u57fa\u7840\u5c31\u662f\u4e00\u4e2a\u7ebf\u6027\u6570\u7ec4\uff0c\u8fd9\u4e2a\u6570\u7ec4\u5c31\u662fEntry[]\uff0cMap\u91cc\u9762\u7684\u5185\u5bb9\u90fd\u4fdd\u5b58\u5728Entry[]\u91cc\u9762\u3002<\/p>\n<h3>\u5b9e\u73b0\u539f\u7406<\/h3>\n<p>\u65e2\u7136\u662f\u7ebf\u6027\u6570\u7ec4\uff0c\u4e3a\u4ec0\u4e48\u80fd\u968f\u673a\u5b58\u53d6\uff1f\u8fd9\u91ccMap\u7528\u4e86\u7b97\u6cd5\uff0c\u5927\u81f4\u662f\u8fd9\u6837\u5b9e\u73b0\uff1a<\/p>\n<p>\u903b\u8f91\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">\/\/ \u5b58\u50a8\u65f6:\nlet hash:int = key.hashCode();\nlet index:int = hash % Entry[].length;\nEntry[index] = value;\n\n\/\/ \u53d6\u503c\u65f6:\nlet hash:int = key.hashCode();\nlet index:int = hash % Entry[].length;\nreturn Entry[index];\n<\/code><\/pre>\n<h4>put\u65b9\u6cd5<\/h4>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u95ee\u9898\uff1a\u5982\u679c\u4e24\u4e2akey\u901a\u8fc7hash%Entry[].length\u5f97\u5230\u7684index\u76f8\u540c\uff0c\u4f1a\u4e0d\u4f1a\u88ab\u8986\u76d6\uff1f<\/p>\n<p>\u3000\u3000\u8fd9\u91ccHashMap\u91cc\u9762\u7528\u5230\u94fe\u5f0f\u6570\u636e\u7ed3\u6784\u7684\u4e00\u4e2a\u6982\u5ff5\u3002\u4e0a\u9762\u6211\u4eec\u63d0\u5230\u8fc7Entry\u7c7b\u91cc\u9762\u6709\u4e00\u4e2anext\u5c5e\u6027\uff0c\u4f5c\u7528\u662f\u6307\u5411\u4e0b\u4e00\u4e2aEntry\u3002\u6253\u4e2a\u6bd4\u65b9\uff0c \u7b2c\u4e00\u4e2a\u952e\u503c\u5bf9A\u8fdb\u6765\uff0c\u901a\u8fc7\u8ba1\u7b97\u5176key\u7684hash\u5f97\u5230\u7684index=0\uff0c\u8bb0\u505a:Entry[0] = A\u3002\u4e00\u4f1a\u540e\u53c8\u8fdb\u6765\u4e00\u4e2a\u952e\u503c\u5bf9B\uff0c\u901a\u8fc7\u8ba1\u7b97\u5176index\u4e5f\u7b49\u4e8e0\uff0c\u73b0\u5728\u600e\u4e48\u529e\uff1fHashMap\u4f1a\u8fd9\u6837\u505a:B.next = A,Entry[0] = B,\u5982\u679c\u53c8\u8fdb\u6765C,index\u4e5f\u7b49\u4e8e0,\u90a3\u4e48C.next = B,Entry[0] = C\uff1b\u8fd9\u6837\u6211\u4eec\u53d1\u73b0index=0\u7684\u5730\u65b9\u5176\u5b9e\u5b58\u53d6\u4e86A,B,C\u4e09\u4e2a\u952e\u503c\u5bf9,\u4ed6\u4eec\u901a\u8fc7next\u8fd9\u4e2a\u5c5e\u6027\u94fe\u63a5\u5728\u4e00\u8d77\u3002\u6240\u4ee5\u7591\u95ee\u4e0d\u7528\u62c5\u5fc3\u3002\u4e5f\u5c31\u662f\u8bf4\u6570\u7ec4\u4e2d\u5b58\u50a8\u7684\u662f\u6700\u540e\u63d2\u5165\u7684\u5143\u7d20\u3002\u5230\u8fd9\u91cc\u4e3a\u6b62\uff0cHashMap\u7684\u5927\u81f4\u5b9e\u73b0\uff0c\u6211\u4eec\u5e94\u8be5\u5df2\u7ecf\u6e05\u695a\u4e86\u3002<\/p>\n<pre><code class=\"language-javascript line-numbers\">public put(key: K, value: V): V {\n    if (key == null)\n        return putForNullKey(value);\n    let hash: int = hash(key.hashCode());\n    let i: int = indexFor(hash, table.length);\n    for (let e: Entry = table[i]; e != null; e = e.next) {\n        let k: Object;\n        if (e.hash == hash &amp;&amp; ((k = e.key) == key || key.equals(k))) {\n            let oldValue: V = e.value;\n            e.value = value;\n            e.recordAccess(this);\n            return oldValue;\n        }\n    }\n\n    modCount++;\n    addEntry(hash, key, value, i);\n    return null;\n}\n\nprivate addEntry(hash: int, key: K, value: V, bucketIndex: int): void {\n    let e: Entry = table[bucketIndex];\n    table[bucketIndex] = new Entry(hash, key, value, e);\n    if (size++ &gt;= threshold)\n        resize(2 * table.length);\n}\n<\/code><\/pre>\n<p>\u5f53\u7136HashMap\u91cc\u9762\u4e5f\u5305\u542b\u4e00\u4e9b\u4f18\u5316\u65b9\u9762\u7684\u5b9e\u73b0\uff0c\u8fd9\u91cc\u4e5f\u8bf4\u4e00\u4e0b\u3002\u6bd4\u5982\uff1aEntry[]\u7684\u957f\u5ea6\u4e00\u5b9a\u540e\uff0c\u968f\u7740map\u91cc\u9762\u6570\u636e\u7684\u8d8a\u6765\u8d8a\u957f\uff0c\u8fd9\u6837\u540c\u4e00\u4e2aindex\u7684\u94fe\u5c31\u4f1a\u5f88\u957f\uff0c\u4f1a\u4e0d\u4f1a\u5f71\u54cd\u6027\u80fd\uff1fHashMap\u91cc\u9762\u8bbe\u7f6e\u4e00\u4e2a\u56e0\u5b50\uff0c\u968f\u7740map\u7684size\u8d8a\u6765\u8d8a\u5927\uff0cEntry[]\u4f1a\u4ee5\u4e00\u5b9a\u7684\u89c4\u5219\u52a0\u957f\u957f\u5ea6\u3002<\/p>\n<h4>get\u65b9\u6cd5<\/h4>\n<pre><code class=\"language-javascript line-numbers\">public get(Object key): V {\n    if (key == null)\n        return getForNullKey();\n    let hash: int = hash(key.hashCode());\n    \/\/\u5148\u5b9a\u4f4d\u5230\u6570\u7ec4\u5143\u7d20\uff0c\u518d\u904d\u5386\u8be5\u5143\u7d20\u5904\u7684\u94fe\u8868\n    for (let e: Entry = table[indexFor(hash, table.length)]; e != null; e = e.next) {\n        let k: Object;\n        if (e.hash == hash &amp;&amp; ((k = e.key) == key || key.equals(k)))\n            return e.value;\n    }\n    return null;\n}\n<\/code><\/pre>\n<h4>\u952enull\u7684\u5b58\u53d6<\/h4>\n<p>null key\u603b\u662f\u5b58\u653e\u5728Entry[]\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<pre><code class=\"language-javascript line-numbers\">private putForNullKey(value: V): V {\n    for (let e: Entry = table[0]; e != null; e = e.next) {\n        if (e.key == null) {\n            let oldValue: Entry = e.value;\n            e.value = value;\n            e.recordAccess(this);\n            return oldValue;\n        }\n    }\n    modCount++;\n    addEntry(0, null, value, 0);\n    return null;\n}\n\nprivate getForNullKey(): V {\n    for (let e: Entry = table[0]; e != null; e = e.next) {\n        if (e.key == null)\n            return e.value;\n    }\n    return null;\n}\n<\/code><\/pre>\n<h4>\u53d6\u6a21<\/h4>\n<pre><code class=\"language-javascript line-numbers\">static indexFor(h: int, length: int): int {\n    return h &amp; (length - 1);\n}\n<\/code><\/pre>\n<p>\u6309\u4f4d\u53d6\u5e76\uff0c\u4f5c\u7528\u4e0a\u76f8\u5f53\u4e8e\u53d6\u6a21mod\u6216\u8005\u53d6\u4f59%\u3002<br \/>\n\u8fd9\u610f\u5473\u7740\u6570\u7ec4\u4e0b\u6807\u76f8\u540c\uff0c\u5e76\u4e0d\u8868\u793ahashCode\u76f8\u540c\u3002<\/p>\n<h4>table\u521d\u59cb\u5927\u5c0f<\/h4>\n<pre><code class=\"language-javascript line-numbers\">public HashMap(initialCapacity: int, loadFactor: float) {\n    \/\/.....\n\n    let capacity: int = 1;\n    while (capacity &lt; initialCapacity)\n        capacity &lt;&lt;= 1;\n\n    this.loadFactor = loadFactor;\n    threshold = capacity * loadFactor;\n    table = new Entry[capacity];\n    init();\n}\n<\/code><\/pre>\n<p>table\u521d\u59cb\u5927\u5c0f\u5e76\u4e0d\u662f\u6784\u9020\u51fd\u6570\u4e2d\u7684initialCapacity\u3002\u800c\u662f >= initialCapacity\u76842\u7684n\u6b21\u5e42\uff01\uff01\uff01\uff01<\/p>\n<h3>\u89e3\u51b3\u51b2\u7a81<\/h3>\n<p>\u53ea\u9700\u8981\u9075\u5faa\u6b65\u9aa4\uff1a<\/p>\n<blockquote>\n<ul>\n<li>\u5f00\u653e\u5b9a\u5740\u6cd5\uff08\u7ebf\u6027\u63a2\u6d4b\u518d\u6563\u5217\uff0c\u4e8c\u6b21\u63a2\u6d4b\u518d\u6563\u5217\uff0c\u4f2a\u968f\u673a\u63a2\u6d4b\u518d\u6563\u5217\uff09<\/p>\n<\/li>\n<li>\n<p>\u518d\u54c8\u5e0c\u6cd5<\/p>\n<\/li>\n<li>\n<p>\u94fe\u5730\u5740\u6cd5<\/p>\n<\/li>\n<li>\n<p>\u5efa\u7acb\u4e00\u4e2a\u516c\u5171\u6ea2\u51fa\u533a<\/p>\n<\/li>\n<\/ul>\n<\/blockquote>\n<h3>\u6563\u5217rehash<\/h3>\n<p>\u5f53\u54c8\u5e0c\u8868\u7684\u5bb9\u91cf\u8d85\u8fc7\u9ed8\u8ba4\u5bb9\u91cf\u65f6\uff0c\u5fc5\u987b\u8c03\u6574table\u7684\u5927\u5c0f\u3002\u5f53\u5bb9\u91cf\u5df2\u7ecf\u8fbe\u5230\u6700\u5927\u53ef\u80fd\u503c\u65f6\uff0c\u90a3\u4e48\u8be5\u65b9\u6cd5\u5c31\u5c06\u5bb9\u91cf\u8c03\u6574\u5230Integer.MAX_VALUE\u8fd4\u56de\uff0c\u8fd9\u65f6\uff0c\u9700\u8981\u521b\u5efa\u4e00\u5f20\u65b0\u8868\uff0c\u5c06\u539f\u8868\u7684\u6620\u5c04\u5230\u65b0\u8868\u4e2d\u3002<\/p>\n<pre><code class=\"language-javascript line-numbers\">private resize(newCapacity: int): void {\n    let oldTable: Entry = table;\n    let oldCapacity: int = oldTable.length;\n    if (oldCapacity == MAXIMUM_CAPACITY) {\n        threshold = Integer.MAX_VALUE;\n        return;\n    }\n\n    let newTable: Entry = new Entry[newCapacity];\n    transfer(newTable);\n    table = newTable;\n    threshold = newCapacity * loadFactor;\n}\n\nprivate transfer(newTable: Entry): void {\n    let src: Entry = table;\n    newCapacity: int = newTable.length;\n    for (let j: int = 0; j &lt; src.length; j++) {\n        let e: Entry = src[j];\n        if (e != null) {\n            src[j] = null;\n            do {\n                let next: Entry = e.next;\n                \/\/\u91cd\u65b0\u8ba1\u7b97index\n                let i: int = indexFor(e.hash, newCapacity);\n                e.next = newTable[i];\n                newTable[i] = e;\n                e = next;\n            } while (e != null);\n        }\n    }\n}\n<\/code><\/pre>\n<p>code enjoy!\ud83d\ude1c\ud83d\ude1c\ud83d\ude1c<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/indeex.club\/\">https:\/\/indeex.club<\/a><\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u65b0\u7248ES262\u7684Map\u5e94\u8be5\u548cHasMap\u7c7b\u4f3c\uff0c\u7406\u89e3HashMap\u57fa\u672c\u5c31\u80fd\u7406\u89e3Map\uff0c\u4ee5\u4e0b\u6240\u6709\u4ee3\u7801\u5747\u4e3a<a href=\"https:\/\/blog.indeex.club\/index.php\/2020\/11\/11\/es262-map%e7%9a%84%e7%ae%80%e5%8d%95%e7%90%86%e8%a7%a3\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/217"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":218,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/217\/revisions\/218"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}