{"id":1245,"date":"2022-10-27T23:08:36","date_gmt":"2022-10-27T15:08:36","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1245"},"modified":"2022-11-27T12:39:47","modified_gmt":"2022-11-27T04:39:47","slug":"hashmap%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90jdk11","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1245","title":{"rendered":"HashMap\u6e90\u7801\u89e3\u6790(JDK11)"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">HashMap\u6e90\u7801\u89e3\u6790(JDK11)<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#%E6%A6%82%E8%A7%88\"><\/a>\u6982\u89c8<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/camo.githubusercontent.com\/d746de553ecfbb18ef9a95e1be3993d111d3a1423997e57780a72a6d3d7031ec\/68747470733a2f2f626c6f672d313330303636333132372e636f732e61702d7368616e676861692e6d7971636c6f75642e636f6d2f4261636b456e645f4e6f7465732f486173684d61702e706e67\" target=\"_blank\" rel=\"noreferrer noopener\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/camo.githubusercontent.com\/d746de553ecfbb18ef9a95e1be3993d111d3a1423997e57780a72a6d3d7031ec\/68747470733a2f2f626c6f672d313330303636333132372e636f732e61702d7368616e676861692e6d7971636c6f75642e636f6d2f4261636b456e645f4e6f7465732f486173684d61702e706e67'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/camo.githubusercontent.com\/d746de553ecfbb18ef9a95e1be3993d111d3a1423997e57780a72a6d3d7031ec\/68747470733a2f2f626c6f672d313330303636333132372e636f732e61702d7368616e676861692e6d7971636c6f75642e636f6d2f4261636b456e645f4e6f7465732f486173684d61702e706e67\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"HashMap\"\/><\/div><\/a><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>HashMap \u6839\u636e key \u7684 hashCode \u503c\u8fdb\u884c\u50a8\u5b58\u6570\u636e\uff0c\u5927\u591a\u6570\u60c5\u51b5\u559c\u7231\u53ef\u4ee5\u76f4\u63a5\u5b9a\u4f4d\u5230\u4ed6\u7684\u503c\uff0c\u56e0\u6b64\u6709\u5f88\u5feb\u7684\u8bbf\u95ee\u901f\u5ea6<\/li><li>\u904d\u5386\u7684\u987a\u5e8f\u4e0d\u786e\u5b9a\uff0c\u5e76\u4e14\u4e0d\u80fd\u4fdd\u8bc1\u987a\u5e8f\u4e0d\u6539\u53d8<\/li><li>\u6269\u5bb9\u662f\u4e00\u4e2a\u7279\u522b\u8017\u6027\u80fd\u7684\u64cd\u4f5c\uff0c\u6240\u4ee5 \u5728\u4f7f\u7528\u65f6\u53ef\u4ee5\u5148\u7ed9\u4e00\u4e2a\u5927\u81f4\u7684\u5bb9\u91cf<\/li><li>\u5141\u8bb8\u4e00\u6761\u8bb0\u5f55\u7684 key \u4e3a null\uff0c\u4e14\u5141\u8bb8\u591a\u6761\u8bb0\u5f55\u7684 value \u4e3a null<\/li><li>\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff0c\u5373\u540c\u4e00\u65f6\u523b\u6709\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5199 HashMap\uff0c\u5bf9HashMap \u9020\u6210\u7ed3\u6784\u6027\u53d8\u5316\uff08A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.\uff09\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e0b\u65b9\u6cd5\u83b7\u5f97\u540c\u6b65\u65b9\u6cd5\uff1aMap m = Collections.synchronizedMap(new HashMap(&#8230;));\u6216\u8005\u4f7f\u7528 ConcurrentHashMap<\/li><li>\u63d0\u4f9b\u4e86\u8017\u65f6 constant-time \u7684\u57fa\u672c\u64cd\u4f5c\uff08get\uff0c put\uff09<\/li><li>Iteration over collection views requires time proportional to the &#8220;capacity&#8221; of the&nbsp;<code>HashMap<\/code>&nbsp;instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it&#8217;s very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.<\/li><li>As a general rule, the default load factor (0.75) offers a good tradeoff between time and space costs.<\/li><li>Note that using many keys with the same&nbsp;<code>hashCode()<\/code>&nbsp;is a sure way to slow down performance of any hash table. To ameliorate impact, when keys are&nbsp;<a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/14\/docs\/api\/java.base\/java\/lang\/Comparable.html\"><code>Comparable<\/code><\/a>, this class may use comparison order among keys to help break ties.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#%E7%B1%BB%E5%90%8D\"><\/a>\u7c7b\u540d<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">public class HashMap&lt;K,V&gt; extends AbstractMap&lt;K,V&gt;\n    implements Map&lt;K,V&gt;, Cloneable, Serializable {<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#%E5%8F%82%E6%95%B0\"><\/a>\u53c2\u6570<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">private static final long serialVersionUID = 362498820763181265L;\n\n\/**\n * The default initial capacity - MUST be a power of two.\n *\/\nstatic final int DEFAULT_INITIAL_CAPACITY = 1 &lt;&lt; 4; \/\/ aka 16\n\n\/**\n * The maximum capacity, used if a higher value is implicitly specified\n * by either of the constructors with arguments.\n * MUST be a power of two &lt;= 1&lt;&lt;30.\n *\/\nstatic final int MAXIMUM_CAPACITY = 1 &lt;&lt; 30;\n\n\/**\n * The load factor used when none specified in constructor.\n *\/\nstatic final float DEFAULT_LOAD_FACTOR = 0.75f;\n\n\/**\n * The bin count threshold for using a tree rather than list for a\n * bin.  Bins are converted to trees when adding an element to a\n * bin with at least this many nodes. The value must be greater\n * than 2 and should be at least 8 to mesh with assumptions in\n * tree removal about conversion back to plain bins upon\n * shrinkage.\n *\/\nstatic final int TREEIFY_THRESHOLD = 8;\n\n\/**\n * The bin count threshold for untreeifying a (split) bin during a\n * resize operation. Should be less than TREEIFY_THRESHOLD, and at\n * most 6 to mesh with shrinkage detection under removal.\n *\/\nstatic final int UNTREEIFY_THRESHOLD = 6;\n\n\/**\n * The smallest table capacity for which bins may be treeified.\n * (Otherwise the table is resized if too many nodes in a bin.)\n * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts\n * between resizing and treeification thresholds.\n *\/\nstatic final int MIN_TREEIFY_CAPACITY = 64;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#maximum_capacity\"><\/a>MAXIMUM_CAPACITY<\/h3>\n\n\n\n<p><code>MAXIMUM_CAPACITY<\/code>&nbsp;\u8868\u793a table \u7684\u6700\u5927\u5bb9\u91cf\uff0c\u5b83\u662f int \u7c7b\u578b\uff0c\u56e0\u6b64\u6700\u5927\u4e3a&nbsp;<code>1 &lt;&lt; 30<\/code>(\u6700\u9ad8\u4f4d\u4e3a\u7b26\u53f7\u4f4d)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#%E4%B8%BA%E4%BB%80%E4%B9%88-map-%E7%9A%84%E5%AE%B9%E9%87%8F%E8%A6%81%E9%99%90%E5%88%B6%E4%B8%BA2%E7%9A%84%E6%95%B4%E6%95%B0%E6%AC%A1%E6%96%B9\"><\/a>\u4e3a\u4ec0\u4e48 Map \u7684\u5bb9\u91cf\u8981\u9650\u5236\u4e3a2\u7684\u6574\u6570\u6b21\u65b9<\/h3>\n\n\n\n<p>Map \u7684\u5bb9\u91cf\u4f5c\u51fa\u8fd9\u6837\u7684\u9650\u5236\u4e3b\u8981\u662f\u4e3a\u4e86\u5feb\u901f\u5b9a\u4f4d\u5230\u6570\u7ec4\u7684\u4f4d\u7f6e\uff0c\u5e76\u4e14\u4f7f\u6570\u636e\u66f4\u52a0\u5206\u6563\uff0c\u51cf\u5c11\u78b0\u649e\uff0c\u67e5\u770b getNode \u6e90\u7801\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0 \uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">final Node&lt;K,V&gt; getNode(int hash, Object key) {\n  Node&lt;K,V&gt;[] tab; Node&lt;K,V&gt; first, e; int n; K k;\n  if ((tab = table) != null &amp;&amp; (n = tab.length) &gt; 0 &amp;&amp;\n      (first = tab[(n - 1) &amp; hash]) != null) {\n    if (first.hash == hash &amp;&amp; \/\/ always check first node\n        ((k = first.key) == key || (key != null &amp;&amp; key.equals(k))))\n      return first;\n    if ((e = first.next) != null) {\n      if (first instanceof TreeNode)\n        return ((TreeNode&lt;K,V&gt;)first).getTreeNode(hash, key);\n      do {\n        if (e.hash == hash &amp;&amp;\n            ((k = e.key) == key || (key != null &amp;&amp; key.equals(k))))\n          return e;\n      } while ((e = e.next) != null);\n    }\n  }\n  return null;\n}<\/pre>\n\n\n\n<p><code>first = tab[(n - 1) &amp; hash]<\/code>&nbsp;\u8fd9\u884c\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u901a\u8fc7hash\u503c\u53d6\u51fa\u6570\u7ec4\u4e2d\u7684\u503c\uff0c\u5176\u4e2d tab \u4e3a\u6570\u7ec4\uff0cn \u4e3a\u6570\u7ec4\u957f\u5ea6\uff0chash\u4e3akey \u7684hash\u503c\uff0c\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cn = 16\uff0c\u5219n &#8211; 1 = 15\uff0c\u4e8c\u8fdb\u5236\u4e3a 1111\uff0c \u8fd9\u6837\u8fdb\u884c &amp; hash \u64cd\u4f5c\u6700\u7ec8\u7ed3\u679c\u8fd8\u662f hash\uff0c \u5047\u8bben = 15 \uff0cn &#8211; 1 = 14\uff081110\uff09\uff0c \u8fd9\u6837\u4f1a\u9020\u6210\u67d0\u4e9b\u4f4d\u7f6e\uff08\u59820001\uff0c 0011\uff0c 0101&#8230;\uff09\u65e0\u6cd5\u5b58\u653e\u5143\u7d20\uff0c\u7a7a\u95f4\u6d6a\u8d39\u5f88\u5927\uff0c\u5e76\u4e14\u6570\u7ec4\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u4f4d\u7f6e\u6bd4\u6570\u7ec4\u957f\u5ea6\u5c0f\u4e86\u5f88\u591a\uff0c\u8fd9\u610f\u5473\u7740\u8fdb\u4e00\u6b65\u589e\u52a0\u4e86\u78b0\u649e\u7684\u51e0\u7387\uff0c\u51cf\u6162\u4e86\u67e5\u8be2\u7684\u6548\u7387\u3002\u56e0\u6b64\u6570\u7ec4\u957f\u5ea6\u4f7f\u7528 2 \u7684\u6574\u6570\u6b21\u65b9\uff0c\u53ef\u4ee5\u662f\u6570\u636e\u5206\u5e03\u5f97\u66f4\u5206\u6563\uff0c\u51cf\u5c11\u78b0\u649e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#default_load_factor\"><\/a>DEFAULT_LOAD_FACTOR<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Because TreeNodes are about twice the size of regular nodes, we use them only when bins contain enough nodes to warrant use (see TREEIFY_THRESHOLD). And when they become too small (due to removal or resizing) they are converted back to plain bins. In usages with well-distributed user hashCodes, tree bins are rarely used. Ideally, under random hashCodes, the frequency of nodes in bins follows a Poisson distribution<\/p><p>\u7ffb\u8bd1\uff1a<\/p><p>treeNodes \u5927\u7ea6\u662f \u666e\u901a nodes \u4e24\u500d\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u5728\u8fbe\u5230 TREEIFY_THRESHOLD\uff088\uff09\u65f6\u4f1a\u4f7f\u7528\uff0c \u5e76\u4e14\u5f53 treeNodes \u6570\u91cf\u53d8\u5c11\u8fbe\u5230 UNTREEIFY_THRESHOLD\uff086\uff09\u65f6\uff0c\u4f1a\u5c06\u6811\u8f6c\u53d8\u4e3a\u6b63\u5e38 node\u3002\u4f7f\u7528hashcode\u4f7f\u5f97\u8282\u70b9\u6563\u5217\u6b63\u5e38\uff0c\u7ea2\u9ed1\u6811\u5728\u5e73\u65f6\u5f88\u5c11\u7528\u5230\u3002\u7406\u8bba\u4e0a\uff0c\u5728\u968f\u673a\u7684 hashcode \u60c5\u51b5\u4e0b\uff0c\u8282\u70b9\u51fa\u73b0\u7684\u6982\u7387\u9075\u4ece Poisson \u5206\u5e03<\/p><\/blockquote>\n\n\n\n<p>\u9ed8\u8ba4\u7684\u88c5\u8f7d\u56e0\u5b50\u4e3a 0.75\uff0c\u8fd9\u662f\u56e0\u4e3abin\uff08\u6307\u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u6876\uff09 \u4e2d\u8282\u70b9\u51fa\u73b0\u7684\u6982\u7387\u9075\u5faaPoisson\u5206\u5e03\uff08\u6cca\u677e\u5206\u5e03\uff09\uff0c\u6b64\u65f6load factor = 0.75\uff0c \u03bb=0.5 $$ P(X=k) =\\frac{\\lambda^ke^{-\\lambda}}{k!} $$ \u5047\u5982\u5728\u957f\u5ea6\u4e3a 16 \u7684\u6570\u7ec4\u4e2d\u653e\u51650.75 * 16 = 12\u4e2a\u6570\u636e\u65f6\uff0c\u6570\u7ec4\u4e2d\u67d0\u4e2a\u4e0b\u6807\u653e\u5165 k \u4e2a\u6570\u636e\uff08\u5373\u6570\u7ec4\u540e\u9762\u94fe\u8868\u4e2d\u7684\u6570\u636e\u91cf\uff09\u7684\u6982\u7387\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u5b58\u50a8\u6570\u636e\u91cf<\/th><th>\u6982\u7387<\/th><\/tr><\/thead><tbody><tr><td>0<\/td><td>0.60653066<\/td><\/tr><tr><td>1<\/td><td>0.30326533<\/td><\/tr><tr><td>2<\/td><td>0.07581633<\/td><\/tr><tr><td>3<\/td><td>0.01263606<\/td><\/tr><tr><td>4<\/td><td>0.00157952<\/td><\/tr><tr><td>5<\/td><td>0.00015795<\/td><\/tr><tr><td>6<\/td><td>0.00001316<\/td><\/tr><tr><td>7<\/td><td>0.00000094<\/td><\/tr><tr><td>8<\/td><td>0.00000006<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4e00\u4e2a\u4e0b\u6807\u4e2d\u5b58\u57288\u4e2anodes\u7684\u6982\u7387\u662f\u5f88\u4f4e\u7684\uff0c\u56e0\u6b64 \u6b64\u65f6\u5c06\u94fe\u8868\u8f6c\u4e3a treeNode\u662f\u5408\u4e4e\u60c5\u7406\u7684\uff0c\u5e76\u4e14\u6027\u4ef7\u6bd4\u8f83\u9ad8 \uff0c\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u6709\u9650\u7684\u7a7a\u95f4\u6d88\u8017\u6362\u53d6\u65f6\u95f4\u6d88\u8017\u4e0a\u7684\u51cf\u5c11\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#%E5%9F%9F\"><\/a>\u57df<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\/**\n * The table, initialized on first use, and resized as\n * necessary. When allocated, length is always a power of two.\n * (We also tolerate length zero in some operations to allow\n * bootstrapping mechanics that are currently not needed.)\n *\/\ntransient Node&lt;K,V&gt;[] table;\n\n\/**\n * Holds cached entrySet(). Note that AbstractMap fields are used\n * for keySet() and values().\n *\/\ntransient Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet;\n\n\/**\n * The number of key-value mappings contained in this map.\n *\/\ntransient int size;\n\n\/**\n * The number of times this HashMap has been structurally modified\n * Structural modifications are those that change the number of mappings in\n * the HashMap or otherwise modify its internal structure (e.g.,\n * rehash).  This field is used to make iterators on Collection-views of\n * the HashMap fail-fast.  (See ConcurrentModificationException).\n *\/\ntransient int modCount;\n\n\/**\n * The next size value at which to resize (capacity * load factor).\n *\n * @serial\n *\/\n\/\/ (The javadoc description is true upon serialization.\n\/\/ Additionally, if the table array has not been allocated, this\n\/\/ field holds the initial array capacity, or zero signifying\n\/\/ DEFAULT_INITIAL_CAPACITY.)\nint threshold;\n\n\/**\n * The load factor for the hash table.\n *\n * @serial\n *\/\nfinal float loadFactor;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#threshold\"><\/a>threshold<\/h3>\n\n\n\n<p>???<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0\"><\/a>\u6784\u9020\u51fd\u6570<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\/**\n * Constructs an empty {@code HashMap} with the default initial capacity\n * (16) and the default load factor (0.75).\n *\/\npublic HashMap() {\n  this.loadFactor = DEFAULT_LOAD_FACTOR; \/\/ all other fields defaulted\n}\n\npublic HashMap(int initialCapacity) {\n  this(initialCapacity, DEFAULT_LOAD_FACTOR);\n}\n\npublic HashMap(int initialCapacity, float loadFactor) {\n  if (initialCapacity &lt; 0)\n    throw new IllegalArgumentException(\"Illegal initial capacity: \" +\n                                       initialCapacity);\n  if (initialCapacity &gt; MAXIMUM_CAPACITY)\n    initialCapacity = MAXIMUM_CAPACITY;\n  if (loadFactor &lt;= 0 || Float.isNaN(loadFactor))\n    throw new IllegalArgumentException(\"Illegal load factor: \" +\n                                       loadFactor);\n  this.loadFactor = loadFactor;\n  this.threshold = tableSizeFor(initialCapacity);\n}\n\nstatic final int tableSizeFor(int cap) {\n  int n = -1 &gt;&gt;&gt; Integer.numberOfLeadingZeros(cap - 1);\n  return (n &lt; 0) ? 1 : (n &gt;= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;\n}\n\npublic HashMap(Map&lt;? extends K, ? extends V&gt; m) {\n  this.loadFactor = DEFAULT_LOAD_FACTOR;\n  putMapEntries(m, false);\n}\n\nfinal void putMapEntries(Map&lt;? extends K, ? extends V&gt; m, boolean evict) {\n  int s = m.size();\n  if (s &gt; 0) {\n    if (table == null) { \/\/ pre-size\n      float ft = ((float)s \/ loadFactor) + 1.0F;\n      int t = ((ft &lt; (float)MAXIMUM_CAPACITY) ?\n               (int)ft : MAXIMUM_CAPACITY);\n      if (t &gt; threshold)\n        threshold = tableSizeFor(t);\n    }\n    else if (s &gt; threshold)\n      resize();\n    for (Map.Entry&lt;? extends K, ? extends V&gt; e : m.entrySet()) {\n      K key = e.getKey();\n      V value = e.getValue();\n      putVal(hash(key), key, value, false, evict);\n    }\n  }\n}<\/pre>\n\n\n\n<p>\u901a\u8fc7\u56db\u4e2a\u6784\u9020\u51fd\u6570\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5176\u4e3b\u8981\u5de5\u4f5c\u4e3a\u521d\u59cb\u5316\u8fd9\u4e9b\u53c2\u6570\uff1a<code>loadFactor<\/code>\uff0c&nbsp;<code>threshold<\/code>\uff0c<strong>\u5e76\u6ca1\u6709\u5b9e\u9645\u521d\u59cb\u5316\u94fe\u8868\u548c\u6570\u7ec4\uff0c\u8fd9\u6837\u53ef\u4ee5\u8282\u7701\u7a7a\u95f4\u3002<\/strong><\/p>\n\n\n\n<p>\u5728 JDK8 \u4e2d\uff0c threshold \u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/**\n * Returns a power of two size for the given target capacity.\n *\/\nstatic final int tableSizeFor(int cap) {\n  int n = cap - 1;\n  n |= n &gt;&gt;&gt; 1;\n  n |= n &gt;&gt;&gt; 2;\n  n |= n &gt;&gt;&gt; 4;\n  n |= n &gt;&gt;&gt; 8;\n  n |= n &gt;&gt;&gt; 16;\n  return (n &lt; 0) ? 1 : (n &gt;= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;\n}<\/pre>\n\n\n\n<p>JDK11 \u4e2d\u8c03\u7528\u4e86&nbsp;<code>Integer.numberOfLeadingZeros()<\/code>&nbsp;\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4e3b\u8981\u4f5c\u7528\uff1a\u6307\u5b9a int \u503c\u7684\u4e8c\u8fdb\u5236\u8865\u7801\u8868\u793a\u5f62\u5f0f\u4e2d\u6700\u9ad8\u4f4d\uff08\u6700\u5de6\u8fb9\uff09\u7684 1 \u4f4d\u4e4b\u524d\uff0c\u8fd4\u56de\u96f6\u4f4d\u7684\u6570\u91cf\u3002 \u53ef\u4ee5\u53c2\u8003\u535a\u5ba2\uff1a<a href=\"https:\/\/it007.blog.csdn.net\/article\/details\/87946142\">jdk11\u6e90\u7801&#8211;Integer.numberOfLeadingZeros<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/**\n * Returns a power of two size for the given target capacity.\n *\/\nstatic final int tableSizeFor(int cap) {\n  int n = -1 &gt;&gt;&gt; Integer.numberOfLeadingZeros(cap - 1);\n  return (n &lt; 0) ? 1 : (n &gt;= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;\n}<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u4e24\u4e2a&nbsp;<code>tableSizeFor(int cap)<\/code>&nbsp;\u90fd\u662f\u8fd4\u56de\u6700\u8fd1\u7684\u4e0d\u5c0f\u4e8e\u8f93\u5165\u53c2\u6570\u76842\u7684\u6574\u6570\u6b21\u5e42\u3002\u6bd4\u598210\uff0c\u5219\u8fd4\u56de16\uff0c\u96c6\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Integer.numberOfLeadingZeros(cap &#8211; 1) = 28\uff0c\u4e8c\u8fdb\u5236\u8868\u793a\u4e3a&nbsp;<code>00000000 00000000 00000000 00011100<\/code><\/li><li>n = -1 &gt;&gt;&gt; 28:<ul><li>-1 \u7684\u539f\u7801\uff1a<code>10000000 00000000 00000000 00000001<\/code><\/li><li>-1 \u7684\u53cd\u7801\uff1a<code>11111111 11111111 11111111 11111110<\/code><\/li><li>-1 \u7684\u8865\u7801\uff1a<code>11111111 11111111 11111111 11111111<\/code><\/li><li>-1 \u65e0\u7b26\u53f7 \u53f3\u79fb28\u4f4d\uff1a&nbsp;<code>00000000 00000000 00000000 00001111<\/code>&nbsp;= 15<\/li><\/ul><\/li><li>\u6700\u7ec8\u7ed3\u679c\uff1a15 + 1 = 16<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84\"><\/a>\u5b58\u50a8\u7ed3\u6784<\/h2>\n\n\n\n<p>HashMap \u7684\u5b58\u50a8\u7ed3\u6784\u4e3a \u6570\u7ec4 + \u94fe\u8868 + \u7ea2\u9ed1\u6811(JDK8\u540e\u65b0\u52a0)\uff0c\u6211\u4eec\u770b\u6e90\u7801\u9700\u8981\u5f04\u6e05\u695a\u4e24\u4e2a\u95ee\u9898\uff1a\u6570\u636e\u5e95\u5c42\u5177\u4f53\u5b58\u50a8\u7684\u662f\u4ec0\u4e48\uff1f\u8fd9\u6837\u7684\u5b58\u50a8\u65b9\u5f0f\u6709\u4ec0\u4e48\u597d\u5904\uff1f<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/camo.githubusercontent.com\/335fd9d86afa649bc07e617e30378912b59fc39fb4c0e486cc6e8a7d4b2f63d5\/68747470733a2f2f626c6f672d313330303636333132372e636f732e61702d7368616e676861692e6d7971636c6f75642e636f6d2f4261636b456e645f4e6f7465732f486173684d61705374727563747572652e706e67\" target=\"_blank\" rel=\"noreferrer noopener\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/camo.githubusercontent.com\/335fd9d86afa649bc07e617e30378912b59fc39fb4c0e486cc6e8a7d4b2f63d5\/68747470733a2f2f626c6f672d313330303636333132372e636f732e61702d7368616e676861692e6d7971636c6f75642e636f6d2f4261636b456e645f4e6f7465732f486173684d61705374727563747572652e706e67'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/camo.githubusercontent.com\/335fd9d86afa649bc07e617e30378912b59fc39fb4c0e486cc6e8a7d4b2f63d5\/68747470733a2f2f626c6f672d313330303636333132372e636f732e61702d7368616e676861692e6d7971636c6f75642e636f6d2f4261636b456e645f4e6f7465732f486173684d61705374727563747572652e706e67\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"hashMap structure\"\/><\/div><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#node\"><\/a>Node<\/h3>\n\n\n\n<p>HashMap \u4e2d\u7684 Node \u5185\u90e8\u7c7b\u5b9e\u73b0\u4e86 Map.Entry \u63a5\u53e3\uff0c\u672c\u8d28\u662f\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u5e76\u4e14 HashMap \u5185\u90e8\u6709\u4e00\u4e2aNode&lt;K, V&gt;[] table \u5b57\u6bb5\uff0c\u8fd9\u662f\u4e00\u4e2a \u54c8\u5e0c\u6876\u6570\u7ec4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static interface Map.Entry&lt;K,V&gt;\n<\/code><\/pre>\n\n\n\n<p>A map entry (key-value pair). The&nbsp;<code>Map.entrySet<\/code>&nbsp;method returns a collection-view of the map, whose elements are of this class. The&nbsp;<em>only<\/em>&nbsp;way to obtain a reference to a map entry is from the iterator of this collection-view.&nbsp;<strong>These&nbsp;<code>Map.Entry<\/code>&nbsp;objects are valid&nbsp;<em>only<\/em>&nbsp;for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the&nbsp;<code>setValue<\/code>&nbsp;operation on the map entry.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/**\n * The table, initialized on first use, and resized as\n * necessary. When allocated, length is always a power of two.\n * (We also tolerate length zero in some operations to allow\n * bootstrapping mechanics that are currently not needed.)\n *\/\ntransient Node&lt;K,V&gt;[] table;\n\n\/**\n * Basic hash bin node, used for most entries.  (See below for\n * TreeNode subclass, and in LinkedHashMap for its Entry subclass.)\n *\/\nstatic class Node&lt;K,V&gt; implements Map.Entry&lt;K,V&gt; {\n  \/\/ \u7528\u6765\u5b9a\u4f4d\u6570\u636e\u7d22\u5f15\u7684\u4f4d\u7f6e\n  final int hash;\n  final K key;\n  V value;\n  \/\/ \u94fe\u8868\u7684\u4e0b\u4e00\u4e2a Node\n  Node&lt;K,V&gt; next;\n\n  Node(int hash, K key, V value, Node&lt;K,V&gt; next) {\n    this.hash = hash;\n    this.key = key;\n    this.value = value;\n    this.next = next;\n  }\n\n  public final K getKey()        { return key; }\n  public final V getValue()      { return value; }\n  public final String toString() { return key + \"=\" + value; }\n\n  public final int hashCode() {\n    return Objects.hashCode(key) ^ Objects.hashCode(value);\n  }\n\n  public final V setValue(V newValue) {\n    V oldValue = value;\n    value = newValue;\n    return oldValue;\n  }\n\n  public final boolean equals(Object o) {\n    if (o == this)\n      return true;\n    if (o instanceof Map.Entry) {\n      Map.Entry&lt;?,?&gt; e = (Map.Entry&lt;?,?&gt;)o;\n      if (Objects.equals(key, e.getKey()) &amp;&amp;\n          Objects.equals(value, e.getValue()))\n        return true;\n    }\n    return false;\n  }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#%E9%87%8D%E8%A6%81%E6%96%B9%E6%B3%95\"><\/a>\u91cd\u8981\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u9996\u5148\u660e\u767d\u4e00\u4e0b\u6982\u5ff5\uff0cHashMap\u5c31\u662f\u4f7f\u7528\u54c8\u5e0c\u8868\u6765\u5b58\u50a8\u7684\u3002\u54c8\u5e0c\u8868\u4e3a\u89e3\u51b3\u51b2\u7a81\uff0c\u53ef\u4ee5\u91c7\u7528\u5f00\u653e\u5730\u5740\u6cd5\u548c\u94fe\u5730\u5740\u6cd5\u7b49\u6765\u89e3\u51b3\u95ee\u9898\uff0cJava\u4e2dHashMap\u91c7\u7528\u4e86\u94fe\u5730\u5740\u6cd5\u3002\u94fe\u5730\u5740\u6cd5\uff0c\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u6570\u7ec4\u52a0\u94fe\u8868\u7684\u7ed3\u5408\u3002\u5728\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u4e0a\u90fd\u4e00\u4e2a\u94fe\u8868\u7ed3\u6784\uff0c\u5f53\u6570\u636e\u88abHash\u540e\uff0c\u5f97\u5230\u6570\u7ec4\u4e0b\u6807\uff0c\u628a\u6570\u636e\u653e\u5728\u5bf9\u5e94\u4e0b\u6807\u5143\u7d20\u7684\u94fe\u8868\u4e0a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#%E7%A1%AE%E5%AE%9A%E5%93%88%E5%B8%8C%E6%A1%B6%E6%95%B0%E7%BB%84%E7%B4%A2%E5%BC%95%E4%BD%8D%E7%BD%AE\"><\/a>\u786e\u5b9a\u54c8\u5e0c\u6876\u6570\u7ec4\u7d22\u5f15\u4f4d\u7f6e<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/**\n * Computes key.hashCode() and spreads (XORs) higher bits of hash\n * to lower.  Because the table uses power-of-two masking, sets of\n * hashes that vary only in bits above the current mask will\n * always collide. (Among known examples are sets of Float keys\n * holding consecutive whole numbers in small tables.)  So we\n * apply a transform that spreads the impact of higher bits\n * downward. There is a tradeoff between speed, utility, and\n * quality of bit-spreading. Because many common sets of hashes\n * are already reasonably distributed (so don't benefit from\n * spreading), and because we use trees to handle large sets of\n * collisions in bins, we just XOR some shifted bits in the\n * cheapest possible way to reduce systematic lossage, as well as\n * to incorporate impact of the highest bits that would otherwise\n * never be used in index calculations because of table bounds.\n * \n * \u7ffb\u8bd1\uff1a\n * \u8ba1\u7b97 key.hashCode()\u7684\u503c\uff0c\u5e76\u4e14\u5c06hash\u7684\u9ad8\u4f4d\u901a\u8fc7\u5f02\u6216\uff08XORs\uff09\u6269\u5c55\u5230\u5730\u4f4d\uff0c\u7531\u4e8etable     \t\n * \u4f7f\u75282\u7684\u6307\u6570\u8fdb\u884cmasking\uff0c\u56e0\u6b64\u4ec5\u5728\u5176\u8303\u56f4\u8fdb\u884cmask\u7684hash\u5c06\u4f1a\u53d1\u751f\u51b2\u7a81\uff08\u5df2\u77e5\u7684\u4f8b\u5b50\u4e3a\u5728     \t\n * \u5c0ftable\u4e2d\u8fde\u7eed\u7684Float keys\uff09\u6240\u4ee5\u8fd9\u91cc\u4f7f\u7528\u4e86\u8f6c\u6362\u5c06\u9ad8\u4f4d\u5ef6\u5c55\u5230\u5730\u4f4d\uff0c\u8fd9\u662f\u5728speed \t\t \t\n * utility\u548cquality of bit-spreading\u4e4b\u95f4\u7684tradeoff\uff0c\u56e0\u4e3a\u5f88\u591a\u5e38\u89c1\u7684hash\u5df2\u7ecf\u5408\u7406       \n * \u5730\u5206\u5e03 \uff08\u6240\u4ee5\u4e0d\u4f1a\u4ecespread\u4e2d\u53d7\u76ca\uff09\uff0c\u5e76\u4e14\u6211\u54e6\u4eec\u4f7f\u7528\u7ea2\u9ed1\u6811\u6765\u5904\u7406\u591ahash\u51b2\u7a81\uff0c\u6211\u4eec\u53eaXOR \t\n * \u4e00 \u4e9b\u4f4d\u6765\u51cf\u5c11\u7cfb\u7edf\u7f3a\u5931\uff0c\u5e76\u4e14\u5426\u5219\u9ad8\u4f4d\u7684\u4e0d\u51c6\u786e\u5f71\u54cd\u4e0d\u4f1a\u5f71\u54cd\u5230index \u8ba1\u7b97\u3002\n *\n *\/\nstatic final int hash(Object key) {\n  int h;\n  return (key == null) ? 0 : (h = key.hashCode()) ^ (h &gt;&gt;&gt; 16);\n}\n\n\/\/jdk1.7\u7684\u6e90\u7801\uff0cjdk1.8\u6ca1\u6709\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u4f46\u662f\u5b9e\u73b0\u539f\u7406\u4e00\u6837\u7684\nstatic int indexFor(int h, int length) {  \n  return h &amp; (length-1);  \n}\n\n\/\/ JDK 1.8 \u6e90\u7801\nfinal Node&lt;K,V&gt; getNode(int hash, Object key) {\n  \/\/ ...\n\t(first = tab[(n - 1) &amp; hash]) != null)\n  \/\/ ...\n}<\/pre>\n\n\n\n<p>\u786e\u5b9a\u6570\u7ec4\u7d22\u5f15\u4f4d\u7f6e\u4e00\u5171\u6709\u4e09\u6b65\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u53d6 hashCode\u503c\uff1ah = key.hashCode()<\/li><li>\u9ad8\u4f4d\u53c2\u4e0e\u8fd0\u7b97\uff1ah ^ (h &gt;&gt;&gt; 16)\uff08\u6b64\u5b9e\u73b0\u4eceJDK1.8\u5f00\u59cb\uff0c\u4f18\u5316\u4e86\u9ad8\u4f4d\u8fd0\u7b97\u7684\u7b97\u6cd5\uff0c\u901a\u8fc7hashCode()\u7684\u9ad816\u4f4d\u5f02\u6216\u4f4e16\u4f4d\u5b9e\u73b0\u7684\uff1a(h = k.hashCode()) ^ (h &gt;&gt;&gt; 16)\uff0c\u4e3b\u8981\u662f\u4ece\u901f\u5ea6\u3001\u529f\u6548\u3001\u8d28\u91cf\u6765\u8003\u8651\u7684\uff0c\u8fd9\u4e48\u505a\u53ef\u4ee5\u5728\ufffd\u6570\u7ec4table\u7684length\u6bd4\u8f83\u5c0f\u7684\u65f6\u5019\uff0c\u4e5f\u80fd\u4fdd\u8bc1\u8003\u8651\u5230\u9ad8\u4f4eBit\u90fd\u53c2\u4e0e\u5230Hash\u7684\u8ba1\u7b97\u4e2d\uff0c\u540c\u65f6\u4e0d\u4f1a\u6709\u592a\u5927\u7684\u5f00\u9500\u3002\uff09<\/li><li>\u53d6\u6a21\u8fd0\u7b97\uff1a\uff08n &#8211; 1\uff09&amp; hash (n \u4e3a capacity\uff0c\u8fd9\u91cc\u5229\u7528\u4f4d\u8fd0\u7b97\u8fdb\u884c\u53d6\u6a21\uff0c\u6548\u7387\u66f4\u9ad8\uff0c\u8be5\u8868\u8fbe\u5f0f\u76f8\u5f53\u4e8e hash % n)<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u8fd9\u91cc\u91cd\u70b9\u770b\u4e00\u4e0b\u53d6\u6a21\u8fd0\u7b97\uff0c\u8fd9\u91cc\u7528\u4e86\u4f4d\u8fd0\u7b97\uff0c\u800c\u6ca1\u6709\u4f7f\u7528&nbsp;<code>%<\/code>\uff0c\u8fd9\u662f\u56e0\u4e3a \u6570\u7ec4\u7684\u957f\u5ea6 n \uff08capacity\uff09\u662f2\u7684\u6307\u6570<\/p>\n\n\n\n<p>\u6211\u4eec\u8fd9\u91cc\u4ee5 x = 1 &lt;&lt; 4 \u4e3a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">x: \t\t 00010000\nx - 1: 00001111<\/pre>\n\n\n\n<p>\u4ee4 y \u4e0e x &#8211; 1 \u505a &amp; \u8fd0\u7b97<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">y :        10110110\nx - 1:     00001111\ny&amp;(x - 1): 00000110<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4e0e y \u5bf9 x \u53d6\u6a21\u7ed3\u679c\u662f\u4e00\u6837\u7684<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">y :     10110110\nx :     00010000\ny &amp; x : 00000110<\/pre>\n\n\n\n<p>\u6211\u4eec\u8fd8\u80fd\u4ece\u8fd0\u7b97\u4e2d\u5f97\u51fa\u7ed3\u8bba\uff1a**\u6570\u7ec4\u7684 \u957f\u5ea6 n \u8d8a\u5c0f\uff0c\u6700\u7ec8\u8ba1\u7b97\u51fa\u7684 \u7d22\u5f15 \u8d8a\u6709\u53ef\u80fd\u53d1\u751f\u78b0\u649e\uff0c\u56e0\u6b64\u6211\u4eec\u5728\u8fdb\u884c\u53d6\u6a21\u8fd0\u7b97\u4e4b\u524d\u9700\u8981\u5148\u8fdb\u884c\u9ad8\u4f4d\u5f02\u6216\u8fd0\u7b97\uff0c\u6765\u51cf\u5c0f\u53d1\u751f\u78b0\u649e\u7684\u6982\u7387\u3002**\u4ec5\u4ec5\u5f02\u6216\u4e00\u4e0b\uff0c\u65e2\u51cf\u5c11\u4e86\u7cfb\u7edf\u7684\u5f00\u9500\uff0c\u4e5f\u4e0d\u4f1a\u9020\u6210\u56e0\u4e3a\u9ad8\u4f4d\u6ca1\u6709\u53c2\u4e0e\u4e0b\u6807\u7684\u8ba1\u7b97(table\u957f\u5ea6\u6bd4\u8f83\u5c0f\u65f6)\uff0c\u4ece\u800c\u5f15\u8d77\u7684\u78b0\u649e\u3002\u5982\u4e0b\u8868\u4e2d\u7684\u4f8b\u5b50<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>key.hashCode()<\/th><th><code>00001010 00001111 00001001 00001101<\/code><\/th><\/tr><\/thead><tbody><tr><td>\u65e0\u7b26\u53f7\u53f3\u79fb16\u4f4d<\/td><td><code>00000000 00000000 00001010 00001111<\/code><\/td><\/tr><tr><td>\u4e24\u8005\u7591\u60d1\u7684\u7ed3\u679c<\/td><td><code>00001010 00001111 00000011 00000010<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u53e6\u5916\u6709\u4e00\u70b9\u8981\u6ce8\u610f\uff0c<code>HashMap<\/code>&nbsp;\u5141\u8bb8&nbsp;<code>null<\/code>&nbsp;\u4f5c\u4e3a&nbsp;<code>key<\/code>\uff0c \u6240\u4ee5\u5728&nbsp;<code>hash<\/code>&nbsp;\u65b9\u6cd5\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230&nbsp;<code>null<\/code>&nbsp;\u5bf9\u8c61\u7684&nbsp;<code>hash<\/code>&nbsp;\u7ed3\u679c\u4e3a 0\uff0c&nbsp;<code>HashMap<\/code>&nbsp;\u4f7f\u7528\u7b2c 0 \u4e2a\u6876\u6765\u5b58\u653e\u952e\u4e3a&nbsp;<code>null<\/code>&nbsp;\u7684\u952e\u503c\u5bf9\u3002<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#put-%E6%96%B9%E6%B3%95\"><\/a>put \u65b9\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">public V put(K key, V value) {\n  return putVal(hash(key), key, value, false, true);\n}\n\nfinal V putVal(int hash, K key, V value, boolean onlyIfAbsent,\n                   boolean evict) {\n  Node&lt;K,V&gt;[] tab; Node&lt;K,V&gt; p; int n, i;\n  \/\/ 1. \u5224\u65ad table \u662f\u5426\u4e3a\u7a7a\u6216\u4e3anull\uff0c\u5982\u679c\u4e3a\u7a7a\u6216null\u5219\u8c03\u7528 `resize()` \u8fdb\u884c\u521d\u59cb\u5316\n  if ((tab = table) == null || (n = tab.length) == 0)\n    n = (tab = resize()).length;\n  \/\/ 2. \u5224\u65ad\u5f53\u524d\u7d22\u5f15\u662f\u5426\u6709\u503c\n  if ((p = tab[i = (n - 1) &amp; hash]) == null)\n    tab[i] = newNode(hash, key, value, null);\n  else {\n    Node&lt;K,V&gt; e; K k;\n    \/\/ 3. \u5224\u65ad\u6570\u7ec4\u5f53\u524d\u7d22\u5f15\u5b58\u5728\u7684 Node \u7684 hash \u4e0e\u6b63\u8981\u52a0\u5165 Node \u7684hash \u662f\u5426\u76f8\u540c\uff0c\u4ee5\u53ca\u4e24\u8005\u7684 key \u662f\u5426\u540c\u4e00\u5f15\u7528\u6216\u8005equals\u65b9\u6cd5\u8fd4\u56detrue\n    if (p.hash == hash &amp;&amp;\n        ((k = p.key) == key || (key != null &amp;&amp; key.equals(k))))\n      e = p;\n    \/\/ 4. \u5224\u65ad\u76ee\u524d\u7d22\u5f15\u7684\u503c\u662f\u5426\u5728\u7ea2\u9ed1\u6811\u4e0a\n    else if (p instanceof TreeNode)\n      e = ((TreeNode&lt;K,V&gt;)p).putTreeVal(this, tab, hash, key, value);\n    else {\n      \/\/ 5. \u904d\u5386\u5f53\u524d\u7d22\u5f15\u94fe\u8868\uff0c\u5982\u679c\u94fe\u8868\u957f\u5ea6\u5927\u4e8e8\uff0c\u5219\u5c06\u94fe\u8868\u8f6c\u4e3a\u7ea2\u9ed1\u6811\uff0c\u5728\u7ea2\u9ed1\u6811\u4e2d\u8fdb\u884c\u63d2\u5165\u64cd\u4f5c\uff0c\u5426\u5219\u8fdb\u884c\u94fe\u8868\u7684\u63d2\u5165\u64cd\u4f5c\uff0c\u5982\u679c\u904d\u5386\u8fc7\u7a0b\u4e2d\u53d1\u73b0\u76f8\u540c\u7684node\uff0c\u5219\u76f4\u63a5\u9000\u51fa\u5faa\u73af\n      for (int binCount = 0; ; ++binCount) {\n        if ((e = p.next) == null) {\n          p.next = newNode(hash, key, value, null);\n          if (binCount &gt;= TREEIFY_THRESHOLD - 1) \/\/ -1 for 1st\n            treeifyBin(tab, hash);\n          break;\n        }\n        \/\/ key \u5df2\u7ecf\u5b58\u5728\u5219\u76f4\u63a5\u8986\u76d6\n        if (e.hash == hash &amp;&amp;\n            ((k = e.key) == key || (key != null &amp;&amp; key.equals(k))))\n          break;\n        p = e;\n      }\n    }\n    if (e != null) { \/\/ existing mapping for key\n      V oldValue = e.value;\n      if (!onlyIfAbsent || oldValue == null)\n        e.value = value;\n      afterNodeAccess(e);\n      return oldValue;\n    }\n  }\n  ++modCount;\n  \/\/ 6. \u5224\u65ad\u662f\u5426\u8d85\u8fc7 threshold\uff0c\u8d85\u8fc7\u5219\u6269\u5bb9\n  if (++size &gt; threshold)\n    resize();\n  afterNodeInsertion(evict);\n  return null;\n}\n\n\/*\n * The following package-protected methods are designed to be\n * overridden by LinkedHashMap, but not by any other subclass.\n * Nearly all other internal methods are also package-protected\n * but are declared final, so can be used by LinkedHashMap, view\n * classes, and HashSet.\n *\/\n\n\/\/ Create a regular (non-tree) node\nNode&lt;K,V&gt; newNode(int hash, K key, V value, Node&lt;K,V&gt; next) {\n  return new Node&lt;&gt;(hash, key, value, next);\n}<\/pre>\n\n\n\n<p>Put \u65b9\u6cd5\u53ef\u4ee5\u62bd\u8c61\u6982\u62ec\u4e3a\u4e00\u4e0b\u516d\u6b65\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5224\u65ad table \u662f\u5426\u4e3a\u7a7a\u6216\u4e3anull\uff0c\u5982\u679c\u4e3a\u7a7a\u6216null\u5219\u8c03\u7528&nbsp;<code>resize()<\/code>&nbsp;\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/li><li>\u8ba1\u7b97 key \u7684 hash \u503c\uff0c\u5e76\u6700\u7ec8\u901a\u8fc7\u4e0a\u9762\u7684\u65b9\u6cd5\u6c42\u51fa\u8be5 key \u5728\u6570\u7ec4\u4e2d\u5bf9\u5e94\u7684\u7d22\u5f15 i\uff0c\u5982\u679c table[i] == null,\u76f4\u63a5\u901a\u8fc7&nbsp;<code>newNode()<\/code>&nbsp;\u65b0\u5efa\u975e\u6811\u8282\u70b9\uff08Node\uff09\uff0c\u8f6c\u5411 \u6b65\u9aa46\uff0c\u5426\u5219\u8f6c\u5411 \u6b65\u9aa43.<\/li><li>\u5224\u65ad\u6570\u7ec4\u5f53\u524d\u7d22\u5f15\u5b58\u5728\u7684&nbsp;<strong>\u9996\u4e2aNode<\/strong>&nbsp;\u7684 hash \u4e0e\u6b63\u8981\u52a0\u5165 Node \u7684hash \u662f\u5426\u76f8\u540c\uff08\u7528\u5230<code>key.hashCode()<\/code>\uff09\uff0c\u4ee5\u53ca\u4e24\u8005\u7684 key \u662f\u5426\u540c\u4e00\u5f15\u7528\u6216\u8005equals\u65b9\u6cd5(\u7528\u5230&nbsp;<code>key.equals()<\/code>)\u8fd4\u56detrue\uff0c**\u8fd9\u4e00\u6b65\u5c31\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u4f5c\u4e3a key \u7684\u7c7b\u4e00\u5b9a\u8981\u91cd\u5199&nbsp;<code>hashCode<\/code>&nbsp;\u548c&nbsp;<code>equals<\/code>&nbsp;\u65b9\u6cd5\u3002**\u6bd4\u8f83\u4e4b\u540e\u5982\u679c\u76f8\u540c\uff0c\u5219\u76f4\u63a5\u8986\u76d6 value\uff0c\u5426\u5219 \u6b65\u9aa44\u3002<\/li><li>\u5224\u65ad table[i] \u662f\u5426\u4e3a TreeNode\uff0c\u5373\u662f\u5426\u662f\u7ea2\u9ed1\u6811\uff0c\u5982\u679c\u662f\u7ea2\u9ed1\u6811\u76f4\u63a5\u5728\u6811\u4e0a\u63d2\u5165\u952e\u503c\u5bf9\uff0c\u5426\u5219\u8f6c\u5411 \u6b65\u9aa45.<\/li><li>\u904d\u5386\u5f53\u524d\u7d22\u5f15\u94fe\u8868\uff0c\u5982\u679c\u94fe\u8868\u957f\u5ea6\u5927\u4e8e8\uff0c\u5219\u5c06\u94fe\u8868\u8f6c\u4e3a\u7ea2\u9ed1\u6811\uff0c\u5728\u7ea2\u9ed1\u6811\u4e2d\u8fdb\u884c\u63d2\u5165\u64cd\u4f5c\uff0c\u5426\u5219\u8fdb\u884c\u94fe\u8868\u7684\u63d2\u5165\u64cd\u4f5c\uff0c\u5982\u679c\u904d\u5386\u8fc7\u7a0b\u4e2d\u53d1\u73b0\u76f8\u540c\u7684node\uff0c\u5219\u76f4\u63a5\u8986\u76d6\u3002<strong>\u4ece\u6e90\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4eceJDK8\u5f00\u59cb\uff0c\u94fe\u8868\u7684\u63d2\u5165\u65b9\u6cd5\u7531\u5934\u63d2\u6cd5\u6539\u6210\u4e86\u5c3e\u63d2\u6cd5\u3002<\/strong><\/li><li>\u63d2\u5165\u6210\u529f\u540e\uff08\u8fd9\u91cc\u7684\u63d2\u5165\u6307\u4e4b\u524d i \u7d22\u5f15\u5904\u65e0 node\u65f6\u63d2\u5165\uff09\uff0c\u5224\u65ad\u5b9e\u9645\u5b58\u5728\u7684\u952e\u503c\u5bf9\u6570\u91cf\u662f\u5426\u8d85\u8fc7\u4e86\u6700\u5927\u5bb9\u91cf threshold\uff0c\u5982\u679c\u8d85\u8fc7\uff0c\u8fdb\u884c\u6269\u5bb9\u3002<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#resize-%E6%96%B9%E6%B3%95\"><\/a>resize \u65b9\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/**\n * Initializes or doubles table size.  If null, allocates in\n * accord with initial capacity target held in field threshold.\n * Otherwise, because we are using power-of-two expansion, the\n * elements from each bin must either stay at same index, or move\n * with a power of two offset in the new table.\n *\n * @return the table\n *\/\nfinal Node&lt;K,V&gt;[] resize() {\n  Node&lt;K,V&gt;[] oldTab = table;\n  int oldCap = (oldTab == null) ? 0 : oldTab.length;\n  int oldThr = threshold;\n  int newCap, newThr = 0;\n  if (oldCap &gt; 0) {\n    \/\/ \u5f53\u6570\u7ec4\u5bb9\u91cf\u8fbe\u5230\u6700\u5927\u65f6\u4e0d\u518d\u6269\u5bb9\n    if (oldCap &gt;= MAXIMUM_CAPACITY) {\n      threshold = Integer.MAX_VALUE;\n      return oldTab;\n    }\n    else if ((newCap = oldCap &lt;&lt; 1) &lt; MAXIMUM_CAPACITY &amp;&amp;\n             oldCap &gt;= DEFAULT_INITIAL_CAPACITY)\n      newThr = oldThr &lt;&lt; 1; \/\/ double threshold\n  }\n  else if (oldThr &gt; 0) \/\/ initial capacity was placed in threshold\n    newCap = oldThr;\n  \/\/ \u521b\u5efa hashMap \u4e4b\u540e\u7b2c\u4e00\u6b21put\n  else {               \/\/ zero initial threshold signifies using defaults\n    newCap = DEFAULT_INITIAL_CAPACITY;\n    newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);\n  }\n  if (newThr == 0) {\n    float ft = (float)newCap * loadFactor;\n    newThr = (newCap &lt; MAXIMUM_CAPACITY &amp;&amp; ft &lt; (float)MAXIMUM_CAPACITY ?\n              (int)ft : Integer.MAX_VALUE);\n  }\n  threshold = newThr;\n  @SuppressWarnings({\"rawtypes\",\"unchecked\"})\n  Node&lt;K,V&gt;[] newTab = (Node&lt;K,V&gt;[])new Node[newCap];\n  table = newTab;\n  \/\/ \u5faa\u73af\u904d\u5386\u8001map\u4e2d\u7684\u5143\u7d20\uff0c\u8fc1\u79fb\u5230\u5bf9\u5e94\u65b0\u6570\u7ec4\u4e2d\u53bb\uff0c\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c\n  if (oldTab != null) {\n    for (int j = 0; j &lt; oldCap; ++j) {\n      Node&lt;K,V&gt; e;\n      if ((e = oldTab[j]) != null) {\n        \/\/ \u8001\u6570\u7ec4\u4e2d\u5143\u7d20\u5f15\u7528\u7f6e\u4e3anull\n        oldTab[j] = null;\n        \/\/ \u8be5\u7d22\u5f15\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u76f4\u63a5\u8ba1\u7b97\u65b0\u7d22\u5f15\u5c06 e \u590d\u5236\u5230\u65b0\u6570\u7ec4\n        if (e.next == null)\n          newTab[e.hash &amp; (newCap - 1)] = e;\n        \/\/ \u5982\u679c\u662f\u7ea2\u9ed1\u6811\n        else if (e instanceof TreeNode)\n          ((TreeNode&lt;K,V&gt;)e).split(this, newTab, j, oldCap);\n        \/\/ \u5982\u679c\u8be5\u6240\u4ee5\u94fe\u8868\u7531\u591a\u4e2a\u503c\n        else { \/\/ preserve order\n          Node&lt;K,V&gt; loHead = null, loTail = null;\n          Node&lt;K,V&gt; hiHead = null, hiTail = null;\n          Node&lt;K,V&gt; next;\n          do {\n            next = e.next;\n            \/\/ \u8fd9\u91cc\u901a\u8fc7\u5f53\u524dhash\u4e0e\u6570\u7ec4\u957f\u5ea6\u8fdb\u884c\u903b\u8f91\u4e0e\u64cd\u4f5c\uff0c\u5224\u65ad\u662f\u5426\u4e3a0\uff0c\u6765\u533a\u5206\u8be5\u5143\u7d20\u662f\u4e0d\u53d8\u4f4d\u7f6e\u8fd8\u662f\u9700\u8981\u91cd\u65b0\u66f4\u6362\u4f4d\u7f6e\n            if ((e.hash &amp; oldCap) == 0) {\n              if (loTail == null)\n                loHead = e;\n              else\n                loTail.next = e;\n              loTail = e;\n            }\n            else {\n              if (hiTail == null)\n                hiHead = e;\n              else\n                hiTail.next = e;\n              hiTail = e;\n            }\n          } while ((e = next) != null);\n          if (loTail != null) {\n            loTail.next = null;\n            newTab[j] = loHead;\n          }\n          if (hiTail != null) {\n            hiTail.next = null;\n            \/\/ \u73b0\u6709index+\u73b0\u6709\u6570\u7ec4\u7684\u957f\u5ea6\u5c31\u662f\u65b0\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u4f4d\u7f6e\n            newTab[j + oldCap] = hiHead;\n          }\n        }\n      }\n    }\n  }\n  return newTab;\n}<\/pre>\n\n\n\n<p>\u5206\u6790\u4ee5\u4e0a\u4ee3\u7801\u53ef\u4ee5\u5f97\u51fa\u5982\u4e0b\u7ed3\u8bba\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u65b0\u5efa\u4e00\u4e2a HashMap \u5bf9\u8c61\u540e\uff0c\u7b2c\u4e00\u6b21&nbsp;<code>put<\/code>&nbsp;\u4f1a\u89e6\u53d1&nbsp;<code>resize<\/code>&nbsp;\u64cd\u4f5c\uff0c\u6b64\u65f6\u4f1a\u7528&nbsp;<code>threshold<\/code>(\u521d\u59cbthreshold\u4e3a12) \u6765\u521d\u59cb\u5316&nbsp;<code>capacity<\/code>&nbsp;(line 687&nbsp;<code>newCap = oldThr;<\/code>)<\/li><li>\u5f53\u5f53\u524d\u6570\u7ec4\u957f\u5ea6 &gt;= 16 \u65f6\uff0c\u6570\u7ec4\u4f1a\u6269\u5bb9\u4e3a\u539f\u6765\u7684\u4e24\u500d\uff08<code>newCap = oldCap &lt;&lt; 1`` newThr = oldThr &lt;&lt; 1<\/code>\uff09<\/li><li>\u6269\u5bb9\u65f6\u4e0d\u4f1a\u91cd\u65b0\u8ba1\u7b97hash\u503c\uff0chash\u503c\u4fdd\u5b58\u5728 Node \u5bf9\u8c61\u4e2d\uff0c\u53ea\u4f1a\u6539\u53d8 Node \u5bf9\u8c61\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15<\/li><li>\u6269\u5bb9\u65f6\uff0c\u73b0\u6709\u5143\u7d20\u8981\u4e48\u4e0d\u52a8\uff0c\u8981\u4e48\u7d22\u5f15\u53d8\u4e3a\u539f\u6765\u7d22\u5f15 + \u539f\u6765\u6570\u7ec4\u957f\u5ea6<\/li><li>\u6269\u5bb9\u65f6\u5224\u65ad\u5143\u7d20\u662f\u5426\u79fb\u52a8\u662f\u901a\u8fc7&nbsp;<code>(e.hash &amp; oldCap) == 0<\/code>\uff0c\u4e0b\u9762\u4e3e\u4f8b\u8bf4\u660e<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u4ee5&nbsp;<code>map.put(\"a1\",\"a\");<\/code>\u4e3a\u4f8b\uff0c a1 \u8ba1\u7b97\u540e\u7684hash\u503c\u4e3a3056\uff0c\u7ecf\u8fc7\u8ba1\u7b97\uff0c\u521d\u59cb\u5316\u65f6 a1\u5b58\u5728\u6570\u7ec4\u76840\u7d22\u5f15\u5904<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u521d\u59cb\u5316\u6570\u7ec4\u957f\u5ea6\u4e3a16\uff0c 16 &#8211; 1 = 15 \u5bf9\u5e94\u7684\u4e8c\u8fdb\u5236<\/th><th>00000000 00001111<\/th><\/tr><\/thead><tbody><tr><td>3056\u5bf9\u5e94\u7684\u4e8c\u8fdb\u5236<\/td><td>00001011 11110000<\/td><\/tr><tr><td>3056 &amp; 15<\/td><td>00000000 00000000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6269\u5bb9\u540e\uff0c\u6570\u7ec4\u957f\u5ea6\u53d8\u4e3a32\uff0c\u6b64\u65f6\u8ba1\u7b97&nbsp;<code>(e.hash &amp; oldCap)<\/code><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u6570\u7ec4\u521d\u59cb\u5316\u957f\u5ea616<\/th><th>00000000 00010000<\/th><\/tr><\/thead><tbody><tr><td>3056 \u4e8c\u8fdb\u5236\u503c<\/td><td>00001011 11110000<\/td><\/tr><tr><td>3056 &amp; 16<\/td><td>00000000 000<strong>1<\/strong>0000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5 \u770b\u5230\u7ed3\u679c\u4e0d\u4e3a0\uff0c \u8bf4\u660e a1 \u9700\u8981\u8fc1\u79fb\uff0c\u6211\u4eec\u6765\u8ba1\u7b97\u4e00\u4e0b\u8fc1\u79fb\u540e\u7684\u65b0\u4f4d\u7f6e\u7d22\u5f15<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u6269\u5bb9\u540e\u6570\u7ec4\u957f\u5ea6\u4e3a32\uff0c 32 &#8211; 1 = 31 \u4e8c\u8fdb\u5236\u503c<\/th><th>00000000 000<strong>1<\/strong>1111<\/th><\/tr><\/thead><tbody><tr><td>3056 \u4e8c\u8fdb\u5236\u503c<\/td><td>00001011 11110000<\/td><\/tr><tr><td>31 &amp; 3056<\/td><td>00000000 000<strong>1<\/strong>0000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8ba1\u7b97\u51fa\u7684\u7d22\u5f15\u4e3a16\uff0c\u6b63\u597d\u7b49\u4e8e<code>j + oldCap<\/code>&nbsp;<code>(0 + 16)<\/code>\uff0c\u56e0\u4e3a\u6269\u5bb9\u540e\uff0c31 \u76f8\u5bf9\u4e8e 15 \u5728\u9ad8\u4f4d\u591a\u4e86\u4e00\u4e2a 1\uff0c \u6b64\u65f6\u53ea\u8981\u901a\u8fc7&nbsp;<code>(e.hash &amp; oldCap)<\/code>&nbsp;\u8ba1\u7b97\u51fa\u8be5\u9ad8\u4f4d\u591a\u51fa\u7684 1 \u5bf9\u7d22\u5f15\u7684\u5f71\u54cd\u662f0\u8fd8\u662f2\u7684\u6307\u6570\u5373\u53ef\uff0c\u662f0\u7684\u8bdd\u7d22\u5f15\u4e0d\u53d8\uff0c\u4e0d\u4e3a0\u5219\u53d8\u6210&nbsp;<code>\u539f\u7d22\u5f15+oldCap<\/code>\uff0c\u56e0\u6b64\u6269\u5bb9\u65f6node \u8981\u4e48\u4e0d\u53d8\uff0c\u8981\u4e48\u5c31\u79fb\u52a82\u7684\u6307\u6570\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a<\/p>\n\n\n\n<p>**JDK7\u4e2dHashMap\u4f7f\u7528\u4e86\u94fe\u8868\u7684\u5934\u63d2\u6cd5\uff0c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u5bb9\u6613\u51fa\u73b0\u6b7b\u5faa\u73af\uff0cJDK8\u4ee5\u540e\u624d\u6709\u5c3e\u63d2\u6cd5\uff0c\u4e0d\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\u3002**\u5177\u4f53\u53ef\u770b\u8fd9\u7bc7&nbsp;<a href=\"https:\/\/mp.weixin.qq.com\/s\/ZyvrxC3gs92OEME3QzfF_Q\">\u9762\u8bd5\u5b98\uff1a HashMap \u4e3a\u4ec0\u4e48\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff1f<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#treeifybin-%E6%96%B9%E6%B3%95\"><\/a>treeifyBin \u65b9\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/**\n * Replaces all linked nodes in bin at index for given hash unless\n * table is too small, in which case resizes instead.\n *\/\nfinal void treeifyBin(Node&lt;K,V&gt;[] tab, int hash) {\n  int n, index; Node&lt;K,V&gt; e;\n  \/\/ \u5982\u679ctable\u4e3anull\u6216\u8005\u957f\u5ea6\u5c0f\u4e8e64\u65f6\u4e0d\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\uff0c\u6b64\u65f6\u8fdb\u884c\u6269\u5bb9\n  if (tab == null || (n = tab.length) &lt; MIN_TREEIFY_CAPACITY)\n    resize();\n  \/\/ \u5982\u679c\u6570\u7ec4\u8be5\u7d22\u5f15\u7684\u5143\u7d20\u4e0d\u4e3anull\n  else if ((e = tab[index = (n - 1) &amp; hash]) != null) {\n    TreeNode&lt;K,V&gt; hd = null, tl = null;\n    \/\/ \u5faa\u73af\u904d\u5386\u94fe\u8868\uff0c\u5c06\u94fe\u8868\u8f6c\u4e3a\u7ea2\u9ed1\u6811\n    do {\n      \/\/ \u6839\u636e\u94fe\u8868\u7684 node \u521b\u5efa TreeNode\n      TreeNode&lt;K,V&gt; p = replacementTreeNode(e, null);\n      if (tl == null)\n        hd = p;\n      else {\n        p.prev = tl;\n        tl.next = p;\n      }\n      tl = p;\n    } while ((e = e.next) != null);\n    if ((tab[index] = hd) != null)\n      hd.treeify(tab);\n  }\n}<\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8fd9\u91cc\u7528\u5230\u4e86&nbsp;<code>MIN_TREEIFY_CAPACITY(64)<\/code>&nbsp;\u53c2\u6570\uff0c\u5982\u679c\u94fe\u8868\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e8\uff0c\u4f46\u662fNode\u6570\u7ec4\u957f\u5ea6\u5c0f\u4e8e64\uff0c\u6b64\u65f6\u4e0d\u4f1a\u8f6c\u4e3a\u7ea2\u9ed1\u6811\uff0c\u800c\u662f\u8fdb\u884c&nbsp;<code>resize()<\/code>&nbsp;\u6269\u5bb9\uff0c\u7ea2\u9ed1\u6811\u867d\u7136\u67e5\u8be2\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(logN)\uff0c\u4f46\u662f\u7a7a\u95f4\u5360\u7528\u5927HashMap\u7684\u8bbe\u8ba1\u662f\u5c3d\u91cf\u4e0d\u7528\u7ea2\u9ed1\u6811\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#get-%E6%96%B9%E6%B3%95\"><\/a>get \u65b9\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">public V get(Object key) {\n  Node&lt;K,V&gt; e;\n  return (e = getNode(hash(key), key)) == null ? null : e.value;\n}\n\nfinal Node&lt;K,V&gt; getNode(int hash, Object key) {\n  Node&lt;K,V&gt;[] tab; Node&lt;K,V&gt; first, e; int n; K k;\n  \/\/ \u5224\u65adtable\u4e0d\u4e3anull\uff0c\u4e14table\u957f\u5ea6\u5927\u4e8e0\uff0c\u4e14\u5bf9\u5e94\u7d22\u5f15\u7684\u7b2c\u4e00\u4e2anode\u4e0d\u4e3anull\n  if ((tab = table) != null &amp;&amp; (n = tab.length) &gt; 0 &amp;&amp;\n      (first = tab[(n - 1) &amp; hash]) != null) {\n    if (first.hash == hash &amp;&amp; \/\/ always check first node\n        ((k = first.key) == key || (key != null &amp;&amp; key.equals(k))))\n      return first;\n    if ((e = first.next) != null) {\n      if (first instanceof TreeNode)\n        return ((TreeNode&lt;K,V&gt;)first).getTreeNode(hash, key);\n      do {\n        if (e.hash == hash &amp;&amp;\n            ((k = e.key) == key || (key != null &amp;&amp; key.equals(k))))\n          return e;\n      } while ((e = e.next) != null);\n    }\n  }\n  return null;\n}<\/pre>\n\n\n\n<p>\u901a\u8fc7\u6e90\u7801\u6211\u4eec\u6574\u7406\u51fa get \u7684\u6b65\u9aa4\u5927\u81f4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5224\u65adtable\u4e0d\u4e3anull\uff0c\u4e14table\u957f\u5ea6\u5927\u4e8e0\uff0c\u4e14\u5bf9\u5e94\u7d22\u5f15\u7684\u7b2c\u4e00\u4e2anode\u4e0d\u4e3anull\uff0c\u5982\u679c\u4e0d\u6ee1\u8db3\u76f4\u63a5\u8fd4\u56denull<\/li><li>\u5224\u65ad\u901a\u8fc7key\u8ba1\u7b97\u51fa\u7684hash\u4e0e\u5f53\u524d\u7d22\u5f15\u7b2c\u4e00\u4e2a\u8282\u70b9\u7684hash\u662f\u5426\u76f8\u540c\uff0c\u5e76\u4e14key\u662f\u5426\u76f8\u540c\uff08\u6ee1\u8db3equals\u65b9\u6cd5\u6216\u8005\u5f15\u7528\u76f8\u540c\uff09\uff0c\u76f8\u540c\u5219\u8fd4\u56de\u5f53\u524d\u7b2c\u4e00\u4e2a\u8282\u70b9\uff0c\u4e0d\u540c\u7ee7\u7eed\u4e0b\u9762\u7684\u6b65\u9aa4\u3002<\/li><li>\u82e5\u7b2c\u4e00\u4e2a\u8282\u70b9first\u4e3a\u7ea2\u9ed1\u6811\uff0c\u5219\u7528\u7ea2\u9ed1\u6811\u7b97\u6cd5\u53d6\u5bf9\u5e94value\uff0c\u82e5\u662f\u94fe\u8868\uff0c\u5219\u5faa\u73af\u94fe\u8868\uff0c\u6309\u7167\u4e0a\u9762\u540c\u6837\u7684\u65b9\u6cd5\uff0c\u60f3\u6bd4\u8f83hash\u662f\u5426\u76f8\u540c\uff0c\u5728\u6bd4\u8f83key\u662f\u5426\u76f8\u540c\u3002<\/li><\/ul>\n\n\n\n<p>\u7ea2\u9ed1\u6811\u67e5\u8be2\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(logN)<\/p>\n\n\n\n<p>\u94fe\u8868\u67e5\u8be2\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(N)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/CornPrincess\/Backend_Notes\/blob\/master\/notes\/Java\/Java%E5%AE%B9%E5%99%A8\/HashMap%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90.md#fail-fast\"><\/a>fail fast<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw&nbsp;<code>ConcurrentModificationException<\/code>&nbsp;on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness:&nbsp;<em>the fail-fast behavior of iterators should be used only to detect bugs.<\/em><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>HashMap\u6e90\u7801\u89e3\u6790(JDK11) \u6982\u89c8 HashMap \u6839\u636e key \u7684 hashCode \u503c\u8fdb\u884c\u50a8\u5b58\u6570\u636e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[46],"class_list":["post-1245","post","type-post","status-publish","format-standard","hentry","category-java-","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1245"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1245"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1245\/revisions"}],"predecessor-version":[{"id":1246,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1245\/revisions\/1246"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}