{"id":34601,"date":"2024-11-26T09:33:39","date_gmt":"2024-11-26T01:33:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34601\/"},"modified":"2024-11-26T09:33:39","modified_gmt":"2024-11-26T01:33:39","slug":"java%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8e%bb%e9%87%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34601\/","title":{"rendered":"java\u5b57\u7b26\u4e32\u6570\u7ec4\u600e\u4e48\u53bb\u91cd"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u53bb\u9664\u5b57\u7b26\u4e32\u6570\u7ec4\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u6709\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a hashset\uff0c\u518d\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528 set.of() \u521b\u5efa\u4e0d\u53ef\u53d8\u96c6\u5408\u3002\u4f7f\u7528 stream.distinct() \u521b\u5efa\u4e0d\u91cd\u590d\u5143\u7d20\u7684\u65b0\u6d41\u3002\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\u518d\u4f7f\u7528 stream.distinct()\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/02\/2024110217395288279.jpg\" class=\"aligncenter\" title=\"java\u5b57\u7b26\u4e32\u6570\u7ec4\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" alt=\"java\u5b57\u7b26\u4e32\u6570\u7ec4\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u53bb\u9664 Java \u4e2d\u7684\u91cd\u590d\u5143\u7d20<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53bb\u9664\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. HashSet<\/strong><\/p>\n<p>HashSet \u662f\u4e00\u4e2a\u96c6\u5408\u7c7b\uff0c\u5b83\u4e0d\u80fd\u5305\u542b\u91cd\u590d\u7684\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a HashSet\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] arr = {\"a\", \"b\", \"c\", \"a\", \"b\"};\n\nHashSet&lt;String&gt; set = new HashSet&lt;&gt;(Arrays.asList(arr));\nString[] newArr = set.toArray(new String[0]);\n\nSystem.out.println(Arrays.toString(newArr)); \/\/ \u8f93\u51fa\uff1a[a, b, c]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. Set.of()<\/strong><\/p>\n<p>Java 9 \u5f15\u5165\u4e86 Set.of() \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u521b\u5efa\u4e0d\u53ef\u53d8\u7684\u96c6\u5408\uff0c\u5e76\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<pre>String[] arr = {\"a\", \"b\", \"c\", \"a\", \"b\"};\n\nSet&lt;String&gt; set = Set.of(arr);\nString[] newArr = set.toArray(new String[0]);\n\nSystem.out.println(Arrays.toString(newArr)); \/\/ \u8f93\u51fa\uff1a[a, b, c]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. Stream.distinct()<\/strong><\/p>\n<p>Java 8 \u5f15\u5165\u4e86 Stream.distinct() \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6d41\uff0c\u5176\u4e2d\u5305\u542b\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u3002<\/p>\n<pre>String[] arr = {\"a\", \"b\", \"c\", \"a\", \"b\"};\n\nString[] newArr = Arrays.stream(arr).distinct().toArray(String[]::new);\n\nSystem.out.println(Arrays.toString(newArr)); \/\/ \u8f93\u51fa\uff1a[a, b, c]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. Arrays.asList() + Stream.distinct()<\/strong><\/p>\n<p>\u5bf9\u4e8e\u8f83\u5c0f\u7684\u9ad8\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u518d\u4f7f\u7528 Stream.distinct() \u65b9\u6cd5\u3002<\/p>\n<pre>String[][] arr = {{\"a\", \"b\"}, {\"c\", \"d\"}, {\"a\", \"b\"}};\n\nString[] newArr = Arrays.asList(arr)\n    .stream()\n    .flatMap(Arrays::stream)\n    .distinct()\n    .toArray(String[]::new);\n\nSystem.out.println(Arrays.toString(newArr)); \/\/ \u8f93\u51fa\uff1a[a, b, c, d]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u53bb\u91cd\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u4e2d\u53bb\u9664\u5b57\u7b26\u4e32\u6570\u7ec4\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u6709\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a hashset\uff0c\u518d\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528 set.of() \u521b\u5efa\u4e0d\u53ef\u53d8\u96c6\u5408\u3002\u4f7f\u7528 stream.distinct() \u521b\u5efa\u4e0d\u91cd\u590d\u5143\u7d20\u7684\u65b0\u6d41\u3002\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\u518d\u4f7f\u7528 stream.distinct()\u3002 \u5982\u4f55\u53bb\u9664 Java \u4e2d\u7684\u91cd\u590d\u5143\u7d20 \u5728 Java \u4e2d\uff0c\u53bb\u9664\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. HashSet HashSet \u662f\u4e00\u4e2a\u96c6\u5408\u7c7b\uff0c\u5b83\u4e0d\u80fd\u5305\u542b\u91cd\u590d\u7684\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a HashSet\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] arr = {&#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;, &#8220;a&#8221;, &#8220;b&#8221;}; HashSet&lt;String&gt; set = new HashSet&lt;&gt;(Arrays.asList(arr)); String[] newArr = set.toArray(new String[0]); System.out.println(Arrays.toString(newArr)); \/\/ \u8f93\u51fa\uff1a[a, b, c] \u767b\u5f55\u540e\u590d\u5236 2. Set.of() Java 9 \u5f15\u5165\u4e86 Set.of() \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u521b\u5efa\u4e0d\u53ef\u53d8\u7684\u96c6\u5408\uff0c\u5e76\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002 String[] arr = {&#8220;a&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-34601","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=34601"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34601\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}