{"id":5004,"date":"2020-05-23T13:44:11","date_gmt":"2020-05-23T05:44:11","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5004"},"modified":"2020-05-23T13:44:12","modified_gmt":"2020-05-23T05:44:12","slug":"java%e9%9b%86%e5%90%88%e4%b8%80%e7%bd%91%e6%89%93%e5%b0%bd%ef%bc%81","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5004","title":{"rendered":"Java\u96c6\u5408\u4e00\u7f51\u6253\u5c3d\uff01"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u76f8\u4fe1\u5728\u9762\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u4e5f\u88ab\u95ee\u8fc7 Java \u96c6\u5408\u77e5\u8bc6\u70b9\uff0c\u6bd4\u5982\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u4e9b\u9898\uff01<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u8bf4\u8bf4\u5e38\u89c1\u7684\u96c6\u5408\u6709\u54ea\u4e9b\u5427\uff1f<\/p><p>HashMap\u8bf4\u4e00\u4e0b\uff0c\u5176\u4e2d\u7684Key\u9700\u8981\u91cd\u5199hashCode()\u548cequals()\u5417\uff1f<\/p><p>HashMap\u4e2dkey\u548cvalue\u53ef\u4ee5\u4e3anull\u5417\uff1f\u5141\u8bb8\u51e0\u4e2a\u4e3anull\u5440\uff1f<\/p><p>HashMap\u7ebf\u7a0b\u5b89\u5168\u5417?ConcurrentHashMap\u548chashTable\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/p><p>List\u548cSet\u8bf4\u4e00\u4e0b\uff0c\u73b0\u5728\u6709\u4e00\u4e2aArrayList\uff0c\u5bf9\u5176\u4e2d\u7684\u6240\u6709\u5143\u7d20\u6309\u7167\u67d0\u4e00\u5c5e\u6027\u5927\u5c0f\u6392\u5e8f\uff0c\u5e94\u8be5\u600e\u4e48\u505a\uff1f<\/p><p>ArrayList \u548c Vector \u7684\u533a\u522b<\/p><p>list \u53ef\u4ee5\u5220\u9664\u5417\uff0c\u904d\u5386\u7684\u65f6\u5019\u53ef\u4ee5\u5220\u9664\u5417\uff0c\u4e3a\u4ec0\u4e48<\/p><cite>Java \u96c6\u5408\u9762\u8bd5\u9898<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u5bf9\u4e8b\u7269\u7684\u4f53\u73b0\u90fd\u662f\u4ee5\u5bf9\u8c61\u7684\u5f62\u5f0f\uff0c\u6240\u4ee5\u4e3a\u4e86\u65b9\u4fbf\u5bf9\u591a\u4e2a\u5bf9\u8c61\u7684\u64cd\u4f5c\uff0c\u9700\u8981\u5c06\u5bf9\u8c61\u8fdb\u884c\u5b58\u50a8\uff0c\u96c6\u5408\u5c31\u662f\u5b58\u50a8\u5bf9\u8c61\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u4e5f\u53eb\u5bb9\u5668\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/00831rSTly1gdp03vldkkg30hv0gzwet.gif\" alt=\"img\" \/><figcaption>Java \u96c6\u5408\u6846\u67b6\u56fe<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u9762\u7684\u96c6\u5408\u6846\u67b6\u56fe\u53ef\u4ee5\u770b\u5230\uff0cJava \u96c6\u5408\u6846\u67b6\u4e3b\u8981\u5305\u62ec\u4e24\u79cd\u7c7b\u578b\u7684\u5bb9\u5668<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4e00\u79cd\u662f\u96c6\u5408\uff08Collection\uff09\uff0c\u5b58\u50a8\u4e00\u4e2a\u5143\u7d20\u96c6\u5408<\/li><li>\u53e6\u4e00\u79cd\u662f\u56fe\uff08Map\uff09\uff0c\u5b58\u50a8\u952e\/\u503c\u5bf9\u6620\u5c04\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Collection \u63a5\u53e3\u53c8\u6709 3 \u79cd\u5b50\u7c7b\u578b\uff0cList\u3001Set \u548c Queue\uff0c\u518d\u4e0b\u9762\u662f\u4e00\u4e9b\u62bd\u8c61\u7c7b\uff0c\u6700\u540e\u662f\u5177\u4f53\u5b9e\u73b0\u7c7b\uff0c\u5e38\u7528\u7684\u6709 ArrayList\u3001LinkedList\u3001HashSet\u3001LinkedHashSet\u3001HashMap\u3001LinkedHashMap \u7b49\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u96c6\u5408\u6846\u67b6\u662f\u4e00\u4e2a\u7528\u6765\u4ee3\u8868\u548c\u64cd\u7eb5\u96c6\u5408\u7684\u7edf\u4e00\u67b6\u6784\u3002\u6240\u6709\u7684\u96c6\u5408\u6846\u67b6\u90fd\u5305\u542b\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>\u63a5\u53e3<\/strong>\uff1a\u662f\u4ee3\u8868\u96c6\u5408\u7684\u62bd\u8c61\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982 Collection\u3001List\u3001Set\u3001Map \u7b49\u3002\u4e4b\u6240\u4ee5\u5b9a\u4e49\u591a\u4e2a\u63a5\u53e3\uff0c\u662f\u4e3a\u4e86\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u64cd\u4f5c\u96c6\u5408\u5bf9\u8c61<\/li><li><strong>\u5b9e\u73b0\uff08\u7c7b\uff09<\/strong>\uff1a\u662f\u96c6\u5408\u63a5\u53e3\u7684\u5177\u4f53\u5b9e\u73b0\u3002\u4ece\u672c\u8d28\u4e0a\u8bb2\uff0c\u5b83\u4eec\u662f\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\uff1aArrayList\u3001LinkedList\u3001HashSet\u3001HashMap\u3002<\/li><li><strong>\u7b97\u6cd5<\/strong>\uff1a\u662f\u5b9e\u73b0\u96c6\u5408\u63a5\u53e3\u7684\u5bf9\u8c61\u91cc\u7684\u65b9\u6cd5\u6267\u884c\u7684\u4e00\u4e9b\u6709\u7528\u7684\u8ba1\u7b97\uff0c\u4f8b\u5982\uff1a\u641c\u7d22\u548c\u6392\u5e8f\u3002\u8fd9\u4e9b\u7b97\u6cd5\u88ab\u79f0\u4e3a\u591a\u6001\uff0c\u90a3\u662f\u56e0\u4e3a\u76f8\u540c\u7684\u65b9\u6cd5\u53ef\u4ee5\u5728\u76f8\u4f3c\u7684\u63a5\u53e3\u4e0a\u6709\u7740\u4e0d\u540c\u7684\u5b9e\u73b0\u3002<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bf4\u8bf4\u5e38\u7528\u7684\u96c6\u5408\u6709\u54ea\u4e9b\u5427\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Map \u63a5\u53e3\u548c Collection \u63a5\u53e3\u662f\u6240\u6709\u96c6\u5408\u6846\u67b6\u7684\u7236\u63a5\u53e3\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Collection\u63a5\u53e3\u7684\u5b50\u63a5\u53e3\u5305\u62ec\uff1aSet\u3001List\u3001Queue<\/li><li>List\u662f\u6709\u5e8f\u7684\u5141\u8bb8\u6709\u91cd\u590d\u5143\u7d20\u7684Collection\uff0c\u5b9e\u73b0\u7c7b\u4e3b\u8981\u6709\uff1aArrayList\u3001LinkedList\u3001Stack\u4ee5\u53caVector\u7b49<\/li><li>Set\u662f\u4e00\u79cd\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\u4e14\u65e0\u5e8f\u7684Collection\uff0c\u5b9e\u73b0\u7c7b\u4e3b\u8981\u6709\uff1aHashSet\u3001TreeSet\u3001LinkedHashSet\u7b49<\/li><li>Map\u6ca1\u6709\u7ee7\u627fCollection\u63a5\u53e3\uff0cMap\u63d0\u4f9bkey\u5230value\u7684\u6620\u5c04\u3002\u5b9e\u73b0\u7c7b\u4e3b\u8981\u6709\uff1aHashMap\u3001TreeMap\u3001Hashtable\u3001ConcurrentHashMap \u4ee5\u53ca Properties \u7b49<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">ArrayList \u548c Vector \u7684\u533a\u522b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f8\u540c\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>ArrayList \u548c Vector \u90fd\u662f\u7ee7\u627f\u4e86\u76f8\u540c\u7684\u7236\u7c7b\u548c\u5b9e\u73b0\u4e86\u76f8\u540c\u7684\u63a5\u53e3\uff08\u90fd\u5b9e\u73b0\u4e86List\uff0c\u6709\u5e8f\u3001\u5141\u8bb8\u91cd\u590d\u548cnull\uff09<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>extends AbstractList&lt;E&gt;\n        implements List&lt;E&gt;, RandomAccess, Cloneable, java.io.Serializable<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5e95\u5c42\u90fd\u662f\u6570\u7ec4\uff08Object[]\uff09\u5b9e\u73b0\u7684<\/li><li>\u521d\u59cb\u9ed8\u8ba4\u957f\u5ea6\u90fd\u4e3a<strong>10<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u540c\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u540c\u6b65\u6027\uff1aVector \u4e2d\u7684 public \u65b9\u6cd5\u591a\u6570\u6dfb\u52a0\u4e86 synchronized \u5173\u952e\u5b57\u3001\u4ee5\u786e\u4fdd\u65b9\u6cd5\u540c\u6b65\u3001\u4e5f\u5373\u662f Vector \u7ebf\u7a0b\u5b89\u5168\u3001ArrayList \u7ebf\u7a0b\u4e0d\u5b89\u5168<\/li><li>\u6027\u80fd\uff1aVector \u5b58\u5728 synchronized \u7684\u9501\u7b49\u5f85\u60c5\u51b5\u3001\u9700\u8981\u7b49\u5f85\u91ca\u653e\u9501\u8fd9\u4e2a\u8fc7\u7a0b\u3001\u6240\u4ee5\u6027\u80fd\u76f8\u5bf9\u8f83\u5dee<\/li><li>\u6269\u5bb9\u5927\u5c0f\uff1aArrayList\u5728\u5e95\u5c42\u6570\u7ec4\u4e0d\u591f\u7528\u65f6\u5728\u539f\u6765\u7684\u57fa\u7840\u4e0a\u6269\u5c55 0.5 \u500d\uff0cVector\u9ed8\u8ba4\u662f\u6269\u5c55 1 \u500d<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6269\u5bb9\u673a\u5236\uff0c\u6269\u5bb9\u65b9\u6cd5\u5176\u5b9e\u5c31\u662f\u65b0\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u7136\u540e\u5c06\u65e7\u6570\u7ec4\u7684\u5143\u7d20\u90fd\u590d\u5236\u5230\u65b0\u6570\u7ec4\u91cc\u9762\u3002\u5176\u5e95\u5c42\u7684\u6269\u5bb9\u65b9\u6cd5\u90fd\u5728&nbsp;<strong>grow()<\/strong>&nbsp;\u4e2d\uff08\u57fa\u4e8eJDK8\uff09<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>ArrayList \u7684 grow()\uff0c\u5728\u6ee1\u8db3\u6269\u5bb9\u6761\u4ef6\u65f6\u3001ArrayList\u4ee5<strong>1.5<\/strong>\u00a0\u500d\u7684\u65b9\u5f0f\u5728\u6269\u5bb9\uff08oldCapacity &gt;&gt;\u00a0<strong>1<\/strong> \uff0c\u53f3\u79fb\u8fd0\u7b97\uff0c\u76f8\u5f53\u4e8e\u9664\u4ee5 2\uff0c\u7ed3\u679c\u4e3a\u4e8c\u5206\u4e4b\u4e00\u7684 oldCapacity\uff09<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>private void grow(int minCapacity) {\n    \/\/ overflow-conscious code\n    int oldCapacity = elementData.length;\n    \/\/newCapacity = oldCapacity + O.5*oldCapacity,\u6b64\u5904\u6269\u5bb90.5\u500d\n    int newCapacity = oldCapacity + (oldCapacity &gt;&gt; 1); \n    if (newCapacity - minCapacity &lt; 0)\n        newCapacity = minCapacity;\n    if (newCapacity - MAX_ARRAY_SIZE &gt; 0)\n        newCapacity = hugeCapacity(minCapacity);\n    \/\/ minCapacity is usually close to size, so this is a win:\n    elementData = Arrays.copyOf(elementData, newCapacity);\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Vector \u7684 grow()\uff0cVector \u6bd4 ArrayList\u591a\u4e00\u4e2a\u5c5e\u6027\uff0c\u6269\u5c55\u56e0\u5b50capacityIncrement\uff0c\u53ef\u4ee5\u6269\u5bb9\u5927\u5c0f\u3002\u5f53\u6269\u5bb9\u5bb9\u91cf\u589e\u91cf\u5927\u4e8e<strong>0<\/strong>\u65f6\u3001\u65b0\u6570\u7ec4\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4\u957f\u5ea6<strong>+<\/strong>\u6269\u5bb9\u5bb9\u91cf\u589e\u91cf\u3001\u5426\u5219\u65b0\u6570\u7ec4\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4\u957f\u5ea6\u7684<strong>2<\/strong>\u500d<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>private void grow(int minCapacity) {\n    \/\/ overflow-conscious code\n    int oldCapacity = elementData.length;\n    \/\/\n    int newCapacity = oldCapacity + ((capacityIncrement &gt; 0) ?\n                                     capacityIncrement : oldCapacity);\n    if (newCapacity - minCapacity &lt; 0)\n        newCapacity = minCapacity;\n    if (newCapacity - MAX_ARRAY_SIZE &gt; 0)\n        newCapacity = hugeCapacity(minCapacity);\n    elementData = Arrays.copyOf(elementData, newCapacity);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">ArrayList \u4e0e LinkedList \u533a\u522b<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u662f\u5426\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff1a ArrayList \u548c LinkedList \u90fd\u662f\u4e0d\u540c\u6b65\u7684\uff0c\u4e5f\u5c31\u662f\u4e0d\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff1b<\/li><li><strong>\u5e95\u5c42\u6570\u636e\u7ed3\u6784<\/strong>\uff1a Arraylist \u5e95\u5c42\u4f7f\u7528\u7684\u662f Object \u6570\u7ec4\uff1bLinkedList \u5e95\u5c42\u4f7f\u7528\u7684\u662f\u53cc\u5411\u5faa\u73af\u94fe\u8868\u6570\u636e\u7ed3\u6784\uff1b<\/li><li><strong>\u63d2\u5165\u548c\u5220\u9664\u662f\u5426\u53d7\u5143\u7d20\u4f4d\u7f6e\u7684\u5f71\u54cd\uff1a<\/strong><ul><li><strong>ArrayList \u91c7\u7528\u6570\u7ec4\u5b58\u50a8\uff0c\u6240\u4ee5\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53d7\u5143\u7d20\u4f4d\u7f6e\u7684\u5f71\u54cd\u3002<\/strong>&nbsp;\u6bd4\u5982\uff1a\u6267\u884c&nbsp;<code>add(E e)<\/code>\u65b9\u6cd5\u7684\u65f6\u5019\uff0c ArrayList \u4f1a\u9ed8\u8ba4\u5728\u5c06\u6307\u5b9a\u7684\u5143\u7d20\u8ffd\u52a0\u5230\u6b64\u5217\u8868\u7684\u672b\u5c3e\uff0c\u8fd9\u79cd\u60c5\u51b5\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u662fO(1)\u3002\u4f46\u662f\u5982\u679c\u8981\u5728\u6307\u5b9a\u4f4d\u7f6e i \u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u7684\u8bdd\uff08&nbsp;<code>add(intindex,E element)<\/code>\uff09\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u4e3a O(n-i)\u3002\u56e0\u4e3a\u5728\u8fdb\u884c\u4e0a\u8ff0\u64cd\u4f5c\u7684\u65f6\u5019\u96c6\u5408\u4e2d\u7b2c i \u548c\u7b2c i \u4e2a\u5143\u7d20\u4e4b\u540e\u7684(n-i)\u4e2a\u5143\u7d20\u90fd\u8981\u6267\u884c\u5411\u540e\u4f4d\/\u5411\u524d\u79fb\u4e00\u4f4d\u7684\u64cd\u4f5c\u3002<\/li><li><strong>LinkedList \u91c7\u7528\u94fe\u8868\u5b58\u50a8\uff0c\u6240\u4ee5\u63d2\u5165\uff0c\u5220\u9664\u5143\u7d20\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u53d7\u5143\u7d20\u4f4d\u7f6e\u7684\u5f71\u54cd\uff0c\u90fd\u662f\u8fd1\u4f3c $O(1)$\uff0c\u800c\u6570\u7ec4\u4e3a\u8fd1\u4f3c $O(n)$\u3002<\/strong><\/li><li>ArrayList \u4e00\u822c\u5e94\u7528\u4e8e\u67e5\u8be2\u8f83\u591a\u4f46\u63d2\u5165\u4ee5\u53ca\u5220\u9664\u8f83\u5c11\u60c5\u51b5\uff0c\u5982\u679c\u63d2\u5165\u4ee5\u53ca\u5220\u9664\u8f83\u591a\u5219\u5efa\u8bae\u4f7f\u7528 LinkedList<\/li><\/ul><\/li><li><strong>\u662f\u5426\u652f\u6301\u5feb\u901f\u968f\u673a\u8bbf\u95ee<\/strong>\uff1a LinkedList \u4e0d\u652f\u6301\u9ad8\u6548\u7684\u968f\u673a\u5143\u7d20\u8bbf\u95ee\uff0c\u800c ArrayList \u5b9e\u73b0\u4e86 RandomAccess \u63a5\u53e3\uff0c\u6240\u4ee5\u6709\u968f\u673a\u8bbf\u95ee\u529f\u80fd\u3002\u5feb\u901f\u968f\u673a\u8bbf\u95ee\u5c31\u662f\u901a\u8fc7\u5143\u7d20\u7684\u5e8f\u53f7\u5feb\u901f\u83b7\u53d6\u5143\u7d20\u5bf9\u8c61(\u5bf9\u5e94\u4e8e&nbsp;<code>get(intindex)<\/code>\u65b9\u6cd5)\u3002<\/li><li><strong>\u5185\u5b58\u7a7a\u95f4\u5360\u7528<\/strong>\uff1a ArrayList \u7684\u7a7a\u95f4\u6d6a\u8d39\u4e3b\u8981\u4f53\u73b0\u5728\u5728 list \u5217\u8868\u7684\u7ed3\u5c3e\u4f1a\u9884\u7559\u4e00\u5b9a\u7684\u5bb9\u91cf\u7a7a\u95f4\uff0c\u800c LinkedList \u7684\u7a7a\u95f4\u82b1\u8d39\u5219\u4f53\u73b0\u5728\u5b83\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u9700\u8981\u6d88\u8017\u6bd4 ArrayList \u66f4\u591a\u7684\u7a7a\u95f4\uff08\u56e0\u4e3a\u8981\u5b58\u653e\u76f4\u63a5\u540e\u7ee7\u548c\u76f4\u63a5\u524d\u9a71\u4ee5\u53ca\u6570\u636e\uff09\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ad8\u7ea7\u5de5\u7a0b\u5e08\u7684\u6211\uff0c\u53ef\u4e0d\u5f97\u770b\u770b\u6e90\u7801\uff0c\u5177\u4f53\u5206\u6790\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>ArrayList\u5de5\u4f5c\u539f\u7406\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u5e95\u5c42\u662f\u52a8\u6001\u6570\u7ec4\uff0c\u6bcf\u6b21\u521b\u5efa\u4e00\u4e2a ArrayList \u5b9e\u4f8b\u65f6\u4f1a\u5206\u914d\u4e00\u4e2a\u521d\u59cb\u5bb9\u91cf\uff08\u6ca1\u6709\u6307\u5b9a\u521d\u59cb\u5bb9\u91cf\u7684\u8bdd\uff0c\u9ed8\u8ba4\u662f 10\uff09\uff0c\u4ee5add\u65b9\u6cd5\u4e3a\u4f8b\uff0c\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u521d\u59cb\u5bb9\u91cf\uff0c\u5f53\u6267\u884cadd\u65b9\u6cd5\uff0c\u5148\u5224\u65ad\u5f53\u524d\u6570\u7ec4\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u7ed9\u4fdd\u5b58\u5bf9\u8c61\u7684\u6570\u7ec4\u5206\u914d\u4e00\u4e2a\u6700\u5c0f\u5bb9\u91cf\uff0c\u9ed8\u8ba4\u4e3a10\u3002\u5f53\u6dfb\u52a0\u5927\u5bb9\u91cf\u5143\u7d20\u65f6\uff0c\u4f1a\u5148\u589e\u52a0\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u4ee5\u63d0\u9ad8\u6dfb\u52a0\u7684\u6548\u7387\uff1b<\/li><li>LinkedList \u662f\u6709\u5e8f\u5e76\u4e14\u652f\u6301\u5143\u7d20\u91cd\u590d\u7684\u96c6\u5408\uff0c\u5e95\u5c42\u662f\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u7684\uff0c\u5373\u6bcf\u4e2a\u8282\u70b9\u65e2\u5305\u542b\u6307\u5411\u5176\u540e\u7ee7\u7684\u5f15\u7528\u4e5f\u5305\u62ec\u6307\u5411\u5176\u524d\u9a71\u7684\u5f15\u7528\u3002\u94fe\u8868\u65e0\u5bb9\u91cf\u9650\u5236\uff0c\u4f46\u53cc\u5411\u94fe\u8868\u672c\u8eab\u4f7f\u7528\u4e86\u66f4\u591a\u7a7a\u95f4\uff0c\u4e5f\u9700\u8981\u989d\u5916\u7684\u94fe\u8868\u6307\u9488\u64cd\u4f5c\u3002\u6309\u4e0b\u6807\u8bbf\u95ee\u5143\u7d20&nbsp;<code>get(i)\/set(i,e)<\/code>&nbsp;\u8981\u60b2\u5267\u7684\u904d\u5386\u94fe\u8868\u5c06\u6307\u9488\u79fb\u52a8\u5230\u4f4d(\u5982\u679ci&gt;\u6570\u7ec4\u5927\u5c0f\u7684\u4e00\u534a\uff0c\u4f1a\u4ece\u672b\u5c3e\u79fb\u8d77)\u3002\u63d2\u5165\u3001\u5220\u9664\u5143\u7d20\u65f6\u4fee\u6539\u524d\u540e\u8282\u70b9\u7684\u6307\u9488\u5373\u53ef\uff0c\u4f46\u8fd8\u662f\u8981\u904d\u5386\u90e8\u5206\u94fe\u8868\u7684\u6307\u9488\u624d\u80fd\u79fb\u52a8\u5230\u4e0b\u6807\u6240\u6307\u7684\u4f4d\u7f6e\uff0c\u53ea\u6709\u5728\u94fe\u8868\u4e24\u5934\u7684\u64cd\u4f5c<code>add()<\/code>\uff0c&nbsp;<code>addFirst()<\/code>\uff0c<code>removeLast()<\/code>\u6216\u7528&nbsp;<code>iterator()<\/code>&nbsp;\u4e0a\u7684&nbsp;<code>remove()<\/code>&nbsp;\u80fd\u7701\u6389\u6307\u9488\u7684\u79fb\u52a8\u3002\u6b64\u5916 LinkedList \u8fd8\u5b9e\u73b0\u4e86 Deque\uff08\u7ee7\u627f\u81eaQueue\u63a5\u53e3\uff09\u63a5\u53e3\uff0c\u53ef\u4ee5\u5f53\u505a\u961f\u5217\u4f7f\u7528\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u4f1a\u56ca\u62ec\u6240\u6709\u65b9\u6cd5\uff0c\u53ea\u662f\u4e3a\u4e86\u5b66\u4e60\uff0c\u8bb0\u5f55\u601d\u60f3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ArrayList \u548c LinkedList \u4e24\u8005\u90fd\u5b9e\u73b0\u4e86 List \u63a5\u53e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ArrayList&lt;E&gt; extends AbstractList&lt;E&gt;\n        implements List&lt;E&gt;, RandomAccess, Cloneable, java.io.Serializable{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class LinkedList&lt;E&gt;\n    extends AbstractSequentialList&lt;E&gt;\n    implements List&lt;E&gt;, Deque&lt;E&gt;, Cloneable, java.io.Serializable<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6784\u9020\u5668<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ArrayList \u63d0\u4f9b\u4e86 3 \u4e2a\u6784\u9020\u5668\uff0c\u2460\u65e0\u53c2\u6784\u9020\u5668 \u2461\u5e26\u521d\u59cb\u5bb9\u91cf\u6784\u9020\u5668 \u2462\u53c2\u6570\u4e3a\u96c6\u5408\u6784\u9020\u5668<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ArrayList&lt;E&gt; extends AbstractList&lt;E&gt;\n        implements List&lt;E&gt;, RandomAccess, Cloneable, java.io.Serializable{\n   \npublic ArrayList(int initialCapacity) {\n   if (initialCapacity &gt; 0) {\n       \/\/ \u521b\u5efa\u521d\u59cb\u5bb9\u91cf\u7684\u6570\u7ec4\n     this.elementData = new Object[initialCapacity];\n    } else if (initialCapacity == 0) {\n     this.elementData = EMPTY_ELEMENTDATA;\n    } else {\n   throw new IllegalArgumentException(\"Illegal Capacity: \"+initialCapacity);\n  }\n}\npublic ArrayList() {\n  \/\/ \u9ed8\u8ba4\u4e3a\u7a7a\u6570\u7ec4\n  this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;\n}\n    \npublic ArrayList(Collection&lt;? extends E&gt; c) { \/\/...}       \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">LinkedList \u63d0\u4f9b\u4e86 2 \u4e2a\u6784\u9020\u5668\uff0c\u56e0\u4e3a\u57fa\u4e8e\u94fe\u8868\uff0c\u6240\u4ee5\u4e5f\u5c31\u6ca1\u6709\u521d\u59cb\u5316\u5927\u5c0f\uff0c\u4e5f\u6ca1\u6709\u6269\u5bb9\u7684\u673a\u5236\uff0c\u5c31\u662f\u4e00\u76f4\u5728\u524d\u9762\u6216\u8005\u540e\u9762\u63d2\u63d2\u63d2~~<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public LinkedList() {\n}\n\npublic LinkedList(Collection&lt;? extends E&gt; c) {\n    this();\n    addAll(c);\n}\n\/\/ LinkedList \u65e2\u7136\u4f5c\u4e3a\u94fe\u8868\uff0c\u90a3\u4e48\u80af\u5b9a\u4f1a\u6709\u8282\u70b9\nprivate static class Node&lt;E&gt; {\n    E item;\n    Node&lt;E&gt; next;\n    Node&lt;E&gt; prev;\n\n    Node(Node&lt;E&gt; prev, E element, Node&lt;E&gt; next) {\n        this.item = element;\n        this.next = next;\n        this.prev = prev;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u63d2\u5165<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ArrayList<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public boolean add(E e) {\n    \/\/ \u786e\u4fdd\u6570\u7ec4\u7684\u5bb9\u91cf\uff0c\u4fdd\u8bc1\u53ef\u4ee5\u6dfb\u52a0\u8be5\u5143\u7d20\n    ensureCapacityInternal(size + 1);  \/\/ Increments modCount!!\n    \/\/ \u5c06\u8be5\u5143\u7d20\u653e\u5165\u6570\u7ec4\u4e2d\n    elementData[size++] = e;\n    return true;\n}\nprivate void ensureCapacityInternal(int minCapacity) {\n    \/\/ \u5982\u679c\u6570\u7ec4\u662f\u7a7a\u7684\uff0c\u90a3\u4e48\u4f1a\u521d\u59cb\u5316\u8be5\u6570\u7ec4\n    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {\n        \/\/ DEFAULT_CAPACITY \u4e3a 10,\u6240\u4ee5\u8c03\u7528\u65e0\u53c2\u9ed8\u8ba4 ArrayList \u6784\u9020\u65b9\u6cd5\u521d\u59cb\u5316\u7684\u8bdd\uff0c\u9ed8\u8ba4\u7684\u6570\u7ec4\u5bb9\u91cf\u4e3a 10\n        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);\n    }\n\n    ensureExplicitCapacity(minCapacity);\n}\n\nprivate void ensureExplicitCapacity(int minCapacity) {\n    modCount++;\n\n    \/\/ \u786e\u4fdd\u6570\u7ec4\u7684\u5bb9\u91cf\uff0c\u5982\u679c\u4e0d\u591f\u7684\u8bdd\uff0c\u8c03\u7528 grow \u65b9\u6cd5\u6269\u5bb9\n    if (minCapacity - elementData.length &gt; 0)\n        grow(minCapacity);\n}\n\/\/\u6269\u5bb9\u5177\u4f53\u7684\u65b9\u6cd5\nprivate void grow(int minCapacity) {\n    \/\/ \u5f53\u524d\u6570\u7ec4\u7684\u5bb9\u91cf\n    int oldCapacity = elementData.length;\n    \/\/ \u65b0\u6570\u7ec4\u6269\u5bb9\u4e3a\u539f\u6765\u5bb9\u91cf\u7684 1.5 \u500d\n    int newCapacity = oldCapacity + (oldCapacity &gt;&gt; 1);\n    \/\/ \u5982\u679c\u65b0\u6570\u7ec4\u6269\u5bb9\u5bb9\u91cf\u8fd8\u662f\u6bd4\u6700\u5c11\u9700\u8981\u7684\u5bb9\u91cf\u8fd8\u8981\u5c0f\u7684\u8bdd\uff0c\u5c31\u8bbe\u7f6e\u6269\u5145\u5bb9\u91cf\u4e3a\u6700\u5c0f\u9700\u8981\u7684\u5bb9\u91cf\n    if (newCapacity - minCapacity &lt; 0)\n        newCapacity = minCapacity;\n    \/\/\u5224\u65ad\u65b0\u6570\u7ec4\u5bb9\u91cf\u662f\u5426\u5df2\u7ecf\u8d85\u51fa\u6700\u5927\u6570\u7ec4\u8303\u56f4\uff0cMAX_ARRAY_SIZE = Integer.MAX_VALUE - 8\n    if (newCapacity - MAX_ARRAY_SIZE &gt; 0)\n        newCapacity = hugeCapacity(minCapacity);\n    \/\/ minCapacity is usually close to size, so this is a win:\n    \/\/ \u590d\u5236\u5143\u7d20\u5230\u65b0\u7684\u6570\u7ec4\u4e2d\n    elementData = Arrays.copyOf(elementData, newCapacity);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u7136\u4e5f\u53ef\u4ee5\u63d2\u5165\u6307\u5b9a\u4f4d\u7f6e\uff0c\u8fd8\u6709\u4e00\u4e2a\u91cd\u8f7d\u7684\u65b9\u6cd5&nbsp;<code>add(int index, E element)<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void add(int index, E element) {\n    \/\/ \u5224\u65ad index \u6709\u6ca1\u6709\u8d85\u51fa\u7d22\u5f15\u7684\u8303\u56f4\n    rangeCheckForAdd(index);\n    \/\/ \u548c\u4e4b\u524d\u7684\u64cd\u4f5c\u662f\u4e00\u6837\u7684\uff0c\u90fd\u662f\u4fdd\u8bc1\u6570\u7ec4\u7684\u5bb9\u91cf\u8db3\u591f\n    ensureCapacityInternal(size + 1);  \/\/ Increments modCount!!\n    \/\/ \u5c06\u6307\u5b9a\u4f4d\u7f6e\u53ca\u5176\u540e\u9762\u6570\u636e\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n    System.arraycopy(elementData, index, elementData, index + 1,\n                     size - index);\n    \/\/ \u5c06\u8be5\u5143\u7d20\u6dfb\u52a0\u5230\u6307\u5b9a\u7684\u6570\u7ec4\u4f4d\u7f6e\n    elementData[index] = element;\n    \/\/ ArrayList \u7684\u5927\u5c0f\u6539\u53d8\n    size++;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\u6bcf\u6b21\u63d2\u5165\u6307\u5b9a\u4f4d\u7f6e\u90fd\u8981\u79fb\u52a8\u5143\u7d20\uff0c\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u6765\u770b&nbsp;<strong>LinkedList<\/strong>&nbsp;\u7684\u63d2\u5165\uff0c\u4e5f\u6709\u63d2\u5165\u672b\u5c3e\uff0c\u63d2\u5165\u6307\u5b9a\u4f4d\u7f6e\u4e24\u79cd\uff0c\u7531\u4e8e\u57fa\u4e8e\u94fe\u8868\uff0c\u80af\u5b9a\u5f97\u5148\u6709\u4e2a Node<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static class Node&lt;E&gt; {\n    E item;\n    Node&lt;E&gt; next;\n    Node&lt;E&gt; prev;\n\n    Node(Node&lt;E&gt; prev, E element, Node&lt;E&gt; next) {\n        this.item = element;\n        this.next = next;\n        this.prev = prev;\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public boolean add(E e) {\n    \/\/ \u76f4\u63a5\u5f80\u961f\u5c3e\u52a0\u5143\u7d20\n    linkLast(e);\n    return true;\n}\n\nvoid linkLast(E e) {\n    \/\/ \u4fdd\u5b58\u539f\u6765\u94fe\u8868\u5c3e\u90e8\u8282\u70b9\uff0clast \u662f\u5168\u5c40\u53d8\u91cf\uff0c\u7528\u6765\u8868\u793a\u961f\u5c3e\u5143\u7d20\n    final Node&lt;E&gt; l = last;\n    \/\/ \u4e3a\u8be5\u5143\u7d20 e \u65b0\u5efa\u4e00\u4e2a\u8282\u70b9\n    final Node&lt;E&gt; newNode = new Node&lt;&gt;(l, e, null);\n    \/\/ \u5c06\u65b0\u8282\u70b9\u8bbe\u4e3a\u961f\u5c3e\n    last = newNode;\n    \/\/ \u5982\u679c\u539f\u6765\u7684\u961f\u5c3e\u5143\u7d20\u4e3a\u7a7a\uff0c\u90a3\u4e48\u8bf4\u660e\u539f\u6765\u7684\u6574\u4e2a\u5217\u8868\u662f\u7a7a\u7684\uff0c\u5c31\u628a\u65b0\u8282\u70b9\u8d4b\u503c\u7ed9\u5934\u7ed3\u70b9\n    if (l == null)\n        first = newNode;\n    else\n    \/\/ \u539f\u6765\u5c3e\u7ed3\u70b9\u7684\u540e\u9762\u4e3a\u65b0\u751f\u6210\u7684\u7ed3\u70b9\n        l.next = newNode;\n    \/\/ \u8282\u70b9\u6570 +1\n    size++;\n    modCount++;\n}\n\npublic void add(int index, E element) {\n    \/\/ \u68c0\u67e5 index \u6709\u6ca1\u6709\u8d85\u51fa\u7d22\u5f15\u8303\u56f4\n    checkPositionIndex(index);\n    \/\/ \u5982\u679c\u8ffd\u52a0\u5230\u5c3e\u90e8\uff0c\u90a3\u4e48\u5c31\u8ddf add(E e) \u4e00\u6837\u4e86\n    if (index == size)\n        linkLast(element);\n    else\n    \/\/ \u5426\u5219\u5c31\u662f\u63d2\u5728\u5176\u4ed6\u4f4d\u7f6e\n     linkBefore(element, node(index));\n}\n\n\/\/linkBefore\u65b9\u6cd5\u4e2d\u8c03\u7528\u4e86\u8fd9\u4e2anode\u65b9\u6cd5\uff0c\u7c7b\u4f3c\u4e8c\u5206\u67e5\u627e\u7684\u4f18\u5316\nNode&lt;E&gt; node(int index) {\n    \/\/ assert isElementIndex(index);\n    \/\/ \u5982\u679c index \u5728\u524d\u534a\u6bb5\uff0c\u4ece\u524d\u5f80\u540e\u904d\u5386\u83b7\u53d6 node\n    if (index &lt; (size &gt;&gt; 1)) {\n        Node&lt;E&gt; x = first;\n        for (int i = 0; i &lt; index; i++)\n            x = x.next;\n        return x;\n    } else {\n        \/\/ \u5982\u679c index \u5728\u540e\u534a\u6bb5\uff0c\u4ece\u540e\u5f80\u524d\u904d\u5386\u83b7\u53d6 node\n        Node&lt;E&gt; x = last;\n        for (int i = size - 1; i &gt; index; i--)\n            x = x.prev;\n        return x;\n    }\n}\n\nvoid linkBefore(E e, Node&lt;E&gt; succ) {\n    \/\/ assert succ != null;\n    \/\/ \u4fdd\u5b58 index \u8282\u70b9\u7684\u524d\u8282\u70b9\n    final Node&lt;E&gt; pred = succ.prev;\n    \/\/ \u65b0\u5efa\u4e00\u4e2a\u76ee\u6807\u8282\u70b9\n    final Node&lt;E&gt; newNode = new Node&lt;&gt;(pred, e, succ);\n    succ.prev = newNode;\n    \/\/ \u5982\u679c\u662f\u5728\u5f00\u5934\u5904\u63d2\u5165\u7684\u8bdd\n    if (pred == null)\n        first = newNode;\n    else\n        pred.next = newNode;\n    size++;\n    modCount++;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u83b7\u53d6<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ArrayList<\/strong>&nbsp;\u7684 get() \u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u5728\u6570\u7ec4\u4e2d\u8fd4\u56de\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u5373\u53ef\uff0c\u6240\u4ee5\u6548\u7387\u5f88\u9ad8<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public E get(int index) {\n    \/\/ \u68c0\u67e5 index \u6709\u6ca1\u6709\u8d85\u51fa\u7d22\u5f15\u7684\u8303\u56f4\n    rangeCheck(index);\n    \/\/ \u8fd4\u56de\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\n    return elementData(index);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>LinkedList<\/strong>&nbsp;\u7684 get() \u65b9\u6cd5\uff0c\u5c31\u662f\u5728\u5185\u90e8\u8c03\u7528\u4e86\u4e0a\u8fb9\u770b\u5230\u7684 node() \u65b9\u6cd5\uff0c\u5224\u65ad\u5728\u524d\u534a\u6bb5\u8fd8\u662f\u5728\u540e\u534a\u6bb5\uff0c\u7136\u540e\u904d\u5386\u5f97\u5230\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public E get(int index) {\n    checkElementIndex(index);\n    return node(index).item;\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">HashMap\u7684\u5e95\u5c42\u5b9e\u73b0<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u4ec0\u4e48\u65f6\u5019\u4f1a\u4f7f\u7528HashMap\uff1f\u4ed6\u6709\u4ec0\u4e48\u7279\u70b9\uff1f<\/p><p>\u4f60\u77e5\u9053HashMap\u7684\u5de5\u4f5c\u539f\u7406\u5417\uff1f<\/p><p>\u4f60\u77e5\u9053get\u548cput\u7684\u539f\u7406\u5417\uff1fequals()\u548chashCode()\u7684\u90fd\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f<\/p><p>\u4f60\u77e5\u9053hash\u7684\u5b9e\u73b0\u5417\uff1f\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u5b9e\u73b0\uff1f<\/p><p>\u5982\u679cHashMap\u7684\u5927\u5c0f\u8d85\u8fc7\u4e86\u8d1f\u8f7d\u56e0\u5b50(load factor)\u5b9a\u4e49\u7684\u5bb9\u91cf\uff0c\u600e\u4e48\u529e\uff1f<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">HashMap \u5728 JDK 7 \u548c JDK8 \u4e2d\u7684\u5b9e\u73b0\u65b9\u5f0f\u7565\u6709\u4e0d\u540c\u3002\u5206\u5f00\u8bb0\u5f55\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6df1\u5165 HahsMap \u4e4b\u524d\uff0c\u5148\u8981\u4e86\u89e3\u7684\u6982\u5ff5<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>initialCapacity\uff1a\u521d\u59cb\u5bb9\u91cf\u3002\u6307\u7684\u662f HashMap \u96c6\u5408\u521d\u59cb\u5316\u7684\u65f6\u5019\u81ea\u8eab\u7684\u5bb9\u91cf\u3002\u53ef\u4ee5\u5728\u6784\u9020\u65b9\u6cd5\u4e2d\u6307\u5b9a\uff1b\u5982\u679c\u4e0d\u6307\u5b9a\u7684\u8bdd\uff0c\u603b\u5bb9\u91cf\u9ed8\u8ba4\u503c\u662f 16 \u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u521d\u59cb\u5bb9\u91cf\u5fc5\u987b\u662f 2 \u7684\u5e42\u6b21\u65b9\u3002\uff08<strong>1.7\u4e2d\uff0c\u5df2\u77e5HashMap\u4e2d\u5c06\u8981\u5b58\u653e\u7684KV\u4e2a\u6570\u7684\u65f6\u5019\uff0c\u8bbe\u7f6e\u4e00\u4e2a\u5408\u7406\u7684\u521d\u59cb\u5316\u5bb9\u91cf\u53ef\u4ee5\u6709\u6548\u7684\u63d0\u9ad8\u6027\u80fd<\/strong>\uff09<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>static final int DEFAULT_INITIAL_CAPACITY = 1 &lt;&lt; 4; \/\/ aka 16<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>size\uff1a\u5f53\u524d HashMap \u4e2d\u5df2\u7ecf\u5b58\u50a8\u7740\u7684\u952e\u503c\u5bf9\u6570\u91cf\uff0c\u5373\u00a0<code>HashMap.size()<\/code>\u00a0\u3002<\/li><li>loadFactor\uff1a\u52a0\u8f7d\u56e0\u5b50\u3002\u6240\u8c13\u7684\u52a0\u8f7d\u56e0\u5b50\u5c31\u662f HashMap (\u5f53\u524d\u7684\u5bb9\u91cf\/\u603b\u5bb9\u91cf) \u5230\u8fbe\u4e00\u5b9a\u503c\u7684\u65f6\u5019\uff0cHashMap \u4f1a\u5b9e\u65bd\u6269\u5bb9\u3002\u52a0\u8f7d\u56e0\u5b50\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u4e2d\u6307\u5b9a\uff0c\u9ed8\u8ba4\u7684\u503c\u662f 0.75 \u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a HashMap \u7684\u521d\u59cb\u5bb9\u91cf\u4e3a 16 \uff0c\u90a3\u4e48\u6269\u5bb9\u7684\u9600\u503c\u5c31\u662f 0.75 * 16 = 12 \u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u4f60\u6253\u7b97\u5b58\u5165\u7b2c 13 \u4e2a\u503c\u7684\u65f6\u5019\uff0cHashMap \u4f1a\u5148\u6267\u884c\u6269\u5bb9\u3002<\/li><li>threshold\uff1a\u6269\u5bb9\u9600\u503c\u3002\u5373 \u6269\u5bb9\u9600\u503c = HashMap \u603b\u5bb9\u91cf * \u52a0\u8f7d\u56e0\u5b50\u3002\u5f53\u524d HashMap \u7684\u5bb9\u91cf\u5927\u4e8e\u6216\u7b49\u4e8e\u6269\u5bb9\u9600\u503c\u7684\u65f6\u5019\u5c31\u4f1a\u53bb\u6267\u884c\u6269\u5bb9\u3002\u6269\u5bb9\u7684\u5bb9\u91cf\u4e3a\u5f53\u524d HashMap \u603b\u5bb9\u91cf\u7684\u4e24\u500d\u3002\u6bd4\u5982\uff0c\u5f53\u524d HashMap \u7684\u603b\u5bb9\u91cf\u4e3a 16 \uff0c\u90a3\u4e48\u6269\u5bb9\u4e4b\u540e\u4e3a 32 \u3002<\/li><li>table\uff1aEntry \u6570\u7ec4\u3002\u6211\u4eec\u90fd\u77e5\u9053 HashMap \u5185\u90e8\u5b58\u50a8 key\/value \u662f\u901a\u8fc7 Entry \u8fd9\u4e2a\u4ecb\u8d28\u6765\u5b9e\u73b0\u7684\u3002\u800c table \u5c31\u662f Entry \u6570\u7ec4\u3002<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">JDK1.7 \u5b9e\u73b0<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JDK1.7 \u4e2d HashMap \u7531&nbsp;<strong>\u6570\u7ec4+\u94fe\u8868<\/strong>&nbsp;\u7ec4\u6210\uff08<strong>\u201c\u94fe\u8868\u6563\u5217\u201d<\/strong>&nbsp;\u5373\u6570\u7ec4\u548c\u94fe\u8868\u7684\u7ed3\u5408\u4f53\uff09\uff0c\u6570\u7ec4\u662f HashMap \u7684\u4e3b\u4f53\uff0c\u94fe\u8868\u5219\u662f\u4e3b\u8981\u4e3a\u4e86\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u800c\u5b58\u5728\u7684\uff08HashMap \u91c7\u7528&nbsp;<strong>\u201c\u62c9\u94fe\u6cd5\u4e5f\u5c31\u662f\u94fe\u5730\u5740\u6cd5\u201d<\/strong>&nbsp;\u89e3\u51b3\u51b2\u7a81\uff09\uff0c\u5982\u679c\u5b9a\u4f4d\u5230\u7684\u6570\u7ec4\u4f4d\u7f6e\u4e0d\u542b\u94fe\u8868\uff08\u5f53\u524d entry \u7684 next \u6307\u5411 null \uff09,\u90a3\u4e48\u5bf9\u4e8e\u67e5\u627e\uff0c\u6dfb\u52a0\u7b49\u64cd\u4f5c\u5f88\u5feb\uff0c\u4ec5\u9700\u4e00\u6b21\u5bfb\u5740\u5373\u53ef\uff1b\u5982\u679c\u5b9a\u4f4d\u5230\u7684\u6570\u7ec4\u5305\u542b\u94fe\u8868\uff0c\u5bf9\u4e8e\u6dfb\u52a0\u64cd\u4f5c\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u4f9d\u7136\u4e3a O(1)\uff0c\u56e0\u4e3a\u6700\u65b0\u7684 Entry \u4f1a\u63d2\u5165\u94fe\u8868\u5934\u90e8\uff0c\u5373\u9700\u8981\u7b80\u5355\u6539\u53d8\u5f15\u7528\u94fe\u5373\u53ef\uff0c\u800c\u5bf9\u4e8e\u67e5\u627e\u64cd\u4f5c\u6765\u8bb2\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u904d\u5386\u94fe\u8868\uff0c\u7136\u540e\u901a\u8fc7 key \u5bf9\u8c61\u7684 equals \u65b9\u6cd5\u9010\u4e00\u6bd4\u5bf9\u67e5\u627e\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u6240\u8c13&nbsp;<strong>\u201c\u62c9\u94fe\u6cd5\u201d<\/strong>&nbsp;\u5c31\u662f\u5c06\u94fe\u8868\u548c\u6570\u7ec4\u76f8\u7ed3\u5408\u3002\u4e5f\u5c31\u662f\u8bf4\u521b\u5efa\u4e00\u4e2a\u94fe\u8868\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u6bcf\u4e00\u683c\u5c31\u662f\u4e00\u4e2a\u94fe\u8868\u3002\u82e5\u9047\u5230\u54c8\u5e0c\u51b2\u7a81\uff0c\u5219\u5c06\u51b2\u7a81\u7684\u503c\u52a0\u5230\u94fe\u8868\u4e2d\u5373\u53ef\u3002<\/p><\/blockquote>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/007S8ZIlly1gdx45a2fbbj31dz0oaacx.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">\u6e90\u7801\u89e3\u6790<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u6784\u9020\u65b9\u6cd5<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u300a\u963f\u91cc\u5df4\u5df4 Java \u5f00\u53d1\u624b\u518c\u300b\u63a8\u8350\u96c6\u5408\u521d\u59cb\u5316\u65f6\uff0c\u6307\u5b9a\u96c6\u5408\u521d\u59cb\u503c\u5927\u5c0f\u3002\uff08\u8bf4\u660e\uff1aHashMap \u4f7f\u7528HashMap(int initialCapacity) \u521d\u59cb\u5316\uff09\u5efa\u8bae\u539f\u56e0\uff0c\u770b\u6211\u516c\u4f17\u53f7\u91cc\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u9ed8\u8ba4\u7684\u6784\u9020\u65b9\u6cd5\u4f7f\u7528\u7684\u90fd\u662f\u9ed8\u8ba4\u7684\u521d\u59cb\u5bb9\u91cf\u548c\u52a0\u8f7d\u56e0\u5b50\n\/\/ DEFAULT_INITIAL_CAPACITY = 16\uff0cDEFAULT_LOAD_FACTOR = 0.75f\npublic HashMap() {\n    this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);\n}\n\n\/\/ \u53ef\u4ee5\u6307\u5b9a\u521d\u59cb\u5bb9\u91cf\uff0c\u5e76\u4e14\u4f7f\u7528\u9ed8\u8ba4\u7684\u52a0\u8f7d\u56e0\u5b50\npublic HashMap(int initialCapacity) {\n    this(initialCapacity, DEFAULT_LOAD_FACTOR);\n}\n\npublic HashMap(int initialCapacity, float loadFactor) {\n    \/\/ \u5bf9\u521d\u59cb\u5bb9\u91cf\u7684\u503c\u5224\u65ad\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    \/\/ \u8bbe\u7f6e\u52a0\u8f7d\u56e0\u5b50\n    this.loadFactor = loadFactor;\n    threshold = initialCapacity;\n    \/\/ \u7a7a\u65b9\u6cd5\n    init();\n}\n\npublic HashMap(Map&lt;? extends K, ? extends V&gt; m) {\n  this(Math.max((int) (m.size() \/ DEFAULT_LOAD_FACTOR) + 1,\n                DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);\n  inflateTable(threshold);\n  putAllForCreate(m);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">HashMap \u7684\u524d 3 \u4e2a\u6784\u9020\u65b9\u6cd5\u6700\u540e\u90fd\u4f1a\u53bb\u8c03\u7528&nbsp;<code>HashMap(int initialCapacity, float loadFactor)<\/code>&nbsp;\u3002\u5728\u5176\u5185\u90e8\u53bb\u8bbe\u7f6e\u521d\u59cb\u5bb9\u91cf\u548c\u52a0\u8f7d\u56e0\u5b50\u3002\u800c\u6700\u540e\u7684&nbsp;<code>init()<\/code>&nbsp;\u662f\u7a7a\u65b9\u6cd5\uff0c\u4e3b\u8981\u7ed9\u5b50\u7c7b\u5b9e\u73b0\uff0c\u6bd4\u5982LinkedHashMap\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">put() \u65b9\u6cd5<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>public V put(K key, V value) {\n    \/\/ \u5982\u679c table \u6570\u7ec4\u4e3a\u7a7a\u65f6\u5148\u521b\u5efa\u6570\u7ec4\uff0c\u5e76\u4e14\u8bbe\u7f6e\u6269\u5bb9\u9600\u503c\n    if (table == EMPTY_TABLE) {\n        inflateTable(threshold);\n    }\n    \/\/ \u5982\u679c key \u4e3a\u7a7a\u65f6\uff0c\u8c03\u7528 putForNullKey \u65b9\u6cd5\u7279\u6b8a\u5904\u7406\n    if (key == null)\n        return putForNullKey(value);\n    \/\/ \u8ba1\u7b97 key \u7684\u54c8\u5e0c\u503c\n    int hash = hash(key);\n    \/\/ \u6839\u636e\u8ba1\u7b97\u51fa\u6765\u7684\u54c8\u5e0c\u503c\u548c\u5f53\u524d\u6570\u7ec4\u7684\u957f\u5ea6\u8ba1\u7b97\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\n    int i = indexFor(hash, table.length);\n    \/\/ \u5148\u904d\u5386\u8be5\u6570\u7ec4\u7d22\u5f15\u4e0b\u7684\u6574\u6761\u94fe\u8868\n    \/\/ \u5982\u679c\u8be5 key \u4e4b\u524d\u5df2\u7ecf\u5728 HashMap \u4e2d\u5b58\u50a8\u4e86\u7684\u8bdd\uff0c\u76f4\u63a5\u66ff\u6362\u5bf9\u5e94\u7684 value \u503c\u5373\u53ef\n    for (Entry&lt;K,V&gt; e = table[i]; e != null; e = e.next) {\n        Object k;\n       \/\/\u5148\u5224\u65adhash\u503c\u662f\u5426\u4e00\u6837\uff0c\u5982\u679c\u4e00\u6837\uff0c\u518d\u5224\u65adkey\u662f\u5426\u4e00\u6837\uff0c\u4e0d\u540c\u5bf9\u8c61\u7684hash\u503c\u53ef\u80fd\u4e00\u6837\n        if (e.hash == hash &amp;&amp; ((k = e.key) == key || key.equals(k))) {\n            V oldValue = e.value;\n            e.value = value;\n            e.recordAccess(this);\n            return oldValue;\n        }\n    }\n\n    modCount++;\n    \/\/ \u5982\u679c\u8be5 key \u4e4b\u524d\u6ca1\u6709\u88ab\u5b58\u50a8\u8fc7\uff0c\u90a3\u4e48\u5c31\u8fdb\u5165 addEntry \u65b9\u6cd5\n    addEntry(hash, key, value, i);\n    return null;\n}\n\nvoid addEntry(int hash, K key, V value, int bucketIndex) {\n    \/\/ \u5f53\u524d\u5bb9\u91cf\u5927\u4e8e\u6216\u7b49\u4e8e\u6269\u5bb9\u9600\u503c\u7684\u65f6\u5019\uff0c\u4f1a\u6267\u884c\u6269\u5bb9\n    if ((size &gt;= threshold) &amp;&amp; (null != table[bucketIndex])) {\n        \/\/ \u6269\u5bb9\u4e3a\u539f\u6765\u5bb9\u91cf\u7684\u4e24\u500d\n        resize(2 * table.length);\n        \/\/ \u91cd\u65b0\u8ba1\u7b97\u54c8\u5e0c\u503c\n        hash = (null != key) ? hash(key) : 0;\n        \/\/ \u91cd\u65b0\u5f97\u5230\u5728\u65b0\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\n        bucketIndex = indexFor(hash, table.length);\n    }\n    \/\/ \u521b\u5efa\u8282\u70b9\n    createEntry(hash, key, value, bucketIndex);\n}\n\n\/\/\u6269\u5bb9\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u7136\u540e\u628a\u6570\u636e\u5168\u90e8\u590d\u5236\u8fc7\u53bb\uff0c\u518d\u628a\u65b0\u6570\u7ec4\u7684\u5f15\u7528\u8d4b\u7ed9 table\nvoid resize(int newCapacity) {\n    Entry[] oldTable = table;  \/\/\u5f15\u7528\u6269\u5bb9\u524d\u7684Entry\u6570\u7ec4\n    int oldCapacity = oldTable.length;\n    if (oldCapacity == MAXIMUM_CAPACITY) { \/\/\u6269\u5bb9\u524d\u7684\u6570\u7ec4\u5927\u5c0f\u5982\u679c\u5df2\u7ecf\u8fbe\u5230\u6700\u5927(2^30)\u4e86\n        threshold = Integer.MAX_VALUE;  \/\/\u4fee\u6539\u9608\u503c\u4e3aint\u7684\u6700\u5927\u503c(2^31-1)\uff0c\u8fd9\u6837\u4ee5\u540e\u5c31\u4e0d\u4f1a\u6269\u5bb9\u4e86\n        return;\n    }\n    \/\/ \u521b\u5efa\u65b0\u7684 entry \u6570\u7ec4\n    Entry[] newTable = new Entry[newCapacity];\n    \/\/ \u5c06\u65e7 entry \u6570\u7ec4\u4e2d\u7684\u6570\u636e\u590d\u5236\u5230\u65b0 entry \u6570\u7ec4\u4e2d\n    transfer(newTable, initHashSeedAsNeeded(newCapacity));\n    \/\/ \u5c06\u65b0\u6570\u7ec4\u7684\u5f15\u7528\u8d4b\u7ed9 table\n    table = newTable;\n    \/\/ \u8ba1\u7b97\u65b0\u7684\u6269\u5bb9\u9600\u503c\n    threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);\n}\n\nvoid transfer(Entry[] newTable) {\n     Entry[] src = table;                   \/\/src\u5f15\u7528\u4e86\u65e7\u7684Entry\u6570\u7ec4\n     int newCapacity = newTable.length;\n     for (int j = 0; j &lt; src.length; j++) { \/\/\u904d\u5386\u65e7\u7684Entry\u6570\u7ec4\n         Entry&lt;K,V&gt; e = src[j];             \/\/\u53d6\u5f97\u65e7Entry\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\n         if (e != null) {\n             src[j] = null;\/\/\u91ca\u653e\u65e7Entry\u6570\u7ec4\u7684\u5bf9\u8c61\u5f15\u7528\uff08for\u5faa\u73af\u540e\uff0c\u65e7\u7684Entry\u6570\u7ec4\u4e0d\u518d\u5f15\u7528\u4efb\u4f55\u5bf9\u8c61\uff09\n             do {\n                 Entry&lt;K,V&gt; next = e.next;\n                 int i = indexFor(e.hash, newCapacity); \/\/\uff01\uff01\u91cd\u65b0\u8ba1\u7b97\u6bcf\u4e2a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\n                 e.next = newTable[i]; \/\/\u6807\u8bb0[1]\n                 newTable[i] = e;      \/\/\u5c06\u5143\u7d20\u653e\u5728\u6570\u7ec4\u4e0a\n                 e = next;             \/\/\u8bbf\u95ee\u4e0b\u4e00\u4e2aEntry\u94fe\u4e0a\u7684\u5143\u7d20\n             } while (e != null);\n         }\n     }\n} \n\nvoid createEntry(int hash, K key, V value, int bucketIndex) {\n   \/\/ \u53d6\u51fatable\u4e2d\u4e0b\u6807\u4e3abucketIndex\u7684Entry\n    Entry&lt;K,V&gt; e = table[bucketIndex];\n   \/\/ \u5229\u7528key\u3001value\u6765\u6784\u5efa\u65b0\u7684Entry\n   \/\/ \u5e76\u4e14\u4e4b\u524d\u5b58\u653e\u5728table[bucketIndex]\u5904\u7684Entry\u4f5c\u4e3a\u65b0Entry\u7684next\n   \/\/ \u628a\u65b0\u521b\u5efa\u7684Entry\u653e\u5230table[bucketIndex]\u4f4d\u7f6e\n    table[bucketIndex] = new Entry&lt;&gt;(hash, key, value, e);\n    \/\/ \u5f53\u524d HashMap \u7684\u5bb9\u91cf\u52a0 1\n    size++;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u7684 createEntry() \u65b9\u6cd5\u5c31\u8bf4\u660e\u4e86\u5f53hash\u51b2\u7a81\u65f6\uff0c\u91c7\u7528\u7684\u62c9\u94fe\u6cd5\u6765\u89e3\u51b3hash\u51b2\u7a81\u7684\uff0c\u5e76\u4e14\u662f\u628a\u65b0\u5143\u7d20\u662f\u63d2\u5165\u5230\u5355\u8fb9\u8868\u7684\u8868\u5934\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/007S8ZIlly1gdx45xr0x5j31hl0u0ncd.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">get() \u65b9\u6cd5<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>public V get(Object key) {\n    \/\/ \u5982\u679c key \u662f\u7a7a\u7684\uff0c\u5c31\u8c03\u7528 getForNullKey \u65b9\u6cd5\u7279\u6b8a\u5904\u7406\n    if (key == null)\n        return getForNullKey();\n    \/\/ \u83b7\u53d6 key \u76f8\u5bf9\u5e94\u7684 entry \n    Entry&lt;K,V&gt; entry = getEntry(key);\n\n    return null == entry ? null : entry.getValue();\n}\n\n\/\/\u627e\u5230\u5bf9\u5e94 key \u7684\u6570\u7ec4\u7d22\u5f15\uff0c\u7136\u540e\u904d\u5386\u94fe\u8868\u67e5\u627e\u5373\u53ef\nfinal Entry&lt;K,V&gt; getEntry(Object key) {\n    if (size == 0) {\n        return null;\n    }\n    \/\/ \u8ba1\u7b97 key \u7684\u54c8\u5e0c\u503c\n    int hash = (key == null) ? 0 : hash(key);\n    \/\/ \u5f97\u5230\u6570\u7ec4\u7684\u7d22\u5f15\uff0c\u7136\u540e\u904d\u5386\u94fe\u8868\uff0c\u67e5\u770b\u662f\u5426\u6709\u76f8\u540c key \u7684 Entry\n    for (Entry&lt;K,V&gt; e = table[indexFor(hash, table.length)];\n         e != null;\n         e = e.next) {\n        Object k;\n        if (e.hash == hash &amp;&amp;\n            ((k = e.key) == key || (key != null &amp;&amp; key.equals(k))))\n            return e;\n    }\n    \/\/ \u6ca1\u6709\u7684\u8bdd\uff0c\u8fd4\u56de null\n    return null;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">JDK1.8 \u5b9e\u73b0<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">JDK 1.7 \u4e2d\uff0c\u5982\u679c\u54c8\u5e0c\u78b0\u649e\u8fc7\u591a\uff0c\u62c9\u94fe\u8fc7\u957f\uff0c\u6781\u7aef\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u503c\u90fd\u843d\u5165\u4e86\u540c\u4e00\u4e2a\u6876\u5185\uff0c\u8fd9\u5c31\u9000\u5316\u6210\u4e86\u4e00\u4e2a\u94fe\u8868\u3002\u901a\u8fc7 key \u503c\u67e5\u627e\u8981\u904d\u5386\u94fe\u8868\uff0c\u6548\u7387\u8f83\u4f4e\u3002 JDK1.8\u5728\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u65f6\u6709\u4e86\u8f83\u5927\u7684\u53d8\u5316\uff0c<strong>\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<\/strong>\uff0c\u4ee5\u51cf\u5c11\u641c\u7d22\u65f6\u95f4\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/007S8ZIlly1gdx468uknnj31650ogjth.jpg\" alt=\"\" \/><figcaption>HashMap Java8\u7684\u5b9e\u73b0<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">TreeMap\u3001TreeSet\u4ee5\u53ca JDK1.8 \u4e4b\u540e\u7684 HashMap \u5e95\u5c42\u90fd\u7528\u5230\u4e86\u7ea2\u9ed1\u6811\u3002\u7ea2\u9ed1\u6811\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u4e8c\u53c9\u67e5\u627e\u6811\u7684\u7f3a\u9677\uff0c\u56e0\u4e3a\u4e8c\u53c9\u67e5\u627e\u6811\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u9000\u5316\u6210\u4e00\u4e2a\u7ebf\u6027\u7ed3\u6784\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6e90\u7801\u89e3\u6790<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u6784\u9020\u65b9\u6cd5<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">JDK8 \u6784\u9020\u65b9\u6cd5\u6539\u52a8\u4e0d\u662f\u5f88\u5927<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public 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\npublic HashMap(Map&lt;? extends K, ? extends V&gt; m) {\n  this.loadFactor = DEFAULT_LOAD_FACTOR;\n  putMapEntries(m, false);\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u786e\u5b9a\u54c8\u5e0c\u6876\u6570\u7ec4\u7d22\u5f15\u4f4d\u7f6e\uff08hash \u51fd\u6570\u7684\u5b9e\u73b0\uff09<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u65b9\u6cd5\u4e00\uff1a\nstatic final int hash(Object key) { \/\/jdk1.8 &amp; jdk1.7\n int h;\n \/\/ h = key.hashCode() \u4e3a\u7b2c\u4e00\u6b65 \u53d6hashCode\u503c\n \/\/ h ^ (h &gt;&gt;&gt; 16)  \u4e3a\u7b2c\u4e8c\u6b65 \u9ad8\u4f4d\u53c2\u4e0e\u8fd0\u7b97\n return (key == null) ? 0 : (h = key.hashCode()) ^ (h &gt;&gt;&gt; 16);\n}\n\/\/\u65b9\u6cd5\u4e8c\uff1a\nstatic int indexFor(int h, int length) { \/\/jdk1.7\u7684\u6e90\u7801\uff0cjdk1.8\u6ca1\u6709\u63d0\u53d6\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u800c\u662f\u653e\u5728\u4e86\u5176\u4ed6\u65b9\u6cd5\u4e2d\uff0c\u6bd4\u5982 put \u7684p = tab[i = (n - 1) &amp; hash]\n return h &amp; (length-1); \/\/\u7b2c\u4e09\u6b65 \u53d6\u6a21\u8fd0\u7b97\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">HashMap\u5b9a\u4f4d\u6570\u7ec4\u7d22\u5f15\u4f4d\u7f6e\uff0c\u76f4\u63a5\u51b3\u5b9a\u4e86hash\u65b9\u6cd5\u7684\u79bb\u6563\u6027\u80fd\u3002Hash\u7b97\u6cd5\u672c\u8d28\u4e0a\u5c31\u662f\u4e09\u6b65\uff1a<strong>\u53d6key\u7684hashCode\u503c\u3001\u9ad8\u4f4d\u8fd0\u7b97\u3001\u53d6\u6a21\u8fd0\u7b97<\/strong>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/007S8ZIlly1gdwv5m4g4sj30ga09cdfy.jpg\" alt=\"hash\" \/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u5462\uff1f<\/p><p>HashMap \u7684\u957f\u5ea6\u4e3a\u4ec0\u4e48\u662f2\u7684\u5e42\u6b21\u65b9?<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u76ee\u7684\u5f53\u7136\u662f\u4e3a\u4e86\u51cf\u5c11\u54c8\u5e0c\u78b0\u649e\uff0c\u4f7f table \u91cc\u7684\u6570\u636e\u5206\u5e03\u7684\u66f4\u5747\u5300\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>HashMap \u4e2d\u6876\u6570\u7ec4\u7684\u5927\u5c0f length \u603b\u662f2\u7684\u5e42\uff0c\u6b64\u65f6\uff0c<code>h &amp; (table.length -1)<\/code>&nbsp;\u7b49\u4ef7\u4e8e\u5bf9 length \u53d6\u6a21&nbsp;<code>h%length<\/code>\u3002\u4f46\u53d6\u6a21\u7684\u8ba1\u7b97\u6548\u7387\u6ca1\u6709\u4f4d\u8fd0\u7b97\u9ad8\uff0c\u6240\u4ee5\u8fd9\u662f\u662f\u4e00\u4e2a\u4f18\u5316\u3002\u5047\u8bbe&nbsp;<code>h = 185<\/code>\uff0c<code>table.length-1 = 15(0x1111)<\/code>\uff0c\u5176\u5b9e\u6563\u5217\u771f\u6b63\u751f\u6548\u7684\u53ea\u662f\u4f4e 4bit \u7684\u6709\u6548\u4f4d\uff0c\u6240\u4ee5\u5f88\u5bb9\u6613\u78b0\u649e\u3002<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/007S8ZIlly1gdx46gac50j30m8037mxf.jpg\" alt=\"img\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\"><li>\u56fe\u4e2d\u7684 hash \u662f\u7531\u952e\u7684 hashCode \u4ea7\u751f\u3002\u8ba1\u7b97\u4f59\u6570\u65f6\uff0c\u7531\u4e8e n \u6bd4\u8f83\u5c0f\uff0chash \u53ea\u6709\u4f4e4\u4f4d\u53c2\u4e0e\u4e86\u8ba1\u7b97\uff0c\u9ad8\u4f4d\u7684\u8ba1\u7b97\u53ef\u4ee5\u8ba4\u4e3a\u662f\u65e0\u6548\u7684\u3002\u8fd9\u6837\u5bfc\u81f4\u4e86\u8ba1\u7b97\u7ed3\u679c\u53ea\u4e0e\u4f4e\u4f4d\u4fe1\u606f\u6709\u5173\uff0c\u9ad8\u4f4d\u6570\u636e\u6ca1\u53d1\u6325\u4f5c\u7528\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e2a\u7f3a\u9677\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0a\u56fe\u4e2d\u7684 hash \u9ad84\u4f4d\u6570\u636e\u4e0e\u4f4e4\u4f4d\u6570\u636e\u8fdb\u884c\u5f02\u6216\u8fd0\u7b97\uff0c\u5373&nbsp;<code>hash ^ (hash &gt;&gt;&gt; 4)<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u8ba9\u9ad8\u4f4d\u6570\u636e\u4e0e\u4f4e\u4f4d\u6570\u636e\u8fdb\u884c\u5f02\u6216\uff0c\u4ee5\u6b64\u52a0\u5927\u4f4e\u4f4d\u4fe1\u606f\u7684\u968f\u673a\u6027\uff0c\u53d8\u76f8\u7684\u8ba9\u9ad8\u4f4d\u6570\u636e\u53c2\u4e0e\u5230\u8ba1\u7b97\u4e2d\u3002\u6b64\u65f6\u7684\u8ba1\u7b97\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/007S8ZIlly1gdx46jwezjj30m804cjrw.jpg\" alt=\"img\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Java \u4e2d\uff0chashCode \u65b9\u6cd5\u4ea7\u751f\u7684 hash \u662f int \u7c7b\u578b\uff0c32 \u4f4d\u5bbd\u3002\u524d16\u4f4d\u4e3a\u9ad8\u4f4d\uff0c\u540e16\u4f4d\u4e3a\u4f4e\u4f4d\uff0c\u6240\u4ee5\u8981\u53f3\u79fb16\u4f4d\uff0c\u5373&nbsp;<code>hash ^ (hash &gt;&gt;&gt; 16)<\/code>&nbsp;\u3002\u8fd9\u6837\u8fd8\u589e\u52a0\u4e86hash \u7684\u590d\u6742\u5ea6\uff0c\u8fdb\u800c\u5f71\u54cd hash \u7684\u5206\u5e03\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>HashMap \u7684\u957f\u5ea6\u4e3a\u4ec0\u4e48\u662f2\u7684\u5e42\u6b21\u65b9\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u80fd\u8ba9HashMap\u5b58\u53d6\u9ad8\u6548\uff0c\u5c3d\u91cf\u51cf\u5c11\u78b0\u649e\uff0c\u4e5f\u5c31\u662f\u8981\u5c3d\u91cf\u628a\u6570\u636e\u5206\u914d\u5747\u5300\uff0cHash\u503c\u7684\u8303\u56f4\u662f-2147483648\u52302147483647\uff0c\u524d\u540e\u52a0\u8d77\u6765\u670940\u4ebf\u7684\u6620\u5c04\u7a7a\u95f4\uff0c\u53ea\u8981\u54c8\u5e0c\u51fd\u6570\u6620\u5c04\u7684\u6bd4\u8f83\u5747\u5300\u677e\u6563\uff0c\u4e00\u822c\u5e94\u7528\u662f\u5f88\u96be\u51fa\u73b0\u78b0\u649e\u7684\uff0c\u4f46\u4e00\u4e2a\u95ee\u9898\u662f40\u4ebf\u7684\u6570\u7ec4\u5185\u5b58\u662f\u653e\u4e0d\u4e0b\u7684\u3002\u6240\u4ee5\u8fd9\u4e2a\u6563\u5217\u503c\u662f\u4e0d\u80fd\u76f4\u63a5\u62ff\u6765\u7528\u7684\u3002\u7528\u4e4b\u524d\u9700\u8981\u5148\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\u8fd0\u7b97\uff0c\u5f97\u5230\u4f59\u6570\u624d\u80fd\u7528\u6765\u5b58\u653e\u4f4d\u7f6e\u4e5f\u5c31\u662f\u5bf9\u5e94\u7684\u6570\u7ec4\u5c0f\u6807\u3002\u8fd9\u4e2a\u6570\u7ec4\u4e0b\u6807\u7684\u8ba1\u7b97\u65b9\u6cd5\u662f(n-1)&amp;hash\uff0cn\u4ee3\u8868\u6570\u7ec4\u957f\u5ea6<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u7b97\u6cd5\u5e94\u8be5\u5982\u4f55\u8bbe\u8ba1\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u9996\u5148\u53ef\u80fd\u4f1a\u60f3\u5230\u91c7\u7528%\u53d6\u4f59\u7684\u64cd\u4f5c\u6765\u5b9e\u73b0\u3002\u4f46\u662f\uff0c\u91cd\u70b9\u6765\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53d6\u4f59\u64cd\u4f5c\u4e2d\u5982\u679c\u9664\u6570\u662f2\u7684\u5e42\u6b21\u5219\u7b49\u4ef7\u4e8e\u5176\u9664\u6570\u51cf\u4e00\u7684\u4e0e\u64cd\u4f5c\uff0c\u4e5f\u5c31\u662f\u8bf4hash%length=hash&amp;(length-1)\uff0c\u4f46\u524d\u63d0\u662flength\u662f2\u7684n\u6b21\u65b9\uff0c\u5e76\u4e14\u91c7\u7528&amp;\u8fd0\u7b97\u6bd4%\u8fd0\u7b97\u6548\u7387\u9ad8\uff0c\u8fd9\u4e5f\u5c31\u89e3\u91ca\u4e86HashMap\u7684\u957f\u5ea6\u4e3a\u4ec0\u4e48\u662f2\u7684\u5e42\u6b21\u65b9\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">put() \u65b9\u6cd5<\/h5>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/007S8ZIlly1gdx470vyzej311c0u0120.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>public V put(K key, V value) {\n  \/\/ \u5bf9key\u7684hashCode()\u505ahash\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  \/\/ tab\u4e3a\u7a7a\u5219\u521b\u5efa\n  if ((tab = table) == null || (n = tab.length) == 0)\n    n = (tab = resize()).length;\n  \/\/ \u8ba1\u7b97index\uff0c\u5e76\u5bf9null\u505a\u5904\u7406\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    \/\/ \u8282\u70b9key\u5b58\u5728\uff0c\u76f4\u63a5\u8986\u76d6value\n    if (p.hash == hash &amp;&amp;\n        ((k = p.key) == key || (key != null &amp;&amp; key.equals(k))))\n      e = p;\n    \/\/ \u5224\u65ad\u8be5\u94fe\u4e3a\u7ea2\u9ed1\u6811\n    else if (p instanceof TreeNode)\n      e = ((TreeNode&lt;K,V&gt;)p).putTreeVal(this, tab, hash, key, value);\n    else {\n      \/\/\u8be5\u94fe\u4e3a\u94fe\u8868\n      for (int binCount = 0; ; ++binCount) {\n        if ((e = p.next) == null) {\n          p.next = newNode(hash, key, value, null);\n          \/\/\u94fe\u8868\u957f\u5ea6\u5927\u4e8e8\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u8fdb\u884c\u5904\u7406\n          if (binCount &gt;= TREEIFY_THRESHOLD - 1) \/\/ -1 for 1st\n            treeifyBin(tab, hash);\n          break;\n        }\n        \/\/key\u5df2\u7ecf\u5b58\u5728\u76f4\u63a5\u8986\u76d6value\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  \/\/ \u8d85\u8fc7\u6700\u5927\u5bb9\u91cf \u5c31\u6269\u5bb9\n  if (++size &gt; threshold)\n    resize();\n  afterNodeInsertion(evict);\n  return null;\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">resize() \u6269\u5bb9<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>final 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    \/\/ \u8d85\u8fc7\u6700\u5927\u503c\u5c31\u4e0d\u518d\u6269\u5145\u4e86\uff0c\u5c31\u53ea\u597d\u968f\u4f60\u78b0\u649e\u4e86\n    if (oldCap &gt;= MAXIMUM_CAPACITY) {\n      \/\/\u4fee\u6539\u9608\u503c\u4e3aint\u7684\u6700\u5927\u503c(2^31-1)\uff0c\u8fd9\u6837\u4ee5\u540e\u5c31\u4e0d\u4f1a\u6269\u5bb9\u4e86\n      threshold = Integer.MAX_VALUE;\n      return oldTab;\n    }\n     \/\/ \u6ca1\u8d85\u8fc7\u6700\u5927\u503c\uff0c\u5c31\u6269\u5145\u4e3a\u539f\u6765\u76842\u500d\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  else {               \/\/ zero initial threshold signifies using defaults\n    newCap = DEFAULT_INITIAL_CAPACITY;\n    newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);\n  }\n  \/\/ \u8ba1\u7b97\u65b0\u7684resize\u4e0a\u9650\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  \/\/ \u628a\u6bcf\u4e2abucket\u90fd\u79fb\u52a8\u5230\u65b0\u7684buckets\u4e2d\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        oldTab[j] = null;\n        if (e.next == null)\n          newTab[e.hash &amp; (newCap - 1)] = e;\n        else if (e instanceof TreeNode)\n          ((TreeNode&lt;K,V&gt;)e).split(this, newTab, j, oldCap);\n        else { \/\/ preserve order  \u94fe\u8868\u4f18\u5316\u91cdhash\u7684\u4ee3\u7801\u5757\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            \/\/ \u539f\u7d22\u5f15\n            next = e.next;\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            \/\/ \u539f\u7d22\u5f15+oldCap\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          \/\/ \u539f\u7d22\u5f15\u653e\u5230bucket\u91cc\n          if (loTail != null) {\n            loTail.next = null;\n            newTab[j] = loHead;\n          }\n          \/\/ \u539f\u7d22\u5f15+oldCap\u653e\u5230bucket\u91cc\n          if (hiTail != null) {\n            hiTail.next = null;\n            newTab[j + oldCap] = hiHead;\n          }\n        }\n      }\n    }\n  }\n  return newTab;\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">get() \u65b9\u6cd5<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>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  \/\/\u5b9a\u4f4d\u952e\u503c\u5bf9\u6240\u5728\u6876\u7684\u4f4d\u7f6e\n  if ((tab = table) != null &amp;&amp; (n = tab.length) &gt; 0 &amp;&amp;\n      (first = tab[(n - 1) &amp; hash]) != null) {\n    \/\/ \u76f4\u63a5\u547d\u4e2d\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    \/\/ \u672a\u547d\u4e2d\n    if ((e = first.next) != null) {\n      \/\/ \u5982\u679c first \u662f TreeNode \u7c7b\u578b\uff0c\u5219\u8c03\u7528\u9ed1\u7ea2\u6811\u67e5\u627e\u65b9\u6cd5\n      if (first instanceof TreeNode)\n        return ((TreeNode&lt;K,V&gt;)first).getTreeNode(hash, key);\n      do {\n        \/\/ \u5728\u94fe\u8868\u4e2d\u67e5\u627e\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}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Hashtable<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hashtable \u548c HashMap \u90fd\u662f\u6563\u5217\u8868\uff0c\u4e5f\u662f\u7528\u201d\u62c9\u94fe\u6cd5\u201c\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\u3002\u4fdd\u5b58\u6570\u636e\u548c JDK7 \u4e2d\u7684 HashMap \u4e00\u6837\uff0c\u662f Entity \u5bf9\u8c61\uff0c\u53ea\u662f Hashtable \u4e2d\u7684\u51e0\u4e4e\u6240\u6709\u7684 public \u65b9\u6cd5\u90fd\u662f synchronized \u7684\uff0c\u800c\u6709\u4e9b\u65b9\u6cd5\u4e5f\u662f\u5728\u5185\u90e8\u901a\u8fc7 synchronized \u4ee3\u7801\u5757\u6765\u5b9e\u73b0\uff0c\u6548\u7387\u80af\u5b9a\u4f1a\u964d\u4f4e\u3002\u4e14 put() \u65b9\u6cd5\u4e0d\u5141\u8bb8\u7a7a\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">HashMap \u548c Hashtable \u7684\u533a\u522b<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>\u7ebf\u7a0b\u662f\u5426\u5b89\u5168\uff1a<\/strong>&nbsp;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&nbsp;<code>synchronized<\/code>&nbsp;\u4fee\u9970\u3002\uff08\u5982\u679c\u4f60\u8981\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u7684\u8bdd\u5c31\u4f7f\u7528 ConcurrentHashMap \u5427\uff01\uff09\uff1b<\/li><li><strong>\u6548\u7387\uff1a<\/strong>&nbsp;\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<\/li><li><strong>\u5bf9Null key \u548cNull value\u7684\u652f\u6301\uff1a<\/strong>&nbsp;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<\/li><li><strong>\u521d\u59cb\u5bb9\u91cf\u5927\u5c0f\u548c\u6bcf\u6b21\u6269\u5145\u5bb9\u91cf\u5927\u5c0f\u7684\u4e0d\u540c \uff1a<\/strong><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\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<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\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\u3002\u4e5f\u5c31\u662f\u8bf4 HashMap \u603b\u662f\u4f7f\u75282\u7684\u5e42\u6b21\u65b9\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684\u5927\u5c0f,\u540e\u9762\u4f1a\u4ecb\u7ecd\u5230\u4e3a\u4ec0\u4e48\u662f2\u7684\u5e42\u6b21\u65b9\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>\u5e95\u5c42\u6570\u636e\u7ed3\u6784\uff1a<\/strong>&nbsp;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<\/li><li>HashMap\u7684\u8fed\u4ee3\u5668\uff08<code>Iterator<\/code>\uff09\u662ffail-fast\u8fed\u4ee3\u5668\uff0c\u4f46\u662f Hashtable\u7684\u8fed\u4ee3\u5668\uff08<code>enumerator<\/code>\uff09\u4e0d\u662f fail-fast\u7684\u3002\u5982\u679c\u6709\u5176\u5b83\u7ebf\u7a0b\u5bf9HashMap\u8fdb\u884c\u7684\u6dfb\u52a0\/\u5220\u9664\u5143\u7d20\uff0c\u5c06\u4f1a\u629b\u51fa<code>ConcurrentModificationException<\/code>\uff0c\u4f46\u8fed\u4ee3\u5668\u672c\u8eab\u7684<code>remove<\/code>\u65b9\u6cd5\u79fb\u9664\u5143\u7d20\u5219\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u8fd9\u6761\u540c\u6837\u4e5f\u662f Enumeration \u548c Iterator \u7684\u533a\u522b\u3002<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">ConcurrentHashMap<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">HashMap\u5728\u591a\u7ebf\u7a0b\u60c5\u51b5\u4e0b\uff0c\u5728put\u7684\u65f6\u5019\uff0c\u63d2\u5165\u7684\u5143\u7d20\u8d85\u8fc7\u4e86\u5bb9\u91cf\uff08\u7531\u8d1f\u8f7d\u56e0\u5b50\u51b3\u5b9a\uff09\u7684\u8303\u56f4\u5c31\u4f1a\u89e6\u53d1\u6269\u5bb9\u64cd\u4f5c\uff0c\u5c31\u662frehash\uff0c\u8fd9\u4e2a\u4f1a\u91cd\u65b0\u5c06\u539f\u6570\u7ec4\u7684\u5185\u5bb9\u91cd\u65b0hash\u5230\u65b0\u7684\u6269\u5bb9\u6570\u7ec4\u4e2d\uff0c\u5728\u591a\u7ebf\u7a0b\u7684\u73af\u5883\u4e0b\uff0c\u5b58\u5728\u540c\u65f6\u5176\u4ed6\u7684\u5143\u7d20\u4e5f\u5728\u8fdb\u884cput\u64cd\u4f5c\uff0c\u5982\u679chash\u503c\u76f8\u540c\uff0c\u53ef\u80fd\u51fa\u73b0\u540c\u65f6\u5728\u540c\u4e00\u6570\u7ec4\u4e0b\u7528\u94fe\u8868\u8868\u793a\uff0c\u9020\u6210<strong>\u95ed\u73af<\/strong>\uff0c\u5bfc\u81f4\u5728get\u65f6\u4f1a\u51fa\u73b0\u6b7b\u5faa\u73af\uff0c\u6240\u4ee5HashMap\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hashtable\uff0c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u5b83\u5728\u6240\u6709\u6d89\u53ca\u5230\u591a\u7ebf\u7a0b\u64cd\u4f5c\u7684\u90fd\u52a0\u4e0a\u4e86synchronized\u5173\u952e\u5b57\u6765\u9501\u4f4f\u6574\u4e2atable\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u6240\u6709\u7684\u7ebf\u7a0b\u90fd\u5728\u7ade\u4e89\u4e00\u628a\u9501\uff0c\u5728\u591a\u7ebf\u7a0b\u7684\u73af\u5883\u4e0b\uff0c\u5b83\u662f\u5b89\u5168\u7684\uff0c\u4f46\u662f\u65e0\u7591\u662f\u6548\u7387\u4f4e\u4e0b\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">JDK1.7 \u5b9e\u73b0<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Hashtable \u5bb9\u5668\u5728\u7ade\u4e89\u6fc0\u70c8\u7684\u5e76\u53d1\u73af\u5883\u4e0b\u8868\u73b0\u51fa\u6548\u7387\u4f4e\u4e0b\u7684\u539f\u56e0\uff0c\u662f\u56e0\u4e3a\u6240\u6709\u8bbf\u95ee Hashtable \u7684\u7ebf\u7a0b\u90fd\u5fc5\u987b\u7ade\u4e89\u540c\u4e00\u628a\u9501\uff0c\u90a3\u5047\u5982\u5bb9\u5668\u91cc\u6709\u591a\u628a\u9501\uff0c\u6bcf\u4e00\u628a\u9501\u7528\u4e8e\u9501\u5bb9\u5668\u5176\u4e2d\u4e00\u90e8\u5206\u6570\u636e\uff0c\u90a3\u4e48\u5f53\u591a\u7ebf\u7a0b\u8bbf\u95ee\u5bb9\u5668\u91cc\u4e0d\u540c\u6570\u636e\u6bb5\u7684\u6570\u636e\u65f6\uff0c\u7ebf\u7a0b\u95f4\u5c31\u4e0d\u4f1a\u5b58\u5728\u9501\u7ade\u4e89\uff0c\uff0c\u8fd9\u5c31\u662fConcurrentHashMap\u6240\u4f7f\u7528\u7684\u9501\u5206\u6bb5\u6280\u672f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 JDK1.7\u7248\u672c\u4e2d\uff0cConcurrentHashMap \u7684\u6570\u636e\u7ed3\u6784\u662f\u7531\u4e00\u4e2a Segment \u6570\u7ec4\u548c\u591a\u4e2a HashEntry \u7ec4\u6210\u3002Segment \u6570\u7ec4\u7684\u610f\u4e49\u5c31\u662f\u5c06\u4e00\u4e2a\u5927\u7684 table \u5206\u5272\u6210\u591a\u4e2a\u5c0f\u7684 table \u6765\u8fdb\u884c\u52a0\u9501\u3002\u6bcf\u4e00\u4e2a Segment \u5143\u7d20\u5b58\u50a8\u7684\u662f HashEntry\u6570\u7ec4+\u94fe\u8868\uff0c\u8fd9\u4e2a\u548c HashMap \u7684\u6570\u636e\u5b58\u50a8\u7ed3\u6784\u4e00\u6837\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/yfzhou.oss-cn-beijing.aliyuncs.com\/blog\/img\/JDK1.7%20ConcurrentHashMap.jpg\" alt=\"\" \/><figcaption>ConcurrentHashMap JDK1.7\u7684\u5b9e\u73b0<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">ConcurrentHashMap \u7c7b\u4e2d\u5305\u542b\u4e24\u4e2a\u9759\u6001\u5185\u90e8\u7c7b HashEntry \u548c Segment\u3002<br>HashEntry \u7528\u6765\u5c01\u88c5\u6620\u5c04\u8868\u7684\u952e\u503c\u5bf9\uff0cSegment \u7528\u6765\u5145\u5f53\u9501\u7684\u89d2\u8272\uff0c\u6bcf\u4e2a Segment \u5bf9\u8c61\u5b88\u62a4\u6574\u4e2a\u6563\u5217\u6620\u5c04\u8868\u7684\u82e5\u5e72\u4e2a\u6876\u3002\u6bcf\u4e2a\u6876\u662f\u7531\u82e5\u5e72\u4e2a HashEntry \u5bf9\u8c61\u94fe\u63a5\u8d77\u6765\u7684\u94fe\u8868\u3002\u4e00\u4e2a ConcurrentHashMap \u5b9e\u4f8b\u4e2d\u5305\u542b\u7531\u82e5\u5e72\u4e2a Segment \u5bf9\u8c61\u7ec4\u6210\u7684\u6570\u7ec4\u3002\u6bcf\u4e2a Segment \u5b88\u62a4\u7740\u4e00\u4e2a HashEntry \u6570\u7ec4\u91cc\u7684\u5143\u7d20\uff0c\u5f53\u5bf9 HashEntry \u6570\u7ec4\u7684\u6570\u636e\u8fdb\u884c\u4fee\u6539\u65f6\uff0c\u5fc5\u987b\u9996\u5148\u83b7\u5f97\u5b83\u5bf9\u5e94\u7684 Segment \u9501\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Segment \u7c7b<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Segment \u7c7b\u7ee7\u627f\u4e8e ReentrantLock \u7c7b\uff0c\u4ece\u800c\u4f7f\u5f97 Segment \u5bf9\u8c61\u80fd\u5145\u5f53\u53ef\u91cd\u5165\u9501\u7684\u89d2\u8272\u3002\u4e00\u4e2a Segment \u5c31\u662f\u4e00\u4e2a\u5b50\u54c8\u5e0c\u8868\uff0cSegment \u91cc\u7ef4\u62a4\u4e86\u4e00\u4e2a HashEntry \u6570\u7ec4\uff0c\u5e76\u53d1\u73af\u5883\u4e0b\uff0c\u5bf9\u4e8e\u4e0d\u540c Segment \u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u662f\u4e0d\u7528\u8003\u8651\u9501\u7ade\u4e89\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u6e90\u7801\u53ef\u4ee5\u770b\u5230\uff0cSegment \u5185\u90e8\u7c7b\u548c\u6211\u4eec\u4e0a\u8fb9\u770b\u5230\u7684 HashMap \u5f88\u76f8\u4f3c\u3002\u4e5f\u6709\u8d1f\u8f7d\u56e0\u5b50\uff0c\u9608\u503c\u7b49\u5404\u79cd\u5c5e\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static final class Segment&lt;K,V&gt; extends ReentrantLock implements Serializable {\n  \n  static final int MAX_SCAN_RETRIES =\n    Runtime.getRuntime().availableProcessors() &gt; 1 ? 64 : 1;\n  transient volatile HashEntry&lt;K,V&gt;[] table;\n  transient int count;\n  transient int modCount;  \/\/\u8bb0\u5f55\u4fee\u6539\u6b21\u6570\n  transient int threshold;\n  final float loadFactor;\n\n  Segment(float lf, int threshold, HashEntry&lt;K,V&gt;[] tab) {\n    this.loadFactor = lf;\n    this.threshold = threshold;\n    this.table = tab;\n  }\n\n  \/\/put \u65b9\u6cd5\u4f1a\u6709\u52a0\u9501\u64cd\u4f5c\uff0c\n  final V put(K key, int hash, V value, boolean onlyIfAbsent) {\n    HashEntry&lt;K,V&gt; node = tryLock() ? null :\n                scanAndLockForPut(key, hash, value);\n    \/\/ ...\n  }\n\n  @SuppressWarnings(\"unchecked\")\n  private void rehash(HashEntry&lt;K,V&gt; node) {\n    \/\/ ...\n  }\n\n  private HashEntry&lt;K,V&gt; scanAndLockForPut(K key, int hash, V value) {\n         \/\/...\n  }\n\n  private void scanAndLock(Object key, int hash) {\n         \/\/...\n  }\n\n  final V remove(Object key, int hash, Object value) {\n    \/\/...\n  }\n\n  final boolean replace(K key, int hash, V oldValue, V newValue) {\n      \/\/...\n  }\n\n  final V replace(K key, int hash, V value) {\n    \/\/...\n  }\n\n  final void clear() {\n        \/\/...\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">HashEntry \u7c7b<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">HashEntry \u662f\u76ee\u524d\u6211\u4eec\u6700\u5c0f\u7684\u903b\u8f91\u5904\u7406\u5355\u5143\u3002\u4e00\u4e2aConcurrentHashMap \u7ef4\u62a4\u4e00\u4e2a Segment \u6570\u7ec4\uff0c\u4e00\u4e2aSegment\u7ef4\u62a4\u4e00\u4e2a HashEntry \u6570\u7ec4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static final class HashEntry&lt;K,V&gt; {\n  final int hash;\n  final K key;\n  volatile V value;   \/\/ value \u4e3a volatie \u7c7b\u578b\uff0c\u4fdd\u8bc1\u53ef\u89c1\n  volatile HashEntry&lt;K,V&gt; next;\n    \/\/...\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">ConcurrentHashMap \u7c7b<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2aConcurrentHashMap \u7c7b\u4f1a\u521b\u5efa16\u4e2a\u5e76\u53d1\u7684 segment\uff0c\u6bcf\u4e2a segment \u91cc\u9762\u5305\u542b\u591a\u4e2a Hash\u8868\uff0c\u6bcf\u4e2a Hash \u94fe\u90fd\u662f\u7531 HashEntry \u8282\u70b9\u7ec4\u6210\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ConcurrentHashMap&lt;K, V&gt; extends AbstractMap&lt;K, V&gt;\n        implements ConcurrentMap&lt;K, V&gt;, Serializable {\n    \/\/\u9ed8\u8ba4\u521d\u59cb\u5bb9\u91cf\u4e3a 16\uff0c\u5373\u521d\u59cb\u9ed8\u8ba4\u4e3a 16 \u4e2a\u6876\n  static final int DEFAULT_INITIAL_CAPACITY = 16;\n  static final float DEFAULT_LOAD_FACTOR = 0.75f;\n  \/\/\u9ed8\u8ba4\u5e76\u53d1\u7ea7\u522b\u4e3a 16\u3002\u8be5\u503c\u8868\u793a\u5f53\u524d\u66f4\u65b0\u7ebf\u7a0b\u7684\u4f30\u8ba1\u6570\n  static final int DEFAULT_CONCURRENCY_LEVEL = 16;\n  \n  static final int MAXIMUM_CAPACITY = 1 &lt;&lt; 30;\n  static final int MIN_SEGMENT_TABLE_CAPACITY = 2;  \n  static final int MAX_SEGMENTS = 1 &lt;&lt; 16; \/\/ slightly conservative  \n  static final int RETRIES_BEFORE_LOCK = 2;\n  final int segmentMask;  \/\/\u6bb5\u63a9\u7801,\u4e3b\u8981\u4e3a\u4e86\u5b9a\u4f4dSegment\n  final int segmentShift;\n  final Segment&lt;K,V&gt;[] segments;   \/\/\u4e3b\u5e72\u5c31\u662f\u8fd9\u4e2a\u5206\u6bb5\u9501\u6570\u7ec4\n  \n  \/\/\u6784\u9020\u5668\n  public ConcurrentHashMap(int initialCapacity,\n                             float loadFactor, int concurrencyLevel) {\n        if (!(loadFactor &gt; 0) || initialCapacity &lt; 0 || concurrencyLevel &lt;= 0)\n            throw new IllegalArgumentException();\n      \/\/MAX_SEGMENTS \u4e3a1&lt;&lt;16=65536\uff0c\u4e5f\u5c31\u662f\u6700\u5927\u5e76\u53d1\u6570\u4e3a65536\n        if (concurrencyLevel &gt; MAX_SEGMENTS)\n            concurrencyLevel = MAX_SEGMENTS;\n        \/\/ 2\u7684sshif\u6b21\u65b9\u7b49\u4e8essize\uff0c\u4f8b:ssize=16,sshift=4;ssize=32,sshif=5\n        int sshift = 0;\n        \/\/ ssize \u4e3asegments\u6570\u7ec4\u957f\u5ea6\uff0c\u6839\u636econcurrentLevel\u8ba1\u7b97\u5f97\u51fa\n        int ssize = 1;\n        while (ssize &lt; concurrencyLevel) {\n            ++sshift;\n            ssize &lt;&lt;= 1;\n        }\n        this.segmentShift = 32 - sshift;\n        this.segmentMask = ssize - 1;\n        if (initialCapacity &gt; MAXIMUM_CAPACITY)\n            initialCapacity = MAXIMUM_CAPACITY;\n        int c = initialCapacity \/ ssize;\n        if (c * ssize &lt; initialCapacity)\n            ++c;\n        int cap = MIN_SEGMENT_TABLE_CAPACITY;\n        while (cap &lt; c)\n            cap &lt;&lt;= 1;\n        \/\/ \u521b\u5efasegments\u6570\u7ec4\u5e76\u521d\u59cb\u5316\u7b2c\u4e00\u4e2aSegment\uff0c\u5176\u4f59\u7684Segment\u5ef6\u8fdf\u521d\u59cb\u5316\n        Segment&lt;K,V&gt; s0 =\n            new Segment&lt;K,V&gt;(loadFactor, (int)(cap * loadFactor),\n                             (HashEntry&lt;K,V&gt;[])new HashEntry[cap]);\n        Segment&lt;K,V&gt;[] ss = (Segment&lt;K,V&gt;[])new Segment[ssize];\n        UNSAFE.putOrderedObject(ss, SBASE, s0); \/\/ ordered write of segments[0]\n        this.segments = ss;\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">put() \u65b9\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>\u5b9a\u4f4dsegment\u5e76\u786e\u4fdd\u5b9a\u4f4d\u7684Segment\u5df2\u521d\u59cb\u5316<\/strong><\/li><li><strong>\u8c03\u7528 Segment\u7684 put \u65b9\u6cd5\u3002<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>public V put(K key, V value) {\n  Segment&lt;K,V&gt; s;\n  \/\/concurrentHashMap\u4e0d\u5141\u8bb8key\/value\u4e3a\u7a7a\n  if (value == null)\n    throw new NullPointerException();\n  \/\/hash\u51fd\u6570\u5bf9key\u7684hashCode\u91cd\u65b0\u6563\u5217\uff0c\u907f\u514d\u5dee\u52b2\u7684\u4e0d\u5408\u7406\u7684hashcode\uff0c\u4fdd\u8bc1\u6563\u5217\u5747\u5300\n  int hash = hash(key);\n  \/\/\u8fd4\u56de\u7684hash\u503c\u65e0\u7b26\u53f7\u53f3\u79fbsegmentShift\u4f4d\u4e0e\u6bb5\u63a9\u7801\u8fdb\u884c\u4f4d\u8fd0\u7b97\uff0c\u5b9a\u4f4dsegment\n  int j = (hash &gt;&gt;&gt; segmentShift) &amp; segmentMask;\n  if ((s = (Segment&lt;K,V&gt;)UNSAFE.getObject          \/\/ nonvolatile; recheck\n       (segments, (j &lt;&lt; SSHIFT) + SBASE)) == null) \/\/  in ensureSegment\n    s = ensureSegment(j);\n  return s.put(key, hash, value, false);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">get() \u65b9\u6cd5<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>get\u65b9\u6cd5\u65e0\u9700\u52a0\u9501\uff0c\u7531\u4e8e\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u5171\u4eab\u53d8\u91cf\u90fd\u4f7f\u7528volatile\u4fee\u9970\uff0cvolatile\u53ef\u4ee5\u4fdd\u8bc1\u5185\u5b58\u53ef\u89c1\u6027\uff0c\u6240\u4ee5\u4e0d\u4f1a\u8bfb\u53d6\u5230\u8fc7\u671f\u6570\u636e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public V get(Object key) {\n  Segment&lt;K,V&gt; s; \/\/ manually integrate access methods to reduce overhead\n  HashEntry&lt;K,V&gt;[] tab;\n  int h = hash(key);\n  long u = (((h &gt;&gt;&gt; segmentShift) &amp; segmentMask) &lt;&lt; SSHIFT) + SBASE;\n  if ((s = (Segment&lt;K,V&gt;)UNSAFE.getObjectVolatile(segments, u)) != null &amp;&amp;\n      (tab = s.table) != null) {\n    for (HashEntry&lt;K,V&gt; e = (HashEntry&lt;K,V&gt;) UNSAFE.getObjectVolatile\n         (tab, ((long)(((tab.length - 1) &amp; h)) &lt;&lt; TSHIFT) + TBASE);\n         e != null; e = e.next) {\n      K k;\n      if ((k = e.key) == key || (e.hash == h &amp;&amp; key.equals(k)))\n        return e.value;\n    }\n  }\n  return null;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">JDK1.8 \u5b9e\u73b0<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/yfzhou.oss-cn-beijing.aliyuncs.com\/blog\/img\/JDK1.8%20ConcurrentHashMap.jpg\" alt=\"img\" \/><figcaption>ConcurrentHashMap JDK1.8\u7684\u5b9e\u73b0<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">ConcurrentHashMap \u5728 JDK8 \u4e2d\u8fdb\u884c\u4e86\u5de8\u5927\u6539\u52a8\uff0c\u5149\u662f\u4ee3\u7801\u91cf\u5c31\u4ece1000\u591a\u884c\u589e\u52a0\u52306000\u884c\uff011.8\u6452\u5f03\u4e86<code>Segment<\/code>(\u9501\u6bb5)\u7684\u6982\u5ff5\uff0c\u91c7\u7528\u4e86&nbsp;<code>CAS + synchronized<\/code>&nbsp;\u6765\u4fdd\u8bc1\u5e76\u53d1\u7684\u5b89\u5168\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\uff0c\u548cHashMap 1.8\u7684\u6570\u636e\u7ed3\u6784\u5f88\u50cf\u3002\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u6539\u53d8\u4e3a\u91c7\u7528\u6570\u7ec4+\u94fe\u8868+\u7ea2\u9ed1\u6811\u7684\u6570\u636e\u5f62\u5f0f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u548cHashMap1.8\u76f8\u540c\u7684\u4e00\u4e9b\u5730\u65b9<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u4e00\u81f4<\/li><li>HashMap\u521d\u59cb\u5316\u662f\u5728\u7b2c\u4e00\u6b21put\u5143\u7d20\u7684\u65f6\u5019\u8fdb\u884c\u7684\uff0c\u800c\u4e0d\u662finit<\/li><li>HashMap\u7684\u5e95\u5c42\u6570\u7ec4\u957f\u5ea6\u603b\u662f\u4e3a2\u7684\u6574\u6b21\u5e42<\/li><li>\u9ed8\u8ba4\u6811\u5316\u7684\u9608\u503c\u4e3a 8\uff0c\u800c\u94fe\u8868\u5316\u7684\u9608\u503c\u4e3a 6<\/li><li>hash\u7b97\u6cd5\u4e5f\u5f88\u7c7b\u4f3c\uff0c\u4f46\u591a\u4e86\u4e00\u6b65<code>&amp; HASH_BITS<\/code>\uff0c\u8be5\u6b65\u662f\u4e3a\u4e86\u6d88\u9664\u6700\u9ad8\u4f4d\u4e0a\u7684\u8d1f\u7b26\u53f7\uff0chash\u7684\u8d1f\u5728ConcurrentHashMap\u4e2d\u6709\u7279\u6b8a\u610f\u4e49\u8868\u793a\u5728<strong>\u6269\u5bb9\u6216\u8005\u662f\u6811\u8282\u70b9<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>static final int HASH_BITS = 0x7fffffff; \/\/ usable bits of normal node hash\n\nstatic final int spread(int h) {\n    return (h ^ (h &gt;&gt;&gt; 16)) &amp; HASH_BITS;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e00\u4e9b\u5173\u952e\u5c5e\u6027<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>private static final int MAXIMUM_CAPACITY = 1 &lt;&lt; 30; \/\/\u6570\u7ec4\u6700\u5927\u5927\u5c0f \u540cHashMap\n\nprivate static final int DEFAULT_CAPACITY = 16;\/\/\u6570\u7ec4\u9ed8\u8ba4\u5927\u5c0f\n\nstatic final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; \/\/\u6570\u7ec4\u53ef\u80fd\u6700\u5927\u503c\uff0c\u9700\u8981\u4e0etoArray\uff08\uff09\u76f8\u5173\u65b9\u6cd5\u5173\u8054\n\nprivate static final int DEFAULT_CONCURRENCY_LEVEL = 16; \/\/\u517c\u5bb9\u65e7\u7248\u4fdd\u7559\u7684\u503c\uff0c\u9ed8\u8ba4\u7ebf\u7a0b\u5e76\u53d1\u5ea6\uff0c\u7c7b\u4f3c\u4fe1\u53f7\u91cf\n\nprivate static final float LOAD_FACTOR = 0.75f;\/\/\u9ed8\u8ba4map\u6269\u5bb9\u6bd4\u4f8b\uff0c\u5b9e\u9645\u7528(n &lt;&lt; 1) - (n &gt;&gt;&gt; 1)\u4ee3\u66ff\u4e86\u66f4\u9ad8\u6548\n\nstatic final int TREEIFY_THRESHOLD = 8; \/\/ \u94fe\u8868\u8f6c\u6811\u9600\u503c\uff0c\u5927\u4e8e8\u65f6\n\nstatic final int UNTREEIFY_THRESHOLD = 6; \/\/\u6811\u8f6c\u94fe\u8868\u9600\u503c\uff0c\u5c0f\u4e8e\u7b49\u4e8e6\uff08tranfer\u65f6\uff0clc\u3001hc=0\u4e24\u4e2a\u8ba1\u6570\u5668\u5206\u522b++\u8bb0\u5f55\u539fbin\u3001\u65b0binTreeNode\u6570\u91cf\uff0c&lt;=UNTREEIFY_THRESHOLD \u5219untreeify(lo)\uff09\u3002\u3010\u4ec5\u5728\u6269\u5bb9tranfer\u65f6\u624d\u53ef\u80fd\u6811\u8f6c\u94fe\u8868\u3011\n\nstatic final int MIN_TREEIFY_CAPACITY = 64;\n\nprivate static final int MIN_TRANSFER_STRIDE = 16;\/\/\u6269\u5bb9\u8f6c\u79fb\u65f6\u7684\u6700\u5c0f\u6570\u7ec4\u5206\u7ec4\u5927\u5c0f\n\nprivate static int RESIZE_STAMP_BITS = 16;\/\/\u672c\u7c7b\u4e2d\u6ca1\u63d0\u4f9b\u4fee\u6539\u7684\u65b9\u6cd5 \u7528\u6765\u6839\u636en\u751f\u6210\u4f4d\u7f6e\u4e00\u4e2a\u7c7b\u4f3c\u65f6\u95f4\u6233\u7684\u529f\u80fd\n\nprivate static final int MAX_RESIZERS = (1 &lt;&lt; (32 - RESIZE_STAMP_BITS)) - 1; \/\/ 2^15-1\uff0chelp resize\u7684\u6700\u5927\u7ebf\u7a0b\u6570\n\nprivate static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS; \/\/ 32-16=16\uff0csizeCtl\u4e2d\u8bb0\u5f55size\u5927\u5c0f\u7684\u504f\u79fb\u91cf\n\nstatic final int MOVED = -1; \/\/ hash for forwarding nodes\uff08forwarding nodes\u7684hash\u503c\uff09\u3001\u6807\u793a\u4f4d\n\nstatic final int TREEBIN = -2; \/\/ hash for roots of trees\uff08\u6811\u6839\u8282\u70b9\u7684hash\u503c\uff09\n\nstatic final int RESERVED = -3; \/\/ ReservationNode\u7684hash\u503c\n\nstatic final int HASH_BITS = 0x7fffffff; \/\/ \u7528\u5728\u8ba1\u7b97hash\u65f6\u8fdb\u884c\u5b89\u4f4d\u4e0e\u8ba1\u7b97\u6d88\u9664\u8d1fhash\n\nstatic final int NCPU = Runtime.getRuntime().availableProcessors(); \/\/ \u53ef\u7528\u5904\u7406\u5668\u6570\u91cf\n\n\/* ---------------- Fields -------------- *\/\n\ntransient volatile Node&lt;K,V&gt;[] table; \/\/\u88c5\u8f7dNode\u7684\u6570\u7ec4\uff0c\u4f5c\u4e3aConcurrentHashMap\u7684\u6570\u636e\u5bb9\u5668\uff0c\u91c7\u7528\u61d2\u52a0\u8f7d\u7684\u65b9\u5f0f\uff0c\u76f4\u5230\u7b2c\u4e00\u6b21\u63d2\u5165\u6570\u636e\u7684\u65f6\u5019\u624d\u4f1a\u8fdb\u884c\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u6570\u7ec4\u7684\u5927\u5c0f\u603b\u662f\u4e3a2\u7684\u5e42\u6b21\u65b9\u3002\n\nprivate transient volatile Node&lt;K,V&gt;[] nextTable; \/\/\u6269\u5bb9\u65f6\u4f7f\u7528\uff0c\u5e73\u65f6\u4e3anull\uff0c\u53ea\u6709\u5728\u6269\u5bb9\u7684\u65f6\u5019\u624d\u4e3a\u975enull\n\n\/**\n* \u5b9e\u9645\u4e0a\u4fdd\u5b58\u7684\u662fhashmap\u4e2d\u7684\u5143\u7d20\u4e2a\u6570  \u5229\u7528CAS\u9501\u8fdb\u884c\u66f4\u65b0\u4f46\u5b83\u5e76\u4e0d\u7528\u8fd4\u56de\u5f53\u524dhashmap\u7684\u5143\u7d20\u4e2a\u6570 \n*\/\nprivate transient volatile long baseCount;\n\n\/**\n*\u8be5\u5c5e\u6027\u7528\u6765\u63a7\u5236table\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u6839\u636e\u662f\u5426\u521d\u59cb\u5316\u548c\u662f\u5426\u6b63\u5728\u6269\u5bb9\u6709\u51e0\u79cd\u60c5\u51b5\uff1a\n*\u5f53\u503c\u4e3a\u8d1f\u6570\u65f6\uff1a\u5982\u679c\u4e3a-1\u8868\u793a\u6b63\u5728\u521d\u59cb\u5316\uff0c\u5982\u679c\u4e3a-N\u5219\u8868\u793a\u5f53\u524d\u6b63\u6709N-1\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c\uff1b\n*\u5f53\u503c\u4e3a\u6b63\u6570\u65f6\uff1a\u5982\u679c\u5f53\u524d\u6570\u7ec4\u4e3anull\u7684\u8bdd\u8868\u793atable\u5728\u521d\u59cb\u5316\u8fc7\u7a0b\u4e2d\uff0csizeCtl\u8868\u793a\u4e3a\u9700\u8981\u65b0\u5efa\u6570\u7ec4\u7684\u957f\u5ea6\uff1b\u82e5\u5df2\u7ecf\u521d\u59cb\u5316\u4e86\uff0c\u8868\u793a\u5f53\u524d\u6570\u636e\u5bb9\u5668\uff08table\u6570\u7ec4\uff09\u53ef\u7528\u5bb9\u91cf\u4e5f\u53ef\u4ee5\u7406\u89e3\u6210\u4e34\u754c\u503c\uff08\u63d2\u5165\u8282\u70b9\u6570\u8d85\u8fc7\u4e86\u8be5\u4e34\u754c\u503c\u5c31\u9700\u8981\u6269\u5bb9\uff09\uff0c\u5177\u4f53\u6307\u4e3a\u6570\u7ec4\u7684\u957f\u5ea6n \u4e58\u4ee5 \u52a0\u8f7d\u56e0\u5b50loadFactor\uff1b\u5f53\u503c\u4e3a0\u65f6\uff0c\u5373\u6570\u7ec4\u957f\u5ea6\u4e3a\u9ed8\u8ba4\u521d\u59cb\u503c\u3002\n*\/\nprivate transient volatile int sizeCtl;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">put() \u65b9\u6cd5<\/h4>\n\n\n\n<ol class=\"wp-block-list\"><li>\u9996\u5148\u4f1a\u5224\u65ad key\u3001value\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\u5c31\u629b\u5f02\u5e38\uff01<\/li><li><code>spread()<\/code>\u65b9\u6cd5\u83b7\u53d6hash\uff0c\u51cf\u5c0fhash\u51b2\u7a81<\/li><li>\u5224\u65ad\u662f\u5426\u521d\u59cb\u5316table\u6570\u7ec4\uff0c\u6ca1\u6709\u7684\u8bdd\u8c03\u7528<code>initTable()<\/code>\u65b9\u6cd5\u8fdb\u884c\u521d\u59cb\u5316<\/li><li>\u5224\u65ad\u662f\u5426\u80fd\u76f4\u63a5\u5c06\u65b0\u503c\u63d2\u5165\u5230table\u6570\u7ec4\u4e2d<\/li><li>\u5224\u65ad\u5f53\u524d\u662f\u5426\u5728\u6269\u5bb9\uff0c<code>MOVED<\/code>\u4e3a-1\u8bf4\u660e\u5f53\u524dConcurrentHashMap\u6b63\u5728\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c\uff0c\u6b63\u5728\u6269\u5bb9\u7684\u8bdd\u5c31\u8fdb\u884c\u534f\u52a9\u6269\u5bb9<\/li><li>\u5f53table[i]\u4e3a\u94fe\u8868\u7684\u5934\u7ed3\u70b9\uff0c\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u65b0\u503c\uff0c\u901a\u8fc7synchronized (f)\u7684\u65b9\u5f0f\u8fdb\u884c\u52a0\u9501\u4ee5\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u6027\u3002<ol><li>\u5728\u94fe\u8868\u4e2d\u5982\u679c\u627e\u5230\u4e86\u4e0e\u5f85\u63d2\u5165\u7684\u952e\u503c\u5bf9\u7684key\u76f8\u540c\u7684\u8282\u70b9\uff0c\u5c31\u76f4\u63a5\u8986\u76d6<\/li><li>\u5982\u679c\u6ca1\u6709\u627e\u5230\u7684\u8bdd\uff0c\u5c31\u76f4\u63a5\u5c06\u5f85\u63d2\u5165\u7684\u952e\u503c\u5bf9\u8ffd\u52a0\u5230\u94fe\u8868\u7684\u672b\u5c3e<\/li><\/ol><\/li><li>\u5f53table[i]\u4e3a\u7ea2\u9ed1\u6811\u7684\u6839\u8282\u70b9\uff0c\u5728\u7ea2\u9ed1\u6811\u4e2d\u63d2\u5165\u65b0\u503c\/\u8986\u76d6\u65e7\u503c<\/li><li>\u6839\u636e\u5f53\u524d\u8282\u70b9\u4e2a\u6570\u8fdb\u884c\u8c03\u6574\uff0c\u5426\u9700\u8981\u8f6c\u6362\u6210\u7ea2\u9ed1\u6811(\u4e2a\u6570\u5927\u4e8e\u7b49\u4e8e8\uff0c\u5c31\u4f1a\u8c03\u7528<code>treeifyBin<\/code>\u65b9\u6cd5\u5c06tabel[i]<code>\u7b2ci\u4e2a\u6563\u5217\u6876<\/code>\u62c9\u94fe\u8f6c\u6362\u6210\u7ea2\u9ed1\u6811)<\/li><li>\u5bf9\u5f53\u524d\u5bb9\u91cf\u5927\u5c0f\u8fdb\u884c\u68c0\u67e5\uff0c\u5982\u679c\u8d85\u8fc7\u4e86\u4e34\u754c\u503c\uff08\u5b9e\u9645\u5927\u5c0f*\u52a0\u8f7d\u56e0\u5b50\uff09\u5c31\u8fdb\u884c\u6269\u5bb9<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>final V putVal(K key, V value, boolean onlyIfAbsent) {\n    \/\/ key \u548c value \u5747\u4e0d\u5141\u8bb8\u4e3a null\n    if (key == null || value == null) throw new NullPointerException();\n    \/\/ \u6839\u636e key \u8ba1\u7b97\u51fa hash \u503c\n    int hash = spread(key.hashCode());\n    int binCount = 0;\n    for (Node&lt;K,V&gt;[] tab = table;;) {\n        Node&lt;K,V&gt; f; int n, i, fh;\n         \/\/ \u5224\u65ad\u662f\u5426\u9700\u8981\u8fdb\u884c\u521d\u59cb\u5316\n        if (tab == null || (n = tab.length) == 0)\n            tab = initTable();\n        \/\/ f \u5373\u4e3a\u5f53\u524d key \u5b9a\u4f4d\u51fa\u7684 Node\uff0c\u5982\u679c\u4e3a\u7a7a\u8868\u793a\u5f53\u524d\u4f4d\u7f6e\u53ef\u4ee5\u5199\u5165\u6570\u636e\uff0c\u5229\u7528 CAS \u5c1d\u8bd5\u5199\u5165\uff0c\u5931\u8d25\u5219\u81ea\u65cb\u4fdd\u8bc1\u6210\u529f\n        else if ((f = tabAt(tab, i = (n - 1) &amp; hash)) == null) {\n            if (casTabAt(tab, i, null,\n                         new Node&lt;K,V&gt;(hash, key, value, null)))\n                break;                   \/\/ no lock when adding to empty bin\n        }\n        \/\/ \u5982\u679c\u5f53\u524d\u4f4d\u7f6e\u7684 hashcode == MOVED == -1,\u5219\u9700\u8981\u8fdb\u884c\u6269\u5bb9\n        else if ((fh = f.hash) == MOVED)\n            tab = helpTransfer(tab, f);\n        \/\/ \u5982\u679c\u90fd\u4e0d\u6ee1\u8db3\uff0c\u5219\u5229\u7528 synchronized \u9501\u5199\u5165\u6570\u636e\n        else {\n          \/\/ \u5269\u4e0b\u60c5\u51b5\u53c8\u5206\u4e24\u79cd\uff0c\u63d2\u5165\u94fe\u8868\u3001\u63d2\u5165\u7ea2\u9ed1\u6811\n            V oldVal = null;\n          \/\/\u91c7\u7528\u540c\u6b65\u5185\u7f6e\u9501\u5b9e\u73b0\u5e76\u53d1\u63a7\u5236\n            synchronized (f) {\n                if (tabAt(tab, i) == f) {\n                    \/\/ \u5982\u679c fh=f.hash &gt;=0\uff0c\u5f53\u524d\u4e3a\u94fe\u8868\uff0c\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u65b0\u7684\u952e\u503c\u5bf9\n                    if (fh &gt;= 0) {\n                        binCount = 1;\n                      \/\/\u904d\u5386\u94fe\u8868\uff0c\u5982\u679c\u627e\u5230\u5bf9\u5e94\u7684 node \u8282\u70b9\uff0c\u4fee\u6539 value,\u5426\u5219\u76f4\u63a5\u5728\u94fe\u8868\u5c3e\u90e8\u52a0\u5165\u8282\u70b9\n                        for (Node&lt;K,V&gt; e = f;; ++binCount) {\n                            K ek;\n                            if (e.hash == hash &amp;&amp;\n                                ((ek = e.key) == key ||\n                                 (ek != null &amp;&amp; key.equals(ek)))) {\n                                oldVal = e.val;\n                                if (!onlyIfAbsent)\n                                    e.val = value;\n                                break;\n                            }\n                            Node&lt;K,V&gt; pred = e;\n                            if ((e = e.next) == null) {\n                                pred.next = new Node&lt;K,V&gt;(hash, key,\n                                                          value, null);\n                                break;\n                            }\n                        }\n                    }\n                    \/\/ \u5f53\u524d\u4e3a\u7ea2\u9ed1\u6811\uff0c\u5c06\u65b0\u7684\u952e\u503c\u5bf9\u63d2\u5165\u5230\u7ea2\u9ed1\u6811\u4e2d\n                    else if (f instanceof TreeBin) {\n                        Node&lt;K,V&gt; p;\n                        binCount = 2;\n                        if ((p = ((TreeBin&lt;K,V&gt;)f).putTreeVal(hash, key,\n                                                       value)) != null) {\n                            oldVal = p.val;\n                            if (!onlyIfAbsent)\n                                p.val = value;\n                        }\n                    }\n                }\n            }\n            \/\/ \u63d2\u5165\u5b8c\u952e\u503c\u5bf9\u540e\u518d\u6839\u636e\u5b9e\u9645\u5927\u5c0f\u770b\u662f\u5426\u9700\u8981\u8f6c\u6362\u6210\u7ea2\u9ed1\u6811\n            if (binCount != 0) {\n                if (binCount &gt;= TREEIFY_THRESHOLD)\n                    treeifyBin(tab, i);\n                if (oldVal != null)\n                    return oldVal;\n                break;\n            }\n        }\n    }\n    \/\/ \u5bf9\u5f53\u524d\u5bb9\u91cf\u5927\u5c0f\u8fdb\u884c\u68c0\u67e5\uff0c\u5982\u679c\u8d85\u8fc7\u4e86\u4e34\u754c\u503c\uff08\u5b9e\u9645\u5927\u5c0f*\u52a0\u8f7d\u56e0\u5b50\uff09\u5c31\u9700\u8981\u6269\u5bb9 \n    addCount(1L, binCount);\n    return null;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0JDK8\u4e2d\u7684\u5b9e\u73b0\u4e5f\u662f<strong>\u9501\u5206\u79bb<\/strong>\u7684\u601d\u60f3\uff0c\u53ea\u662f\u9501\u4f4f\u7684\u662f\u4e00\u4e2aNode\uff0c\u800c\u4e0d\u662fJDK7\u4e2d\u7684Segment\uff0c\u800c\u9501\u4f4fNode\u4e4b\u524d\u7684\u64cd\u4f5c\u662f\u65e0\u9501\u7684\u5e76\u4e14\u4e5f\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u5efa\u7acb\u5728\u4e4b\u524d\u63d0\u5230\u7684\u539f\u5b50\u64cd\u4f5c\u4e0a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">get() \u65b9\u6cd5<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">get\u65b9\u6cd5\u65e0\u9700\u52a0\u9501\uff0c\u7531\u4e8e\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u5171\u4eab\u53d8\u91cf\u90fd\u4f7f\u7528volatile\u4fee\u9970\uff0cvolatile\u53ef\u4ee5\u4fdd\u8bc1\u5185\u5b58\u53ef\u89c1\u6027\uff0c\u6240\u4ee5\u4e0d\u4f1a\u8bfb\u53d6\u5230\u8fc7\u671f\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public V get(Object key) {\n  Node&lt;K,V&gt;[] tab; Node&lt;K,V&gt; e, p; int n, eh; K ek;\n  int h = spread(key.hashCode());\n  \/\/ \u5224\u65ad\u6570\u7ec4\u662f\u5426\u4e3a\u7a7a\n  if ((tab = table) != null &amp;&amp; (n = tab.length) &gt; 0 &amp;&amp;\n      (e = tabAt(tab, (n - 1) &amp; h)) != null) {\n    \/\/ \u5224\u65adnode \u8282\u70b9\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u4e0d\u662f\u8981\u627e\u7684\uff0c\u5982\u679c\u662f\u76f4\u63a5\u8fd4\u56de\n    if ((eh = e.hash) == h) {\n      if ((ek = e.key) == key || (ek != null &amp;&amp; key.equals(ek)))\n        return e.val;\n    }\n    \/\/ \/\/ hash\u5c0f\u4e8e0,\u8bf4\u660e\u662f\u7279\u6b8a\u8282\u70b9\uff08TreeBin\u6216ForwardingNode\uff09\u8c03\u7528find\n    else if (eh &lt; 0)\n      return (p = e.find(h, key)) != null ? p.val : null;\n    \/\/ \u4e0d\u662f\u4e0a\u9762\u7684\u60c5\u51b5,\u90a3\u5c31\u662f\u94fe\u8868\u4e86,\u904d\u5386\u94fe\u8868\n    while ((e = e.next) != null) {\n      if (e.hash == h &amp;&amp;\n          ((ek = e.key) == key || (ek != null &amp;&amp; key.equals(ek))))\n        return e.val;\n    }\n  }\n  return null;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Hashtable \u548c ConcurrentHashMap \u7684\u533a\u522b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ConcurrentHashMap \u548c Hashtable \u7684\u533a\u522b\u4e3b\u8981\u4f53\u73b0\u5728\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u65b9\u5f0f\u4e0a\u4e0d\u540c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>\u5e95\u5c42\u6570\u636e\u7ed3\u6784\uff1a<\/strong>&nbsp;JDK1.7\u7684 ConcurrentHashMap \u5e95\u5c42\u91c7\u7528&nbsp;<strong>\u5206\u6bb5\u7684\u6570\u7ec4+\u94fe\u8868<\/strong>&nbsp;\u5b9e\u73b0\uff0cJDK1.8 \u91c7\u7528\u7684\u6570\u636e\u7ed3\u6784\u548cHashMap1.8\u7684\u7ed3\u6784\u7c7b\u4f3c\uff0c<strong>\u6570\u7ec4+\u94fe\u8868\/\u7ea2\u9ed1\u4e8c\u53c9\u6811<\/strong>\u3002Hashtable \u548c JDK1.8 \u4e4b\u524d\u7684 HashMap \u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u7c7b\u4f3c\u90fd\u662f\u91c7\u7528&nbsp;<strong>\u6570\u7ec4+\u94fe\u8868<\/strong>&nbsp;\u7684\u5f62\u5f0f\uff0c\u6570\u7ec4\u662f HashMap \u7684\u4e3b\u4f53\uff0c\u94fe\u8868\u5219\u662f\u4e3b\u8981\u4e3a\u4e86\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u800c\u5b58\u5728\u7684\uff1b<\/li><li><strong>\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u65b9\u5f0f\uff08\u91cd\u8981\uff09\uff1a<\/strong><ul><li><strong>\u5728JDK1.7\u7684\u65f6\u5019\uff0cConcurrentHashMap\uff08\u5206\u6bb5\u9501\uff09<\/strong>&nbsp;\u5bf9\u6574\u4e2a\u6876\u6570\u7ec4\u8fdb\u884c\u4e86\u5206\u5272\u5206\u6bb5(Segment)\uff0c\u6bcf\u4e00\u628a\u9501\u53ea\u9501\u5bb9\u5668\u5176\u4e2d\u4e00\u90e8\u5206\u6570\u636e\uff0c\u591a\u7ebf\u7a0b\u8bbf\u95ee\u5bb9\u5668\u91cc\u4e0d\u540c\u6570\u636e\u6bb5\u7684\u6570\u636e\uff0c\u5c31\u4e0d\u4f1a\u5b58\u5728\u9501\u7ade\u4e89\uff0c\u63d0\u9ad8\u5e76\u53d1\u8bbf\u95ee\u7387\u3002\uff08\u9ed8\u8ba4\u5206\u914d16\u4e2aSegment\uff0c\u6bd4Hashtable\u6548\u7387\u63d0\u9ad816\u500d\u3002\uff09&nbsp;<strong>\u5230\u4e86 JDK1.8 \u7684\u65f6\u5019\u5df2\u7ecf\u6452\u5f03\u4e86Segment\u7684\u6982\u5ff5\uff0c\u800c\u662f\u76f4\u63a5\u7528 Node \u6570\u7ec4+\u94fe\u8868\/\u7ea2\u9ed1\u6811\u7684\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0\uff0c\u5e76\u53d1\u63a7\u5236\u4f7f\u7528 synchronized \u548c CAS \u6765\u64cd\u4f5c\u3002\uff08JDK1.6\u4ee5\u540e \u5bf9 synchronized\u9501\u505a\u4e86\u5f88\u591a\u4f18\u5316\uff09<\/strong>&nbsp;\u6574\u4e2a\u770b\u8d77\u6765\u5c31\u50cf\u662f\u4f18\u5316\u8fc7\u4e14\u7ebf\u7a0b\u5b89\u5168\u7684 HashMap\uff0c\u867d\u7136\u5728 JDK1.8 \u4e2d\u8fd8\u80fd\u770b\u5230 Segment \u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u662f\u5df2\u7ecf\u7b80\u5316\u4e86\u5c5e\u6027\uff0c\u53ea\u662f\u4e3a\u4e86\u517c\u5bb9\u65e7\u7248\u672c\uff1b<\/li><li>Hashtable(\u540c\u4e00\u628a\u9501) :\u4f7f\u7528 synchronized \u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff0c\u6548\u7387\u975e\u5e38\u4f4e\u4e0b\u3002\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u6b65\u65b9\u6cd5\u65f6\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4e5f\u8bbf\u95ee\u540c\u6b65\u65b9\u6cd5\uff0c\u53ef\u80fd\u4f1a\u8fdb\u5165\u963b\u585e\u6216\u8f6e\u8be2\u72b6\u6001\uff0c\u5982\u4f7f\u7528 put \u6dfb\u52a0\u5143\u7d20\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4e0d\u80fd\u4f7f\u7528 put \u6dfb\u52a0\u5143\u7d20\uff0c\u4e5f\u4e0d\u80fd\u4f7f\u7528 get\uff0c\u7ade\u4e89\u8d8a\u6fc0\u70c8\u6548\u7387\u8d8a\u4f4e\u3002<\/li><\/ul><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Java\u5feb\u901f\u5931\u8d25\uff08fail-fast\uff09\u548c\u5b89\u5168\u5931\u8d25\uff08fail-safe\uff09\u533a\u522b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5feb\u901f\u5931\u8d25\uff08fail\u2014fast\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7528\u8fed\u4ee3\u5668\u904d\u5386\u4e00\u4e2a\u96c6\u5408\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u904d\u5386\u8fc7\u7a0b\u4e2d\u5bf9\u96c6\u5408\u5bf9\u8c61\u7684\u5185\u5bb9\u8fdb\u884c\u4e86\u4fee\u6539\uff08\u589e\u52a0\u3001\u5220\u9664\u3001\u4fee\u6539\uff09\uff0c\u5219\u4f1a\u629b\u51faConcurrentModificationException\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u539f\u7406\uff1a\u8fed\u4ee3\u5668\u5728\u904d\u5386\u65f6\u76f4\u63a5\u8bbf\u95ee\u96c6\u5408\u4e2d\u7684\u5185\u5bb9\uff0c\u5e76\u4e14\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u4e00\u4e2a modCount \u53d8\u91cf\u3002\u96c6\u5408\u5728\u88ab\u904d\u5386\u671f\u95f4\u5982\u679c\u5185\u5bb9\u53d1\u751f\u53d8\u5316\uff0c\u5c31\u4f1a\u6539\u53d8 modCount \u7684\u503c\u3002\u6bcf\u5f53\u8fed\u4ee3\u5668\u4f7f\u7528 hashNext()\/next() \u904d\u5386\u4e0b\u4e00\u4e2a\u5143\u7d20\u4e4b\u524d\uff0c\u90fd\u4f1a\u68c0\u6d4b modCount \u53d8\u91cf\u662f\u5426\u4e3a expectedmodCount \u503c\uff0c\u662f\u7684\u8bdd\u5c31\u8fd4\u56de\u904d\u5386\uff1b\u5426\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u7ec8\u6b62\u904d\u5386\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1a\u8fd9\u91cc\u5f02\u5e38\u7684\u629b\u51fa\u6761\u4ef6\u662f\u68c0\u6d4b\u5230 modCount\uff01=expectedmodCount \u8fd9\u4e2a\u6761\u4ef6\u3002\u5982\u679c\u96c6\u5408\u53d1\u751f\u53d8\u5316\u65f6\u4fee\u6539modCount \u503c\u521a\u597d\u53c8\u8bbe\u7f6e\u4e3a\u4e86 expectedmodCount \u503c\uff0c\u5219\u5f02\u5e38\u4e0d\u4f1a\u629b\u51fa\u3002\u56e0\u6b64\uff0c\u4e0d\u80fd\u4f9d\u8d56\u4e8e\u8fd9\u4e2a\u5f02\u5e38\u662f\u5426\u629b\u51fa\u800c\u8fdb\u884c\u5e76\u53d1\u64cd\u4f5c\u7684\u7f16\u7a0b\uff0c\u8fd9\u4e2a\u5f02\u5e38\u53ea\u5efa\u8bae\u7528\u4e8e\u68c0\u6d4b\u5e76\u53d1\u4fee\u6539\u7684bug\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u573a\u666f\uff1ajava.util\u5305\u4e0b\u7684\u96c6\u5408\u7c7b\u90fd\u662f\u5feb\u901f\u5931\u8d25\u7684\uff0c\u4e0d\u80fd\u5728\u591a\u7ebf\u7a0b\u4e0b\u53d1\u751f\u5e76\u53d1\u4fee\u6539\uff08\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u88ab\u4fee\u6539\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b89\u5168\u5931\u8d25\uff08fail\u2014safe\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u91c7\u7528\u5b89\u5168\u5931\u8d25\u673a\u5236\u7684\u96c6\u5408\u5bb9\u5668\uff0c\u5728\u904d\u5386\u65f6\u4e0d\u662f\u76f4\u63a5\u5728\u96c6\u5408\u5185\u5bb9\u4e0a\u8bbf\u95ee\u7684\uff0c\u800c\u662f\u5148\u590d\u5236\u539f\u6709\u96c6\u5408\u5185\u5bb9\uff0c\u5728\u62f7\u8d1d\u7684\u96c6\u5408\u4e0a\u8fdb\u884c\u904d\u5386\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u539f\u7406\uff1a\u7531\u4e8e\u8fed\u4ee3\u65f6\u662f\u5bf9\u539f\u96c6\u5408\u7684\u62f7\u8d1d\u8fdb\u884c\u904d\u5386\uff0c\u6240\u4ee5\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\u5bf9\u539f\u96c6\u5408\u6240\u4f5c\u7684\u4fee\u6539\u5e76\u4e0d\u80fd\u88ab\u8fed\u4ee3\u5668\u68c0\u6d4b\u5230\uff0c\u6240\u4ee5\u4e0d\u4f1a\u89e6\u53d1 Concurrent Modification Exception\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f3a\u70b9\uff1a\u57fa\u4e8e\u62f7\u8d1d\u5185\u5bb9\u7684\u4f18\u70b9\u662f\u907f\u514d\u4e86Concurrent Modification Exception\uff0c\u4f46\u540c\u6837\u5730\uff0c\u8fed\u4ee3\u5668\u5e76\u4e0d\u80fd\u8bbf\u95ee\u5230\u4fee\u6539\u540e\u7684\u5185\u5bb9\uff0c\u5373\uff1a\u8fed\u4ee3\u5668\u904d\u5386\u7684\u662f\u5f00\u59cb\u904d\u5386\u90a3\u4e00\u523b\u62ff\u5230\u7684\u96c6\u5408\u62f7\u8d1d\uff0c\u5728\u904d\u5386\u671f\u95f4\u539f\u96c6\u5408\u53d1\u751f\u7684\u4fee\u6539\u8fed\u4ee3\u5668\u662f\u4e0d\u77e5\u9053\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u573a\u666f\uff1ajava.util.concurrent\u5305\u4e0b\u7684\u5bb9\u5668\u90fd\u662f\u5b89\u5168\u5931\u8d25\uff0c\u53ef\u4ee5\u5728\u591a\u7ebf\u7a0b\u4e0b\u5e76\u53d1\u4f7f\u7528\uff0c\u5e76\u53d1\u4fee\u6539\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5feb\u901f\u5931\u8d25\u548c\u5b89\u5168\u5931\u8d25\u662f\u5bf9\u8fed\u4ee3\u5668\u800c\u8a00\u7684\u3002 \u5feb\u901f\u5931\u8d25\uff1a\u5f53\u5728\u8fed\u4ee3\u4e00\u4e2a\u96c6\u5408\u7684\u65f6\u5019\uff0c\u5982\u679c\u6709\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u5728\u4fee\u6539\u8fd9\u4e2a\u96c6\u5408\uff0c\u5c31\u4f1a\u629b\u51faConcurrentModification\u5f02\u5e38\uff0cjava.util\u4e0b\u90fd\u662f\u5feb\u901f\u5931\u8d25\u3002 \u5b89\u5168\u5931\u8d25\uff1a\u5728\u8fed\u4ee3\u65f6\u5019\u4f1a\u5728\u96c6\u5408\u4e8c\u5c42\u505a\u4e00\u4e2a\u62f7\u8d1d\uff0c\u6240\u4ee5\u5728\u4fee\u6539\u96c6\u5408\u4e0a\u5c42\u5143\u7d20\u4e0d\u4f1a\u5f71\u54cd\u4e0b\u5c42\u3002\u5728java.util.concurrent\u4e0b\u90fd\u662f\u5b89\u5168\u5931\u8d25<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u907f\u514d<strong>fail-fast<\/strong>&nbsp;\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5728\u5355\u7ebf\u7a0b\u7684\u904d\u5386\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u8981\u8fdb\u884cremove\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8c03\u7528\u8fed\u4ee3\u5668 ListIterator \u7684 remove \u65b9\u6cd5\u800c\u4e0d\u662f\u96c6\u5408\u7c7b\u7684 remove\u65b9\u6cd5\u3002\u770b\u770b ArrayList\u4e2d\u8fed\u4ee3\u5668\u7684 remove\u65b9\u6cd5\u7684\u6e90\u7801\uff0c\u8be5\u65b9\u6cd5\u4e0d\u80fd\u6307\u5b9a\u5143\u7d20\u5220\u9664\uff0c\u53ea\u80fdremove\u5f53\u524d\u904d\u5386\u5143\u7d20\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public void remove() {\n    if (lastRet &lt; 0)\n        throw new IllegalStateException();\n    checkForComodification();\n\n    try {\n        SubList.this.remove(lastRet);\n        cursor = lastRet;\n        lastRet = -1;\n        expectedModCount = ArrayList.this.modCount;  \/\/\n    } catch (IndexOutOfBoundsException ex) {\n        throw new ConcurrentModificationException();\n    }\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f7f\u7528\u5e76\u53d1\u5305(<code>java.util.concurrent<\/code>)\u4e2d\u7684\u7c7b\u6765\u4ee3\u66ff ArrayList \u548c hashMap<ul><li>CopyOnWriterArrayList \u4ee3\u66ff ArrayList<\/li><li>ConcurrentHashMap \u4ee3\u66ff HashMap<\/li><\/ul><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Iterator \u548c Enumeration \u533a\u522b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Java\u96c6\u5408\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u90fd\u901a\u8fc7 \u201cIterator(\u8fed\u4ee3\u5668)\u201d \u6216 \u201cEnumeration(\u679a\u4e3e\u7c7b)\u201d \u53bb\u904d\u5386\u96c6\u5408\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface Enumeration&lt;E&gt; {\n    boolean hasMoreElements();\n    E nextElement();\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface Iterator&lt;E&gt; {\n    boolean hasNext();\n    E next();\n    void remove();\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>\u51fd\u6570\u63a5\u53e3\u4e0d\u540c<\/strong>\uff0cEnumeration<strong>\u53ea\u67092\u4e2a\u51fd\u6570\u63a5\u53e3\u3002<\/strong>\u901a\u8fc7Enumeration\uff0c\u6211\u4eec\u53ea\u80fd\u8bfb\u53d6\u96c6\u5408\u7684\u6570\u636e\uff0c\u800c\u4e0d\u80fd\u5bf9\u6570\u636e\u8fdb\u884c\u4fee\u6539\u3002Iterator<strong>\u53ea\u67093\u4e2a\u51fd\u6570\u63a5\u53e3\u3002<\/strong>Iterator\u9664\u4e86\u80fd\u8bfb\u53d6\u96c6\u5408\u7684\u6570\u636e\u4e4b\u5916\uff0c\u4e5f\u80fd\u6570\u636e\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u3002<\/li><li><strong>Iterator\u652f\u6301 fail-fast\u673a\u5236\uff0c\u800cEnumeration\u4e0d\u652f\u6301<\/strong>\u3002Enumeration \u662fJDK 1.0\u6dfb\u52a0\u7684\u63a5\u53e3\u3002\u4f7f\u7528\u5230\u5b83\u7684\u51fd\u6570\u5305\u62ecVector\u3001Hashtable\u7b49\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u90fd\u662fJDK 1.0\u4e2d\u52a0\u5165\u7684\uff0cEnumeration\u5b58\u5728\u7684\u76ee\u7684\u5c31\u662f\u4e3a\u5b83\u4eec\u63d0\u4f9b\u904d\u5386\u63a5\u53e3\u3002Enumeration\u672c\u8eab\u5e76\u6ca1\u6709\u652f\u6301\u540c\u6b65\uff0c\u800c\u5728Vector\u3001Hashtable\u5b9e\u73b0Enumeration\u65f6\uff0c\u6dfb\u52a0\u4e86\u540c\u6b65\u3002<br>\u800cIterator \u662fJDK 1.2\u624d\u6dfb\u52a0\u7684\u63a5\u53e3\uff0c\u5b83\u4e5f\u662f\u4e3a\u4e86HashMap\u3001ArrayList\u7b49\u96c6\u5408\u63d0\u4f9b\u904d\u5386\u63a5\u53e3\u3002Iterator\u662f\u652f\u6301fail-fast\u673a\u5236\u7684\uff1a\u5f53\u591a\u4e2a\u7ebf\u7a0b\u5bf9\u540c\u4e00\u4e2a\u96c6\u5408\u7684\u5185\u5bb9\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u5c31\u53ef\u80fd\u4f1a\u4ea7\u751ffail-fast\u4e8b\u4ef6<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Comparable \u548c Comparator\u63a5\u53e3\u6709\u4f55\u533a\u522b\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Java\u4e2d\u5bf9\u96c6\u5408\u5bf9\u8c61\u6216\u8005\u6570\u7ec4\u5bf9\u8c61\u6392\u5e8f\uff0c\u6709\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5bf9\u8c61\u5b9e\u73b0Comparable \u63a5\u53e3<ul><li>Comparable \u5728 java.lang \u5305\u4e0b\uff0c\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u5185\u90e8\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5 compareTo()<code>public interface Comparable&lt;T&gt; {\n    public int compareTo(T o);\n}<\/code><\/li><li>Comparable \u53ef\u4ee5\u8ba9\u5b9e\u73b0\u5b83\u7684\u7c7b\u7684\u5bf9\u8c61\u8fdb\u884c\u6bd4\u8f83\uff0c\u5177\u4f53\u7684\u6bd4\u8f83\u89c4\u5219\u662f\u6309\u7167 compareTo \u65b9\u6cd5\u4e2d\u7684\u89c4\u5219\u8fdb\u884c\u3002\u8fd9\u79cd\u987a\u5e8f\u79f0\u4e3a&nbsp;<strong>\u81ea\u7136\u987a\u5e8f<\/strong>\u3002<\/li><li>\u5b9e\u73b0\u4e86 Comparable \u63a5\u53e3\u7684 List \u6216\u5219\u6570\u7ec4\u53ef\u4ee5\u4f7f\u7528&nbsp;<code>Collections.sort()<\/code>&nbsp;\u6216\u8005&nbsp;<code>Arrays.sort()<\/code>&nbsp;\u65b9\u6cd5\u8fdb\u884c\u6392\u5e8f<\/li><\/ul><\/li><li>\u5b9a\u4e49\u6bd4\u8f83\u5668\uff0c\u5b9e\u73b0 Comparator\u63a5\u53e3<ul><li>Comparator \u5728 java.util \u5305\u4e0b\uff0c\u4e5f\u662f\u4e00\u4e2a\u63a5\u53e3\uff0cJDK 1.8 \u4ee5\u524d\u53ea\u6709\u4e24\u4e2a\u65b9\u6cd5\uff1a<code>public interface Comparator&lt;T&gt; {\n    public int compare(T lhs, T rhs);\n    public boolean equals(Object object);\n}<\/code><\/li><\/ul><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>comparable\u76f8\u5f53\u4e8e\u5185\u90e8\u6bd4\u8f83\u5668\u3002comparator\u76f8\u5f53\u4e8e\u5916\u90e8\u6bd4\u8f83\u5668<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u533a\u522b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Comparator \u4f4d\u4e8e&nbsp;<code>java.util<\/code>&nbsp;\u5305\u4e0b\uff0c\u800c Comparable \u4f4d\u4e8e&nbsp;<code>java.lang<\/code>&nbsp;\u5305\u4e0b<\/li><li>Comparable \u63a5\u53e3\u7684\u5b9e\u73b0\u662f\u5728\u7c7b\u7684\u5185\u90e8\uff08\u5982 String\u3001Integer\u5df2\u7ecf\u5b9e\u73b0\u4e86 Comparable \u63a5\u53e3\uff0c\u81ea\u5df1\u5c31\u53ef\u4ee5\u5b8c\u6210\u6bd4\u8f83\u5927\u5c0f\u64cd\u4f5c\uff09\uff0cComparator \u63a5\u53e3\u7684\u5b9e\u73b0\u662f\u5728\u7c7b\u7684\u5916\u90e8\uff08\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u662f\u81ea\u5df2\u5b8c\u6210\u6bd4\u8f83\uff0c\u4e00\u4e2a\u662f\u5916\u90e8\u7a0b\u5e8f\u5b9e\u73b0\u6bd4\u8f83\uff09<\/li><li>\u5b9e\u73b0 Comparable \u63a5\u53e3\u8981\u91cd\u5199 compareTo \u65b9\u6cd5, \u5728 compareTo \u65b9\u6cd5\u91cc\u9762\u5b9e\u73b0\u6bd4\u8f83\u3002\u4e00\u4e2a\u5df2\u7ecf\u5b9e\u73b0Comparable \u7684\u7c7b\u7684\u5bf9\u8c61\u6216\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7&nbsp;<strong>Collections.sort(list) \u6216\u8005 Arrays.sort(arr)<\/strong>\u5b9e\u73b0\u6392\u5e8f\u3002\u901a\u8fc7&nbsp;<strong>Collections.sort(list,Collections.reverseOrder())<\/strong>&nbsp;\u5bf9list\u8fdb\u884c\u5012\u5e8f\u6392\u5217\u3002<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/007S8ZIlly1gdzefrcjolj30ui0u0dm0.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5b9e\u73b0Comparator\u9700\u8981\u91cd\u5199 compare \u65b9\u6cd5<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/007S8ZIlly1gdzefws30uj311s0t643v.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">HashSet<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">HashSet\u662f\u7528\u6765\u5b58\u50a8\u6ca1\u6709\u91cd\u590d\u5143\u7d20\u7684\u96c6\u5408\u7c7b\uff0c\u5e76\u4e14\u5b83\u662f\u65e0\u5e8f\u7684\u3002HashSet \u5185\u90e8\u5b9e\u73b0\u662f\u57fa\u4e8e HashMap \uff0c\u5b9e\u73b0\u4e86 Set \u63a5\u53e3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece HahSet \u63d0\u4f9b\u7684\u6784\u9020\u5668\u53ef\u4ee5\u770b\u51fa\uff0c\u9664\u4e86\u6700\u540e\u4e00\u4e2a HashSet \u7684\u6784\u9020\u65b9\u6cd5\u5916\uff0c\u5176\u4ed6\u6240\u6709\u5185\u90e8\u5c31\u662f\u53bb\u521b\u5efa\u4e00\u4e2a Hashap \u3002\u6ca1\u6709\u5176\u4ed6\u7684\u64cd\u4f5c\u3002\u800c\u6700\u540e\u4e00\u4e2a\u6784\u9020\u65b9\u6cd5\u4e0d\u662f public \u7684\uff0c\u6240\u4ee5\u4e0d\u5bf9\u5916\u516c\u5f00\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/007S8ZIlly1gdzeqxhvj3j311s0ka787.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">HashSet\u5982\u4f55\u68c0\u67e5\u91cd\u590d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">HashSet\u7684\u5e95\u5c42\u5176\u5b9e\u5c31\u662fHashMap\uff0c\u53ea\u4e0d\u8fc7\u6211\u4eec<strong>HashSet\u662f\u5b9e\u73b0\u4e86Set\u63a5\u53e3\u5e76\u4e14\u628a\u6570\u636e\u4f5c\u4e3aK\u503c\uff0c\u800cV\u503c\u4e00\u76f4\u4f7f\u7528\u4e00\u4e2a\u76f8\u540c\u7684\u865a\u503c\u6765\u4fdd\u5b58<\/strong>\uff0cHashMap\u7684K\u503c\u672c\u8eab\u5c31\u4e0d\u5141\u8bb8\u91cd\u590d\uff0c\u5e76\u4e14\u5728HashMap\u4e2d\u5982\u679cK\/V\u76f8\u540c\u65f6\uff0c\u4f1a\u7528\u65b0\u7684V\u8986\u76d6\u6389\u65e7\u7684V\uff0c\u7136\u540e\u8fd4\u56de\u65e7\u7684V\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tva1.sinaimg.cn\/large\/007S8ZIlly1gdzeyvg5h0j311s0jen0n.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Iterater \u548c ListIterator \u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Iterator\u6765\u904d\u5386Set\u548cList\u96c6\u5408\uff0c\u800cListIterator\u53ea\u80fd\u904d\u5386List<\/li><li>ListIterator\u6709add\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5411List\u4e2d\u6dfb\u52a0\u5bf9\u8c61\uff0c\u800cIterator\u4e0d\u80fd<\/li><li>ListIterator\u548cIterator\u90fd\u6709hasNext()\u548cnext()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u987a\u5e8f\u5411\u540e\u904d\u5386\uff0c\u4f46\u662fListIterator\u6709hasPrevious()\u548cprevious()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9006\u5411\uff08\u987a\u5e8f\u5411\u524d\uff09\u904d\u5386\u3002Iterator\u4e0d\u53ef\u4ee5<\/li><li>ListIterator\u53ef\u4ee5\u5b9a\u4f4d\u5f53\u524d\u7d22\u5f15\u7684\u4f4d\u7f6e\uff0cnextIndex()\u548cpreviousIndex()\u53ef\u4ee5\u5b9e\u73b0\u3002Iterator\u6ca1\u6709\u6b64\u529f\u80fd<\/li><li>\u90fd\u53ef\u5b9e\u73b0\u5220\u9664\u64cd\u4f5c\uff0c\u4f46\u662f ListIterator\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u8c61\u7684\u4fee\u6539\uff0cset()\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u3002Iterator\u4ec5\u80fd\u904d\u5386\uff0c\u4e0d\u80fd\u4fee\u6539<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">https:\/\/www.javatpoint.com\/java-arraylist<br>https:\/\/www.runoob.com\/java\/java-collections.html<br>https:\/\/www.javazhiyin.com\/21717.html<br>https:\/\/yuqirong.me\/2018\/01\/31\/LinkedList\u5185\u90e8\u539f\u7406\u89e3\u6790\/<br>https:\/\/youzhixueyuan.com\/the-underlying-structure-and-principle-of-hashmap.html<br>\u300aHashMap\u6e90\u7801\u8be6\u7ec6\u5206\u6790\u300bhttp:\/\/www.tianxiaobo.com\/2018\/01\/18\/HashMap-\u6e90\u7801\u8be6\u7ec6\u5206\u6790-JDK1-8\/<br>\u300aConcurrentHashMap1.7\u6e90\u7801\u5206\u6790\u300bhttps:\/\/www.cnblogs.com\/chengxiao\/p\/6842045.html<br>http:\/\/www.justdojava.com\/2019\/12\/18\/java-collection-15.1\/<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5004\">Java\u96c6\u5408\u4e00\u7f51\u6253\u5c3d\uff01<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u76f8\u4fe1\u5728\u9762\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u4e5f\u88ab\u95ee\u8fc7 Java \u96c6\u5408\u77e5\u8bc6\u70b9\uff0c\u6bd4\u5982\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u4e9b\u9898\uff01 \u8bf4\u8bf4\u5e38\u89c1\u7684\u96c6\u5408\u6709\u54ea\u4e9b\u5427\uff1f HashM [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1164,1161,1779,1162,410],"class_list":["post-5004","post","type-post","status-publish","format-standard","hentry","category-java","tag-concurrenthashmap","tag-hashmap","tag-hashset","tag-hashtable","tag-410"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5004"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5004\/revisions"}],"predecessor-version":[{"id":5005,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5004\/revisions\/5005"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}