{"id":64324,"date":"2025-05-03T14:38:11","date_gmt":"2025-05-03T06:38:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64324\/"},"modified":"2025-05-03T14:38:11","modified_gmt":"2025-05-03T06:38:11","slug":"java%e6%80%8e%e4%b9%88%e5%8e%bb%e9%99%a4%e6%95%b0%e7%bb%84%e4%b8%ad%e9%87%8d%e5%a4%8d%e7%9a%84%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64324\/","title":{"rendered":"JAVA\u600e\u4e48\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u7684\u503c"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u4ece\u6570\u7ec4\u4e2d\u53bb\u9664\u91cd\u590d\u503c\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u4f7f\u7528 hashset\u3001\u4f7f\u7528 set api\u3001\u4f7f\u7528 stream api\u3002hashset \u662f\u6700\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528 set api \u548c stream api \u4e5f\u80fd\u8f7b\u677e\u5b9e\u73b0\u53bb\u91cd\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110305221991127.jpg\" class=\"aligncenter\" title=\"JAVA\u600e\u4e48\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u7684\u503c\u63d2\u56fe\" alt=\"JAVA\u600e\u4e48\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u7684\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4ece Java \u6570\u7ec4\u4e2d\u53bb\u9664\u91cd\u590d\u503c<\/strong><\/p>\n<p>\u5728 Java \u4e2d\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 HashSet<\/strong><\/p>\n<p>\u4f7f\u7528 HashSet \u662f\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u503c\u6700\u6709\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002HashSet \u662f Java \u4e2d\u7684\u4e00\u4e2a\u96c6\u5408\u7c7b\uff0c\u5b83\u81ea\u52a8\u8fc7\u6ee4\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.util.HashSet;\nimport java.util.Arrays;\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        \/\/ \u521b\u5efa\u4e00\u4e2a HashSet\n        HashSet&lt;Integer&gt; set = new HashSet&lt;&gt;();\n\n        \/\/ \u5c06\u6570\u7ec4\u5143\u7d20\u6dfb\u52a0\u5230 HashSet \u4e2d\n        for (int num : arr) {\n            set.add(num);\n        }\n\n        \/\/ \u5c06 HashSet \u8f6c\u6362\u4e3a\u6570\u7ec4\n        int[] newArr = new int[set.size()];\n        int index = 0;\n        for (int num : set) {\n            newArr[index++] = num;\n        }\n\n        \/\/ \u6253\u5370\u53bb\u91cd\u540e\u7684\u6570\u7ec4\n        System.out.println(Arrays.toString(newArr));\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Set API<\/strong><\/p>\n<p>Java \u4e2d\u63d0\u4f9b\u4e86 Collections \u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u540d\u4e3a Set \u7684\u65b9\u6cd5\uff0c\u53ef\u7528\u4e8e\u8f7b\u677e\u5730\u4ece\u6570\u7ec4\u4e2d\u53bb\u9664\u91cd\u590d\u503c\u3002Set \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u4e0d\u53ef\u53d8\u7684 Set\uff0c\u5176\u4e2d\u5305\u542b\u8be5\u6570\u7ec4\u4e2d\u7684\u552f\u4e00\u5143\u7d20\u3002<\/p>\n<pre>import java.util.Arrays;\nimport java.util.Collections;\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 Collections.Set \u65b9\u6cd5\n        int[] newArr = Arrays.stream(arr)\n                .boxed()\n                .distinct()\n                .mapToInt(Integer::intValue)\n                .toArray();\n\n        \/\/ \u6253\u5370\u53bb\u91cd\u540e\u7684\u6570\u7ec4\n        System.out.println(Arrays.toString(newArr));\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Stream API<\/strong><\/p>\n<p>Java 8 \u4e2d\u5f15\u5165\u7684 Stream API \u4e5f\u53ef\u7528\u4e8e\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u503c\u3002distinct() \u65b9\u6cd5\u53ef\u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6570\u7ec4\u4e2d\u552f\u4e00\u5143\u7d20\u7684 Stream\u3002<\/p>\n<pre>import java.util.Arrays;\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 API\n        int[] newArr = Arrays.stream(arr)\n                .boxed()\n                .distinct()\n                .collect(Collectors.toList())\n                .stream()\n                .mapToInt(Integer::intValue)\n                .toArray();\n\n        \/\/ \u6253\u5370\u53bb\u91cd\u540e\u7684\u6570\u7ec4\n        System.out.println(Arrays.toString(newArr));\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJAVA\u600e\u4e48\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u7684\u503c\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\u4ece\u6570\u7ec4\u4e2d\u53bb\u9664\u91cd\u590d\u503c\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a\u4f7f\u7528 hashset\u3001\u4f7f\u7528 set api\u3001\u4f7f\u7528 stream api\u3002hashset \u662f\u6700\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528 set api \u548c stream api \u4e5f\u80fd\u8f7b\u677e\u5b9e\u73b0\u53bb\u91cd\u3002 \u5982\u4f55\u4ece Java \u6570\u7ec4\u4e2d\u53bb\u9664\u91cd\u590d\u503c \u5728 Java \u4e2d\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a 1. \u4f7f\u7528 HashSet \u4f7f\u7528 HashSet \u662f\u53bb\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u503c\u6700\u6709\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002HashSet \u662f Java \u4e2d\u7684\u4e00\u4e2a\u96c6\u5408\u7c7b\uff0c\u5b83\u81ea\u52a8\u8fc7\u6ee4\u91cd\u590d\u5143\u7d20\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import java.util.HashSet; import java.util.Arrays; public class RemoveDuplicates { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 1, 2, [&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-64324","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64324","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=64324"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64324\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}