{"id":65310,"date":"2025-05-03T10:16:51","date_gmt":"2025-05-03T02:16:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65310\/"},"modified":"2025-05-03T10:16:51","modified_gmt":"2025-05-03T02:16:51","slug":"java%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8e%bb%e9%87%8d%e5%a4%8d-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65310\/","title":{"rendered":"java\u6570\u7ec4\u600e\u4e48\u53bb\u91cd\u590d"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u53bb\u9664\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u5e38\u7528\u65b9\u6cd5\uff1a\u4f7f\u7528 stream \u548c set\uff1a\u5229\u7528 stream \u8f6c\u6362\u6570\u7ec4\u4e3a set \u53bb\u91cd\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u56de\u6570\u7ec4\u3002\u4f7f\u7528 hashset\uff1a\u904d\u5386\u6570\u7ec4\uff0c\u5c06\u5143\u7d20\u6dfb\u52a0\u5230 hashset \u4e2d\u4ee5\u5b9e\u73b0\u53bb\u91cd\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u56de\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111219512253530.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u600e\u4e48\u53bb\u91cd\u590d\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u600e\u4e48\u53bb\u91cd\u590d\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u53bb\u9664 Java \u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u6700\u5e38\u7528\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Stream \u548c Set<\/strong><\/p>\n<pre>import java.util.Arrays;\nimport java.util.Set;\nimport java.util.stream.Collectors;\n\npublic class RemoveDuplicates {\n\n    public static void main(String[] args) {\n        int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\n\n        \/\/ \u4f7f\u7528 Stream \u548c Set\n        Set&lt;Integer&gt; uniqueElements = Arrays.stream(arr)\n                .boxed()\n                .collect(Collectors.toSet());\n\n        \/\/ \u5c06 Set \u8f6c\u6362\u56de\u6570\u7ec4\n        int[] uniqueArray = uniqueElements.stream()\n                .mapToInt(Integer::intValue)\n                .toArray();\n\n        System.out.println(Arrays.toString(uniqueArray)); \/\/ \u8f93\u51fa\uff1a\"[1, 2, 3, 4, 5]\"\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 HashSet<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.util.Arrays;\nimport java.util.HashSet;\n\npublic class RemoveDuplicates {\n\n    public static void main(String[] args) {\n        int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\n\n        \/\/ \u4f7f\u7528 HashSet\n        HashSet&lt;Integer&gt; uniqueElements = new HashSet&lt;&gt;();\n        for (int element : arr) {\n            uniqueElements.add(element);\n        }\n\n        \/\/ \u5c06 HashSet \u8f6c\u6362\u56de\u6570\u7ec4\n        int[] uniqueArray = new int[uniqueElements.size()];\n        int index = 0;\n        for (int element : uniqueElements) {\n            uniqueArray[index++] = element;\n        }\n\n        System.out.println(Arrays.toString(uniqueArray)); \/\/ \u8f93\u51fa\uff1a\"[1, 2, 3, 4, 5]\"\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u600e\u4e48\u53bb\u91cd\u590d\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>\u5728 java \u4e2d\u53bb\u9664\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u5e38\u7528\u65b9\u6cd5\uff1a\u4f7f\u7528 stream \u548c set\uff1a\u5229\u7528 stream \u8f6c\u6362\u6570\u7ec4\u4e3a set \u53bb\u91cd\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u56de\u6570\u7ec4\u3002\u4f7f\u7528 hashset\uff1a\u904d\u5386\u6570\u7ec4\uff0c\u5c06\u5143\u7d20\u6dfb\u52a0\u5230 hashset \u4e2d\u4ee5\u5b9e\u73b0\u53bb\u91cd\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u56de\u6570\u7ec4\u3002 \u5982\u4f55\u53bb\u9664 Java \u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20 \u5728 Java \u4e2d\uff0c\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u6700\u5e38\u7528\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 Stream \u548c Set import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; public class RemoveDuplicates { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 1, 2, 3}; \/\/ \u4f7f\u7528 Stream \u548c [&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-65310","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65310","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=65310"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65310\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}