{"id":66515,"date":"2025-05-03T13:54:44","date_gmt":"2025-05-03T05:54:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66515\/"},"modified":"2025-05-03T13:54:44","modified_gmt":"2025-05-03T05:54:44","slug":"java%e6%80%8e%e4%b9%88%e5%88%a0%e9%99%a4%e6%95%b0%e7%bb%84%e9%87%8d%e5%a4%8d%e7%9a%84%e6%95%b0%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66515\/","title":{"rendered":"java\u600e\u4e48\u5220\u9664\u6570\u7ec4\u91cd\u590d\u7684\u6570\u5b57"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 java \u5220\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u6570\u5b57\uff1a\u4f7f\u7528 set\uff1a\u901a\u8fc7\u521b\u5efa\u96c6\u5408\u5e76\u6dfb\u52a0\u6570\u7ec4\u5143\u7d20\uff0c\u83b7\u53d6\u552f\u4e00\u5143\u7d20\u3002\u4f7f\u7528 arrays.sort()\uff1a\u6392\u5e8f\u6570\u7ec4\u5e76\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\uff0c\u5220\u9664\u91cd\u590d\u5143\u7d20\u3002\u4f7f\u7528 stream api\uff1a\u901a\u8fc7\u8fc7\u6ee4\u91cd\u590d\u5143\u7d20\uff0c\u83b7\u53d6\u552f\u4e00\u5143\u7d20\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111605305478103.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5220\u9664\u6570\u7ec4\u91cd\u590d\u7684\u6570\u5b57\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5220\u9664\u6570\u7ec4\u91cd\u590d\u7684\u6570\u5b57\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u5220\u9664\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u6570\u5b57<\/strong><\/p>\n<p><strong>\u5f00\u7bc7\u76f4\u8ff0\uff1a<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u91cd\u590d\u6570\u5b57\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Set<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u4f7f\u7528 HashSet \u6216 TreeSet \u521b\u5efa\u4e00\u4e2a\u65b0\u96c6\u5408\u3002<\/li>\n<li>\u904d\u5386\u6570\u7ec4\u5e76\u9010\u4e2a\u6dfb\u52a0\u5143\u7d20\u5230\u96c6\u5408\u4e2d\u3002<\/li>\n<li>\u56e0\u4e3a\u96c6\u5408\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u53ea\u4fdd\u7559\u4e86\u552f\u4e00\u5143\u7d20\u3002<\/li>\n<li>\u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u4ee5\u83b7\u53d6\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528 Arrays.sort()<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 Arrays.sort() \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li>\u904d\u5386\u5df2\u6392\u5e8f\u7684\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528 i \u548c i+1 \u7d22\u5f15\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u3002<\/li>\n<li>\u5982\u679c array[i] \u7b49\u4e8e array[i+1], \u5219\u5220\u9664 array[i+1]\u3002<\/li>\n<\/ul>\n<p><strong>3. \u4f7f\u7528 Stream API<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 Arrays.stream() \u521b\u5efa\u6570\u7ec4\u6d41\u3002<\/li>\n<li>\u4f7f\u7528 distinct() \u65b9\u6cd5\u8fc7\u6ee4\u91cd\u590d\u5143\u7d20\u3002<\/li>\n<li>\u5c06\u8fc7\u6ee4\u540e\u7684\u6d41\u8f6c\u6362\u4e3a\u6570\u7ec4\u4ee5\u83b7\u53d6\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f7f\u7528 Set\nSet&lt;Integer&gt; uniqueNumbers = new HashSet&lt;&gt;();\nfor (int num : array) {\n    uniqueNumbers.add(num);\n}\nint[] result = new int[uniqueNumbers.size()];\nint i = 0;\nfor (int num : uniqueNumbers) {\n    result[i++] = num;\n}\n\n\/\/ \u4f7f\u7528 Arrays.sort()\nArrays.sort(array);\nint[] result = new int[array.length];\nint j = 0;\nfor (int i = 0; i &lt; array.length - 1; i++) {\n    if (array[i] != array[i+1]) {\n        result[j++] = array[i];\n    }\n}\nresult[j++] = array[array.length - 1];\n\n\/\/ \u4f7f\u7528 Stream API\nint[] result = Arrays.stream(array).distinct().toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5220\u9664\u6570\u7ec4\u91cd\u590d\u7684\u6570\u5b57\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>\u4f7f\u7528 java \u5220\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u6570\u5b57\uff1a\u4f7f\u7528 set\uff1a\u901a\u8fc7\u521b\u5efa\u96c6\u5408\u5e76\u6dfb\u52a0\u6570\u7ec4\u5143\u7d20\uff0c\u83b7\u53d6\u552f\u4e00\u5143\u7d20\u3002\u4f7f\u7528 arrays.sort()\uff1a\u6392\u5e8f\u6570\u7ec4\u5e76\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\uff0c\u5220\u9664\u91cd\u590d\u5143\u7d20\u3002\u4f7f\u7528 stream api\uff1a\u901a\u8fc7\u8fc7\u6ee4\u91cd\u590d\u5143\u7d20\uff0c\u83b7\u53d6\u552f\u4e00\u5143\u7d20\u6570\u7ec4\u3002 \u5982\u4f55\u4f7f\u7528 Java \u5220\u9664\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u6570\u5b57 \u5f00\u7bc7\u76f4\u8ff0\uff1a \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u91cd\u590d\u6570\u5b57\uff1a 1. \u4f7f\u7528 Set \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f7f\u7528 HashSet \u6216 TreeSet \u521b\u5efa\u4e00\u4e2a\u65b0\u96c6\u5408\u3002 \u904d\u5386\u6570\u7ec4\u5e76\u9010\u4e2a\u6dfb\u52a0\u5143\u7d20\u5230\u96c6\u5408\u4e2d\u3002 \u56e0\u4e3a\u96c6\u5408\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u53ea\u4fdd\u7559\u4e86\u552f\u4e00\u5143\u7d20\u3002 \u5c06\u96c6\u5408\u8f6c\u6362\u4e3a\u6570\u7ec4\u4ee5\u83b7\u53d6\u7ed3\u679c\u3002 2. \u4f7f\u7528 Arrays.sort() \u4f7f\u7528 Arrays.sort() \u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002 \u904d\u5386\u5df2\u6392\u5e8f\u7684\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528 i \u548c i+1 \u7d22\u5f15\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u3002 \u5982\u679c array[i] \u7b49\u4e8e array[i+1], \u5219\u5220\u9664 array[i+1]\u3002 3. \u4f7f\u7528 Stream API \u4f7f\u7528 Arrays.stream() \u521b\u5efa\u6570\u7ec4\u6d41\u3002 \u4f7f\u7528 distinct() \u65b9\u6cd5\u8fc7\u6ee4\u91cd\u590d\u5143\u7d20\u3002 \u5c06\u8fc7\u6ee4\u540e\u7684\u6d41\u8f6c\u6362\u4e3a\u6570\u7ec4\u4ee5\u83b7\u53d6\u7ed3\u679c\u3002 \u793a\u4f8b\uff1a [&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-66515","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66515","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=66515"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66515\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}