{"id":65761,"date":"2025-05-03T09:53:03","date_gmt":"2025-05-03T01:53:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65761\/"},"modified":"2025-05-03T09:53:03","modified_gmt":"2025-05-03T01:53:03","slug":"java%e6%95%b0%e7%bb%84%e4%b8%ad%e6%80%8e%e4%b9%88%e5%8e%bb%e9%87%8d-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65761\/","title":{"rendered":"java\u6570\u7ec4\u4e2d\u600e\u4e48\u53bb\u91cd"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u6570\u7ec4\u53bb\u91cd\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 hashset \u548c linkedhashset \u53bb\u9664\u91cd\u590d\u9879\uff1b\u4f7f\u7528 arrays.sort() \u548c\u53cc\u6307\u9488\u8df3\u8fc7\u91cd\u590d\u5143\u7d20\uff1b\u4f7f\u7528 java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u7684\u6d41 api \u5b9e\u73b0\u53bb\u91cd\u64cd\u4f5c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111309251221523.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u4e2d\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u4e2d\u600e\u4e48\u53bb\u91cd\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5<\/strong><\/p>\n<p>\u6570\u7ec4\u53bb\u91cd\u662f\u6307\u79fb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u8fc7\u7a0b\u3002Java \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6570\u7ec4\u53bb\u91cd\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 HashSet<\/strong><\/p>\n<p>HashSet \u662f Java \u4e2d\u4e00\u79cd\u57fa\u4e8e\u54c8\u5e0c\u8868\u7684\u96c6\u5408\u7c7b\uff0c\u5b83\u53ef\u4ee5\u81ea\u52a8\u53bb\u91cd\u590d\u9879\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 HashSet \u53bb\u91cd\u6570\u7ec4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\nSet&lt;Integer&gt; set = new HashSet&lt;&gt;();\nfor (int num : arr) {\n    set.add(num);\n}\nArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(set);\nint[] newArr = new int[list.size()];\nfor (int i = 0; i &lt; list.size(); i++) {\n    newArr[i] = list.get(i);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 LinkedHashSet<\/strong><\/p>\n<p>LinkedHashSet \u4e0e HashSet \u7c7b\u4f3c\uff0c\u4f46\u5b83\u4fdd\u7559\u4e86\u5143\u7d20\u7684\u63d2\u5165\u987a\u5e8f\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528 LinkedHashSet \u53bb\u91cd\u5e76\u4fdd\u7559\u5143\u7d20\u7684\u987a\u5e8f\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\nSet&lt;Integer&gt; set = new LinkedHashSet&lt;&gt;();\nfor (int num : arr) {\n    set.add(num);\n}\nArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(set);\nint[] newArr = new int[list.size()];\nfor (int i = 0; i &lt; list.size(); i++) {\n    newArr[i] = list.get(i);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.sort() \u548c\u53cc\u6307\u9488<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 Arrays.sort() \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528\u53cc\u6307\u9488\u904d\u5386\u6570\u7ec4\uff0c\u8df3\u8fc7\u91cd\u590d\u7684\u5143\u7d20\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\nArrays.sort(arr);\nint[] newArr = new int[arr.length];\nint j = 0;\nfor (int i = 0; i &lt; arr.length; i++) {\n    if (i == 0 || arr[i] != arr[i - 1]) {\n        newArr[j++] = arr[i];\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u6d41 API\uff08Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\uff09<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u7684\u6d41 API \u5b9e\u73b0\u6570\u7ec4\u53bb\u91cd\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\nint[] newArr = Arrays.stream(arr)\n        .distinct()\n        .toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u4e2d\u600e\u4e48\u53bb\u91cd\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u4e2d\u6570\u7ec4\u53bb\u91cd\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 hashset \u548c linkedhashset \u53bb\u9664\u91cd\u590d\u9879\uff1b\u4f7f\u7528 arrays.sort() \u548c\u53cc\u6307\u9488\u8df3\u8fc7\u91cd\u590d\u5143\u7d20\uff1b\u4f7f\u7528 java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u7684\u6d41 api \u5b9e\u73b0\u53bb\u91cd\u64cd\u4f5c\u3002 Java \u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5 \u6570\u7ec4\u53bb\u91cd\u662f\u6307\u79fb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u8fc7\u7a0b\u3002Java \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6570\u7ec4\u53bb\u91cd\uff1a 1. \u4f7f\u7528 HashSet HashSet \u662f Java \u4e2d\u4e00\u79cd\u57fa\u4e8e\u54c8\u5e0c\u8868\u7684\u96c6\u5408\u7c7b\uff0c\u5b83\u53ef\u4ee5\u81ea\u52a8\u53bb\u91cd\u590d\u9879\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 HashSet \u53bb\u91cd\u6570\u7ec4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = {1, 2, 3, 4, 5, 1, 2, 3}; Set&lt;Integer&gt; set = new HashSet&lt;&gt;(); for (int num : arr) { set.add(num); } ArrayList&lt;Integer&gt; list = new [&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-65761","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65761","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=65761"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65761\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}