{"id":2256,"date":"2025-03-02T18:42:31","date_gmt":"2025-03-02T10:42:31","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2256"},"modified":"2025-03-02T18:42:31","modified_gmt":"2025-03-02T10:42:31","slug":"java-hashset","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/02\/java-hashset\/","title":{"rendered":"Java HashSet"},"content":{"rendered":"\n<p><code>HashSet<\/code> \u662f Java \u96c6\u5408\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7b\uff0c\u5c5e\u4e8e <strong>Set<\/strong> \u63a5\u53e3\u7684\u5b9e\u73b0\uff0c\u5b83\u57fa\u4e8e\u54c8\u5e0c\u8868\uff08<code>HashMap<\/code>\uff09\u6765\u5b58\u50a8\u5143\u7d20\u3002<code>HashSet<\/code> \u5177\u6709\u96c6\u5408\u7684\u57fa\u672c\u7279\u6027\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3001\u65e0\u5e8f\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>HashSet \u7684\u57fa\u672c\u7279\u6027<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20<\/strong>\uff1a<code>HashSet<\/code> \u4e0d\u4f1a\u5b58\u50a8\u91cd\u590d\u7684\u5143\u7d20\u3002\u5982\u679c\u4f60\u5c1d\u8bd5\u6dfb\u52a0\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u5143\u7d20\uff0c\u5b83\u4f1a\u5ffd\u7565\u8be5\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u65e0\u5e8f\u6027<\/strong>\uff1a<code>HashSet<\/code> \u4e2d\u7684\u5143\u7d20\u6ca1\u6709\u56fa\u5b9a\u7684\u987a\u5e8f\uff0c\u56e0\u6b64\u4e0d\u80fd\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u3002\u5b83\u7684\u8fed\u4ee3\u987a\u5e8f\u53ef\u80fd\u4e0e\u5143\u7d20\u63d2\u5165\u7684\u987a\u5e8f\u4e0d\u540c\u3002<\/li>\n\n\n\n<li><strong>\u5feb\u901f\u67e5\u627e<\/strong>\uff1a\u7531\u4e8e <code>HashSet<\/code> \u4f7f\u7528\u54c8\u5e0c\u8868\u5b58\u50a8\u5143\u7d20\uff0c\u5b83\u7684\u67e5\u627e\u3001\u6dfb\u52a0\u3001\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a O(1)\uff0c\u4f46\u662f\u4e5f\u53ef\u80fd\u56e0\u4e3a\u54c8\u5e0c\u51b2\u7a81\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002<\/li>\n\n\n\n<li><strong>\u5141\u8bb8\u5b58\u50a8 <code>null<\/code><\/strong>\uff1a<code>HashSet<\/code> \u53ef\u4ee5\u5b58\u50a8\u4e00\u4e2a <code>null<\/code> \u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>HashSet \u7684\u5e38\u7528\u6784\u9020\u51fd\u6570<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684 HashSet\uff0c\u9ed8\u8ba4\u521d\u59cb\u5bb9\u91cf\u4e3a 16\uff0c\u8d1f\u8f7d\u56e0\u5b50\u4e3a 0.75\nHashSet&lt;E&gt; set = new HashSet&lt;&gt;();\n\n\/\/ \u4f7f\u7528\u6307\u5b9a\u7684\u521d\u59cb\u5bb9\u91cf\u6784\u9020 HashSet\nHashSet&lt;E&gt; set = new HashSet&lt;&gt;(int initialCapacity);\n\n\/\/ \u4f7f\u7528\u6307\u5b9a\u7684\u521d\u59cb\u5bb9\u91cf\u548c\u8d1f\u8f7d\u56e0\u5b50\u6784\u9020 HashSet\nHashSet&lt;E&gt; set = new HashSet&lt;&gt;(int initialCapacity, float loadFactor);\n\n\/\/ \u57fa\u4e8e\u53e6\u4e00\u4e2a\u96c6\u5408\u521b\u5efa HashSet\nHashSet&lt;E&gt; set = new HashSet&lt;&gt;(Collection&lt;? extends E&gt; c);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>HashSet \u5e38\u7528\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 <strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>add(E e)<\/code>\uff1a\u5c06\u6307\u5b9a\u5143\u7d20\u6dfb\u52a0\u5230 <code>HashSet<\/code> \u4e2d\u3002\u5982\u679c\u96c6\u5408\u4e2d\u5df2\u7ecf\u5b58\u5728\u8be5\u5143\u7d20\uff0c\u5219\u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c\uff0c\u8fd4\u56de <code>false<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>true<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>HashSet&lt;String&gt; set = new HashSet&lt;&gt;();\nset.add(\"Apple\");  \/\/ \u8fd4\u56de true\nset.add(\"Banana\"); \/\/ \u8fd4\u56de true\nset.add(\"Apple\");  \/\/ \u8fd4\u56de false\uff0c\u56e0\u4e3a\u5143\u7d20\u91cd\u590d<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 <strong>\u5220\u9664\u5143\u7d20<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>remove(Object o)<\/code>\uff1a\u5220\u9664\u6307\u5b9a\u7684\u5143\u7d20\u3002\u5982\u679c\u5143\u7d20\u5b58\u5728\uff0c\u8fd4\u56de <code>true<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>false<\/code>\u3002<\/li>\n\n\n\n<li><code>clear()<\/code>\uff1a\u5220\u9664\u96c6\u5408\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/li>\n\n\n\n<li><code>contains(Object o)<\/code>\uff1a\u68c0\u67e5 <code>HashSet<\/code> \u4e2d\u662f\u5426\u5305\u542b\u6307\u5b9a\u7684\u5143\u7d20\uff0c\u8fd4\u56de <code>true<\/code> \u6216 <code>false<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>set.remove(\"Apple\"); \/\/ \u8fd4\u56de true\uff0c\u5220\u9664 \"Apple\"\nset.clear();          \/\/ \u6e05\u7a7a\u6240\u6709\u5143\u7d20\nboolean contains = set.contains(\"Banana\");  \/\/ \u68c0\u67e5\u662f\u5426\u5305\u542b \"Banana\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.3 <strong>\u67e5\u8be2\u5143\u7d20<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>size()<\/code>\uff1a\u8fd4\u56de <code>HashSet<\/code> \u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u3002<\/li>\n\n\n\n<li><code>isEmpty()<\/code>\uff1a\u68c0\u67e5 <code>HashSet<\/code> \u662f\u5426\u4e3a\u7a7a\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>int size = set.size();   \/\/ \u83b7\u53d6\u5143\u7d20\u4e2a\u6570\nboolean isEmpty = set.isEmpty();  \/\/ \u68c0\u67e5\u662f\u5426\u4e3a\u7a7a<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.4 <strong>\u904d\u5386\u5143\u7d20<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <strong>for-each<\/strong> \u5faa\u73af\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>for (String fruit : set) {\n    System.out.println(fruit);\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <strong>Iterator<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Iterator&lt;String&gt; iterator = set.iterator();\nwhile (iterator.hasNext()) {\n    System.out.println(iterator.next());\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.5 <strong>\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>toArray()<\/code>\uff1a\u5c06 <code>HashSet<\/code> \u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Object&#91;] array = set.toArray();<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>toArray(T[] a)<\/code>\uff1a\u5c06 <code>HashSet<\/code> \u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>String&#91;] array = set.toArray(new String&#91;0]);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.6 <strong>\u5176\u4ed6\u5e38\u7528\u65b9\u6cd5<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>removeAll(Collection&lt;?> c)<\/code>\uff1a\u5220\u9664\u96c6\u5408\u4e2d\u6240\u6709\u4e0e\u7ed9\u5b9a\u96c6\u5408 <code>c<\/code> \u76f8\u540c\u7684\u5143\u7d20\u3002<\/li>\n\n\n\n<li><code>retainAll(Collection&lt;?> c)<\/code>\uff1a\u4fdd\u7559\u96c6\u5408\u4e2d\u6240\u6709\u4e0e\u7ed9\u5b9a\u96c6\u5408 <code>c<\/code> \u76f8\u540c\u7684\u5143\u7d20\u3002<\/li>\n\n\n\n<li><code>containsAll(Collection&lt;?> c)<\/code>\uff1a\u68c0\u67e5\u96c6\u5408\u662f\u5426\u5305\u542b\u7ed9\u5b9a\u96c6\u5408\u7684\u6240\u6709\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>HashSet \u7684\u6027\u80fd<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u67e5\u627e\u3001\u6dfb\u52a0\u3001\u5220\u9664<\/strong>\uff1a<code>HashSet<\/code> \u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u67e5\u627e\u3001\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(1)\u3002<\/li>\n\n\n\n<li><strong>\u54c8\u5e0c\u51b2\u7a81<\/strong>\uff1a\u5728\u54c8\u5e0c\u51b2\u7a81\u53d1\u751f\u65f6\uff0c<code>HashSet<\/code> \u9700\u8981\u901a\u8fc7\u94fe\u8868\u6216\u5176\u4ed6\u673a\u5236\u6765\u89e3\u51b3\u51b2\u7a81\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u6700\u574f\u60c5\u51b5\u4e0b\uff0c\u64cd\u4f5c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u964d\u4e3a O(n)\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>HashSet \u4e0e\u5176\u4ed6 Set \u7684\u6bd4\u8f83<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th><code>HashSet<\/code><\/th><th><code>TreeSet<\/code><\/th><th><code>LinkedHashSet<\/code><\/th><\/tr><\/thead><tbody><tr><td><strong>\u5e95\u5c42\u5b9e\u73b0<\/strong><\/td><td>\u54c8\u5e0c\u8868<\/td><td>\u7ea2\u9ed1\u6811<\/td><td>\u53cc\u5411\u94fe\u8868 + \u54c8\u5e0c\u8868<\/td><\/tr><tr><td><strong>\u662f\u5426\u6709\u5e8f<\/strong><\/td><td>\u65e0\u5e8f<\/td><td>\u6709\u5e8f\uff08\u6309\u81ea\u7136\u6392\u5e8f\u6216\u81ea\u5b9a\u4e49\u6392\u5e8f\uff09<\/td><td>\u63d2\u5165\u987a\u5e8f\uff08\u63d2\u5165\u65f6\u7684\u987a\u5e8f\uff09<\/td><\/tr><tr><td><strong>\u67e5\u627e\/\u63d2\u5165\u6027\u80fd<\/strong><\/td><td>\u5feb O(1)\uff0c\u4f46\u662f\u53ef\u80fd\u6709\u54c8\u5e0c\u51b2\u7a81<\/td><td>\u8f83\u6162 O(log n)<\/td><td>\u4e2d\u7b49 O(1)\uff0c\u4f46\u8f83\u6162\u4e8e <code>HashSet<\/code><\/td><\/tr><tr><td><strong>\u5141\u8bb8\u91cd\u590d\u5143\u7d20<\/strong><\/td><td>\u4e0d\u5141\u8bb8<\/td><td>\u4e0d\u5141\u8bb8<\/td><td>\u4e0d\u5141\u8bb8<\/td><\/tr><tr><td><strong>\u5141\u8bb8 null \u5143\u7d20<\/strong><\/td><td>\u5141\u8bb8<\/td><td>\u4e0d\u5141\u8bb8<\/td><td>\u5141\u8bb8<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u7ebf\u7a0b\u5b89\u5168<\/strong><\/h3>\n\n\n\n<p><code>HashSet<\/code> \u672c\u8eab\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u548c\u4fee\u6539 <code>HashSet<\/code>\uff0c\u9700\u8981\u989d\u5916\u7684\u540c\u6b65\u5904\u7406\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Collections.synchronizedSet(new HashSet&lt;>())<\/code>\uff1a\u5c06 <code>HashSet<\/code> \u5305\u88c5\u4e3a\u7ebf\u7a0b\u5b89\u5168\u7684\u96c6\u5408\u3002<\/li>\n\n\n\n<li><code>CopyOnWriteArraySet<\/code>\uff1a\u5982\u679c\u662f\u53ea\u8bfb\u64cd\u4f5c\u8f83\u591a\uff0c\u4fee\u6539\u64cd\u4f5c\u8f83\u5c11\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>CopyOnWriteArraySet<\/code> \u6765\u66ff\u4ee3 <code>HashSet<\/code>\uff0c\u5b83\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Set&lt;String&gt; synchronizedSet = Collections.synchronizedSet(new HashSet&lt;&gt;());<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u4f7f\u7528 HashSet \u7684\u573a\u666f<\/strong><\/h3>\n\n\n\n<p><code>HashSet<\/code> \u9002\u5408\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53bb\u91cd\u64cd\u4f5c<\/strong>\uff1a\u5982\u679c\u9700\u8981\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff0c<code>HashSet<\/code> \u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/li>\n\n\n\n<li><strong>\u67e5\u627e\u64cd\u4f5c\u9891\u7e41\u7684\u573a\u666f<\/strong>\uff1a\u7531\u4e8e <code>HashSet<\/code> \u5bf9\u5143\u7d20\u7684\u67e5\u627e\u548c\u63d2\u5165\u64cd\u4f5c\u5177\u6709 O(1) \u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u56e0\u6b64\u5728\u9700\u8981\u9ad8\u6548\u67e5\u627e\u5143\u7d20\u7684\u573a\u666f\u4e2d\u975e\u5e38\u9002\u7528\u3002<\/li>\n\n\n\n<li><strong>\u65e0\u9700\u6392\u5e8f\u7684\u96c6\u5408<\/strong>\uff1a\u5982\u679c\u4e0d\u5173\u5fc3\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u800c\u53ea\u662f\u5e0c\u671b\u53bb\u91cd\u548c\u9ad8\u6548\u67e5\u627e\uff0c\u5219\u4f7f\u7528 <code>HashSet<\/code> \u6700\u4e3a\u5408\u9002\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c0f\u7ed3<\/h3>\n\n\n\n<p><code>HashSet<\/code> \u662f\u4e00\u4e2a\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\u7684\u96c6\u5408\u7c7b\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u8fdb\u884c\u5143\u7d20\u67e5\u627e\u3001\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u4e14\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3002\u5b83\u4e0d\u4fdd\u8bc1\u5143\u7d20\u7684\u987a\u5e8f\uff0c\u56e0\u6b64\u9002\u7528\u4e8e\u53bb\u91cd\u548c\u5feb\u901f\u67e5\u627e\u7b49\u573a\u666f\u3002\u5982\u679c\u9700\u8981\u6309\u6392\u5e8f\u987a\u5e8f\u5b58\u50a8\u5143\u7d20\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 <code>TreeSet<\/code>\uff1b\u5982\u679c\u9700\u8981\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\uff0c\u53ef\u4ee5\u8003\u8651 <code>LinkedHashSet<\/code>\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HashSet \u662f Java \u96c6\u5408\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7b\uff0c\u5c5e\u4e8e Set \u63a5\u53e3\u7684\u5b9e\u73b0\uff0c\u5b83\u57fa\u4e8e\u54c8\u5e0c\u8868\uff08HashMap\uff09 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-2256","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2256"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2256\/revisions"}],"predecessor-version":[{"id":2257,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2256\/revisions\/2257"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}