{"id":34998,"date":"2024-11-26T11:10:00","date_gmt":"2024-11-26T03:10:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34998\/"},"modified":"2024-11-26T11:10:00","modified_gmt":"2024-11-26T03:10:00","slug":"java%e6%80%8e%e4%b9%88%e6%b1%82%e6%95%b0%e7%bb%84%e4%b8%ad%e4%b8%8d%e9%87%8d%e5%a4%8d%e6%95%b0%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34998\/","title":{"rendered":"java\u600e\u4e48\u6c42\u6570\u7ec4\u4e2d\u4e0d\u91cd\u590d\u6570\u503c"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u6570\u7ec4\u4e2d\u627e\u51fa\u4e0d\u91cd\u590d\u7684\u503c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u521b\u5efa\u4e00\u4e2a hashset \u96c6\u5408\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8\u4e0d\u91cd\u590d\u7684\u503c\u3002\u904d\u5386\u6570\u7ec4\uff0c\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5df2\u5b58\u5728\u4e8e hashset \u4e2d\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230 hashset \u4e2d\u3002\u904d\u5386\u7ed3\u675f\u540e\uff0chashset \u5c06\u5305\u542b\u6570\u7ec4\u4e2d\u6240\u6709\u4e0d\u91cd\u590d\u7684\u503c\uff0c\u5c06\u5176\u8fd4\u56de\u4f5c\u4e3a\u7ed3\u679c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110308062558652.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u6c42\u6570\u7ec4\u4e2d\u4e0d\u91cd\u590d\u6570\u503c\u63d2\u56fe\" alt=\"java\u600e\u4e48\u6c42\u6570\u7ec4\u4e2d\u4e0d\u91cd\u590d\u6570\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u6570\u7ec4\u4e2d\u627e\u51fa\u4e0d\u91cd\u590d\u7684\u503c<\/strong><\/p>\n<p>\u8981\u627e\u51fa Java \u6570\u7ec4\u4e2d\u4e0d\u91cd\u590d\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u4e00\u4e2a HashSet<\/strong><\/p>\n<p>HashSet \u662f\u4e00\u79cd\u96c6\u5408\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8\u4e0d\u91cd\u590d\u7684\u503c\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a uniqueValues \u7684 HashSet\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>Set&lt;Integer&gt; uniqueValues = new HashSet&lt;&gt;();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u904d\u5386\u6570\u7ec4<\/strong><\/p>\n<p>\u4f7f\u7528\u4e00\u4e2a for \u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e8e HashSet \u4e2d\u3002<\/p>\n<pre>for (int num : array) {\n    if (!uniqueValues.contains(num)) {\n        uniqueValues.add(num);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u8fd4\u56de HashSet<\/strong><\/p>\n<p>\u904d\u5386\u7ed3\u675f\u540e\uff0cuniqueValues \u5c06\u5305\u542b\u6570\u7ec4\u4e2d\u6240\u6709\u4e0d\u91cd\u590d\u7684\u503c\u3002\u5c06\u5176\u8fd4\u56de\u4f5c\u4e3a\u7ed3\u679c\uff1a<\/p>\n<pre>return uniqueValues;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>int[] array = {1, 2, 3, 4, 5, 1, 2, 3};\nSet&lt;Integer&gt; uniqueValues = findUniqueValues(array);\nSystem.out.println(uniqueValues); \/\/ \u8f93\u51fa\uff1a{4, 5}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u6c42\u6570\u7ec4\u4e2d\u4e0d\u91cd\u590d\u6570\u503c\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u6570\u7ec4\u4e2d\u627e\u51fa\u4e0d\u91cd\u590d\u7684\u503c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u521b\u5efa\u4e00\u4e2a hashset \u96c6\u5408\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8\u4e0d\u91cd\u590d\u7684\u503c\u3002\u904d\u5386\u6570\u7ec4\uff0c\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5df2\u5b58\u5728\u4e8e hashset \u4e2d\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230 hashset \u4e2d\u3002\u904d\u5386\u7ed3\u675f\u540e\uff0chashset \u5c06\u5305\u542b\u6570\u7ec4\u4e2d\u6240\u6709\u4e0d\u91cd\u590d\u7684\u503c\uff0c\u5c06\u5176\u8fd4\u56de\u4f5c\u4e3a\u7ed3\u679c\u3002 \u5982\u4f55\u5728 Java \u6570\u7ec4\u4e2d\u627e\u51fa\u4e0d\u91cd\u590d\u7684\u503c \u8981\u627e\u51fa Java \u6570\u7ec4\u4e2d\u4e0d\u91cd\u590d\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u521b\u5efa\u4e00\u4e2a HashSet HashSet \u662f\u4e00\u79cd\u96c6\u5408\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8\u4e0d\u91cd\u590d\u7684\u503c\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a uniqueValues \u7684 HashSet\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b Set&lt;Integer&gt; uniqueValues = new HashSet&lt;&gt;(); \u767b\u5f55\u540e\u590d\u5236 2. \u904d\u5386\u6570\u7ec4 \u4f7f\u7528\u4e00\u4e2a for \u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e8e HashSet \u4e2d\u3002 for (int num : array) { if (!uniqueValues.contains(num)) { uniqueValues.add(num); } } \u767b\u5f55\u540e\u590d\u5236 3. \u8fd4\u56de HashSet \u904d\u5386\u7ed3\u675f\u540e\uff0cuniqueValues [&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-34998","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34998","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=34998"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34998\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}