{"id":35607,"date":"2024-11-26T12:17:10","date_gmt":"2024-11-26T04:17:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35607\/"},"modified":"2024-11-26T12:17:10","modified_gmt":"2024-11-26T04:17:10","slug":"java%e6%80%8e%e4%b9%88%e5%af%b9%e4%b8%80%e5%a0%86%e6%95%b0%e5%80%bc%e6%95%b0%e7%bb%84%e5%8e%bb%e9%87%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35607\/","title":{"rendered":"java\u600e\u4e48\u5bf9\u4e00\u5806\u6570\u503c\u6570\u7ec4\u53bb\u91cd"},"content":{"rendered":"<blockquote><p>\n  java\u4e2d\u5bf9\u6570\u7ec4\u53bb\u91cd\u7684\u6700\u4f73\u65b9\u6cd5\u662f\uff1a\u4f7f\u7528arrays.stream(array).distinct().toarray()\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\u3001\u8fc7\u6ee4\u91cd\u590d\u5143\u7d20\u3001\u5e76\u8f6c\u6362\u4e3a\u65b0\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/04\/2024110406574022511.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5bf9\u4e00\u5806\u6570\u503c\u6570\u7ec4\u53bb\u91cd\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5bf9\u4e00\u5806\u6570\u503c\u6570\u7ec4\u53bb\u91cd\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u4e2d\u6570\u7ec4\u53bb\u91cd\u7684\u6700\u4f73\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p>Java\u4e2d\u5bf9\u6570\u7ec4\u53bb\u91cd\u7684\u6700\u5feb\u6377\u65b9\u6cd5\u662f\u4f7f\u7528Arrays.stream(array).distinct().toArray()\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u8bf4\u660e\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1. \u4f7f\u7528\u6d41\u5f0f\u5904\u7406<\/strong><\/p>\n<p>Arrays.stream(array)\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6d41\u3002\u6d41\u662fJava 8\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5141\u8bb8\u5bf9\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u987a\u5e8f\u5904\u7406\u3002<\/p>\n<p><strong>2. \u8c03\u7528distinct()\u65b9\u6cd5<\/strong><\/p>\n<p>distinct()\u65b9\u6cd5\u8fc7\u6ee4\u6389\u6d41\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u5b83\u4f7f\u7528equals()\u65b9\u6cd5\u6bd4\u8f83\u5143\u7d20\uff0c\u56e0\u6b64\u81ea\u5b9a\u4e49\u5bf9\u8c61\u5fc5\u987b\u6b63\u786e\u5b9e\u73b0equals()\u65b9\u6cd5\u624d\u80fd\u6b63\u786e\u53bb\u91cd\u3002<\/p>\n<p><strong>3. \u8f6c\u6362\u56de\u6570\u7ec4<\/strong><\/p>\n<p>toArray()\u65b9\u6cd5\u5c06\u53bb\u91cd\u7684\u6d41\u8f6c\u6362\u4e3a\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002\u6570\u7ec4\u7c7b\u578b\u4e0e\u539f\u59cb\u6570\u7ec4\u76f8\u540c\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>int[] numbers = {1, 2, 3, 4, 5, 1, 2, 3};\nnumbers = Arrays.stream(numbers).distinct().toArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\u540e\uff0cnumbers\u6570\u7ec4\u5c06\u5305\u542b\u53bb\u91cd\u7684\u503c\uff1a[1, 2, 3, 4, 5]\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e\u5c0f\u6570\u7ec4\uff0c\u4f7f\u7528Set\u53bb\u91cd\u53ef\u80fd\u6bd4\u6d41\u5f0f\u5904\u7406\u66f4\u5feb\u3002<\/li>\n<li>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u7ec4\uff0c\u8003\u8651\u4f7f\u7528\u81ea\u5b9a\u4e49\u89e3\u51b3\u65b9\u6848\uff0c\u4f8b\u5982\u57fa\u4e8e\u54c8\u5e0c\u8868\u6216\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5bf9\u4e00\u5806\u6570\u503c\u6570\u7ec4\u53bb\u91cd\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>java\u4e2d\u5bf9\u6570\u7ec4\u53bb\u91cd\u7684\u6700\u4f73\u65b9\u6cd5\u662f\uff1a\u4f7f\u7528arrays.stream(array).distinct().toarray()\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\u3001\u8fc7\u6ee4\u91cd\u590d\u5143\u7d20\u3001\u5e76\u8f6c\u6362\u4e3a\u65b0\u6570\u7ec4\u3002 Java\u4e2d\u6570\u7ec4\u53bb\u91cd\u7684\u6700\u4f73\u65b9\u6cd5 \u56de\u7b54\uff1a Java\u4e2d\u5bf9\u6570\u7ec4\u53bb\u91cd\u7684\u6700\u5feb\u6377\u65b9\u6cd5\u662f\u4f7f\u7528Arrays.stream(array).distinct().toArray()\u3002 \u8be6\u7ec6\u8bf4\u660e\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1. \u4f7f\u7528\u6d41\u5f0f\u5904\u7406 Arrays.stream(array)\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6d41\u3002\u6d41\u662fJava 8\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5141\u8bb8\u5bf9\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u987a\u5e8f\u5904\u7406\u3002 2. \u8c03\u7528distinct()\u65b9\u6cd5 distinct()\u65b9\u6cd5\u8fc7\u6ee4\u6389\u6d41\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u5b83\u4f7f\u7528equals()\u65b9\u6cd5\u6bd4\u8f83\u5143\u7d20\uff0c\u56e0\u6b64\u81ea\u5b9a\u4e49\u5bf9\u8c61\u5fc5\u987b\u6b63\u786e\u5b9e\u73b0equals()\u65b9\u6cd5\u624d\u80fd\u6b63\u786e\u53bb\u91cd\u3002 3. \u8f6c\u6362\u56de\u6570\u7ec4 toArray()\u65b9\u6cd5\u5c06\u53bb\u91cd\u7684\u6d41\u8f6c\u6362\u4e3a\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002\u6570\u7ec4\u7c7b\u578b\u4e0e\u539f\u59cb\u6570\u7ec4\u76f8\u540c\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a int[] numbers = {1, 2, 3, 4, 5, 1, 2, 3}; numbers = Arrays.stream(numbers).distinct().toArray(); \u767b\u5f55\u540e\u590d\u5236 \u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\u540e\uff0cnumbers\u6570\u7ec4\u5c06\u5305\u542b\u53bb\u91cd\u7684\u503c\uff1a[1, 2, 3, 4, 5]\u3002 \u6ce8\u610f\uff1a \u5bf9\u4e8e\u5c0f\u6570\u7ec4\uff0c\u4f7f\u7528Set\u53bb\u91cd\u53ef\u80fd\u6bd4\u6d41\u5f0f\u5904\u7406\u66f4\u5feb\u3002 \u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u7ec4\uff0c\u8003\u8651\u4f7f\u7528\u81ea\u5b9a\u4e49\u89e3\u51b3\u65b9\u6848\uff0c\u4f8b\u5982\u57fa\u4e8e\u54c8\u5e0c\u8868\u6216\u6392\u5e8f\u3002 \u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5bf9\u4e00\u5806\u6570\u503c\u6570\u7ec4\u53bb\u91cd\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/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-35607","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35607","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=35607"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35607\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}