{"id":65501,"date":"2025-05-03T08:40:38","date_gmt":"2025-05-03T00:40:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65501\/"},"modified":"2025-05-03T08:40:38","modified_gmt":"2025-05-03T00:40:38","slug":"java%e6%95%b0%e7%bb%84%e4%b8%ad%e6%80%8e%e4%b9%88%e6%9f%a5%e9%87%8d-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65501\/","title":{"rendered":"java\u6570\u7ec4\u4e2d\u600e\u4e48\u67e5\u91cd"},"content":{"rendered":"<blockquote><p>\n  java\u6570\u7ec4\u4e2d\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528set\uff1a\u5b83\u4e0d\u5b58\u50a8\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u5c06\u6570\u7ec4\u5143\u7d20\u6dfb\u52a0\u5230set\u4e2d\uff0c\u91cd\u590d\u5143\u7d20\u4e0d\u6dfb\u52a0\u3002\u4f7f\u7528hashmap\uff1a\u8bb0\u5f55\u5143\u7d20\u51fa\u73b0\u6b21\u6570\uff0c\u51fa\u73b0\u6b21\u6570\u8d85\u8fc71\u7684\u5373\u4e3a\u91cd\u590d\u5143\u7d20\u3002\u4f7f\u7528\u6392\u5e8f\uff1a\u5c06\u6570\u7ec4\u6392\u5e8f\uff0c\u91cd\u590d\u5143\u7d20\u5c06\u6210\u4e3a\u76f8\u90bb\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111302280064209.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u4e2d\u600e\u4e48\u67e5\u91cd\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u4e2d\u600e\u4e48\u67e5\u91cd\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u67e5\u627e Java \u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20<\/strong><\/p>\n<p>Java \u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u6309\u987a\u5e8f\u5b58\u50a8\u540c\u4e00\u7c7b\u578b\u7684\u5143\u7d20\u3002\u6709\u65f6\u5019\uff0c\u53ef\u80fd\u9700\u8981\u67e5\u627e\u6570\u7ec4\u4e2d\u91cd\u590d\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 Java \u4e2d\u67e5\u627e\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Set<\/strong><\/p>\n<p>Set \u63a5\u53e3\u4e0d\u5b58\u50a8\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u53ef\u4ee5\u5c06\u5176\u7528\u4e8e\u67e5\u627e\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.util.Arrays;\nimport java.util.Set;\nimport java.util.HashSet;\n\nclass FindDuplicates {\n    public static void main(String[] args) {\n        int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\n\n        Set&lt;Integer&gt; uniqueElements = new HashSet&lt;&gt;();\n        Set&lt;Integer&gt; duplicates = new HashSet&lt;&gt;();\n\n        for (int element : arr) {\n            if (!uniqueElements.add(element)) {\n                duplicates.add(element);\n            }\n        }\n\n        System.out.println(\"\u91cd\u590d\u5143\u7d20\uff1a\" + duplicates);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 HashMap<\/strong><\/p>\n<p>HashMap \u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u952e\u662f\u552f\u4e00\u7684\u3002\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8bb0\u5f55\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<pre>import java.util.Arrays;\nimport java.util.HashMap;\n\nclass FindDuplicates {\n    public static void main(String[] args) {\n        int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\n\n        HashMap&lt;Integer, Integer&gt; elementCounts = new HashMap&lt;&gt;();\n\n        for (int element : arr) {\n            elementCounts.put(element, elementCounts.getOrDefault(element, 0) + 1);\n        }\n\n        for (int element : elementCounts.keySet()) {\n            if (elementCounts.get(element) &gt; 1) {\n                System.out.println(\"\u91cd\u590d\u5143\u7d20\uff1a\" + element);\n            }\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528\u6392\u5e8f<\/strong><\/p>\n<p>\u5982\u679c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u662f\u53ef\u6392\u5e8f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u6392\u5e8f\u7b97\u6cd5\u3002\u6392\u5e8f\u540e\uff0c\u91cd\u590d\u5143\u7d20\u5c06\u6210\u4e3a\u76f8\u90bb\u7684\u5143\u7d20\u3002<\/p>\n<pre>import java.util.Arrays;\n\nclass FindDuplicates {\n    public static void main(String[] args) {\n        int[] arr = {1, 2, 3, 4, 5, 1, 2, 3};\n\n        Arrays.sort(arr);\n\n        for (int i = 0; i &lt; arr.length - 1; i++) {\n            if (arr[i] == arr[i + 1]) {\n                System.out.println(\"\u91cd\u590d\u5143\u7d20\uff1a\" + arr[i]);\n            }\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u4e2d\u600e\u4e48\u67e5\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\u6570\u7ec4\u4e2d\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528set\uff1a\u5b83\u4e0d\u5b58\u50a8\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u5c06\u6570\u7ec4\u5143\u7d20\u6dfb\u52a0\u5230set\u4e2d\uff0c\u91cd\u590d\u5143\u7d20\u4e0d\u6dfb\u52a0\u3002\u4f7f\u7528hashmap\uff1a\u8bb0\u5f55\u5143\u7d20\u51fa\u73b0\u6b21\u6570\uff0c\u51fa\u73b0\u6b21\u6570\u8d85\u8fc71\u7684\u5373\u4e3a\u91cd\u590d\u5143\u7d20\u3002\u4f7f\u7528\u6392\u5e8f\uff1a\u5c06\u6570\u7ec4\u6392\u5e8f\uff0c\u91cd\u590d\u5143\u7d20\u5c06\u6210\u4e3a\u76f8\u90bb\u5143\u7d20\u3002 \u5982\u4f55\u67e5\u627e Java \u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u5143\u7d20 Java \u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u6309\u987a\u5e8f\u5b58\u50a8\u540c\u4e00\u7c7b\u578b\u7684\u5143\u7d20\u3002\u6709\u65f6\u5019\uff0c\u53ef\u80fd\u9700\u8981\u67e5\u627e\u6570\u7ec4\u4e2d\u91cd\u590d\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 Java \u4e2d\u67e5\u627e\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 Set Set \u63a5\u53e3\u4e0d\u5b58\u50a8\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u53ef\u4ee5\u5c06\u5176\u7528\u4e8e\u67e5\u627e\u91cd\u590d\u5143\u7d20\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import java.util.Arrays; import java.util.Set; import java.util.HashSet; class FindDuplicates { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 1, 2, 3}; Set&lt;Integer&gt; uniqueElements = new HashSet&lt;&gt;(); Set&lt;Integer&gt; duplicates = new HashSet&lt;&gt;(); for (int element : arr) [&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-65501","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65501","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=65501"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65501\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}