{"id":39961,"date":"2024-11-26T12:39:34","date_gmt":"2024-11-26T04:39:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39961\/"},"modified":"2024-11-26T12:39:34","modified_gmt":"2024-11-26T04:39:34","slug":"java-streamdistinct","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39961\/","title":{"rendered":"Java Streamdistinct()"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173182447219626.jpg\" class=\"aligncenter\" title=\"Java Streamdistinct()\u63d2\u56fe\" alt=\"Java Streamdistinct()\u63d2\u56fe\" \/><\/p>\n<p>java \u4e2d\u7684 stream.distinct() \u65b9\u6cd5\u7528\u4e8e\u8fc7\u6ee4\u6389\u6d41\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff0c\u786e\u4fdd\u751f\u6210\u7684\u6d41\u4ec5\u5305\u542b\u552f\u4e00\u5143\u7d20\u3002\u5b83\u57fa\u4e8e\u6d41\u4e2d\u5bf9\u8c61\u7684 equals() \u65b9\u6cd5\u5de5\u4f5c\u3002<\/p>\n<p>\u6b64\u65b9\u6cd5\u662f java 8 \u4e2d\u5f15\u5165\u7684 java stream api \u7684\u4e00\u90e8\u5206\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u5177\u6709\u91cd\u590d\u503c\u7684\u96c6\u5408\u6216\u6570\u7ec4\u3002<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a\u4ece\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u5220\u9664\u91cd\u590d\u9879<\/strong><\/p>\n<p>\u60f3\u8c61\u4e00\u4e0b\u4f60\u6709\u4e00\u4e2a\u540d\u5b57\u5217\u8868\uff0c\u5176\u4e2d\u4e00\u4e9b\u540d\u5b57\u662f\u91cd\u590d\u7684\u3002\u60a8\u60f3\u8981\u4e00\u4e2a\u552f\u4e00\u540d\u79f0\u7684\u5217\u8868\u3002<\/p>\n<pre>import java.util.list;\nimport java.util.stream.collectors;\n\npublic class main {\n    public static void main(string[] args) {\n        \/\/ list of names with duplicates\n        list&lt;string&gt; names = list.of(\"alice\", \"bob\", \"alice\", \"charlie\", \"bob\", \"david\");\n\n        \/\/ use stream.distinct() to remove duplicates\n        list&lt;string&gt; uniquenames = names.stream()\n                                        .distinct()\n                                        .collect(collectors.tolist());\n\n        system.out.println(uniquenames);\n        \/\/ output: [alice, bob, charlie, david]\n    }\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5de5\u4f5c\u539f\u7406\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>distinct() \u65b9\u6cd5\u6bd4\u8f83\u6bcf\u4e2a\u540d\u79f0\u5e76\u4ec5\u4fdd\u7559\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u91cd\u590d\u9879\u3002<\/p>\n<p>\u793a\u4f8b 2\uff1a\u4ece\u6570\u5b57\u5217\u8868\u4e2d\u5220\u9664\u91cd\u590d\u9879<br \/> \u8ba9\u6211\u4eec\u5217\u51fa\u5b58\u5728\u91cd\u590d\u7684\u6570\u5b57\u5e76\u4ec5\u63d0\u53d6\u552f\u4e00\u7684\u6570\u5b57\u3002<\/p>\n<pre>import java.util.list;\nimport java.util.stream.collectors;\n\npublic class main {\n    public static void main(string[] args) {\n        \/\/ list of numbers with duplicates\n        list&lt;integer&gt; numbers = list.of(1, 2, 3, 2, 4, 3, 5);\n\n        \/\/ use stream.distinct() to remove duplicates\n        list&lt;integer&gt; uniquenumbers = numbers.stream()\n                                             .distinct()\n                                             .collect(collectors.tolist());\n\n        system.out.println(uniquenumbers);\n        \/\/ output: [1, 2, 3, 4, 5]\n    }\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5de5\u4f5c\u539f\u7406\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4f7f\u7528\u6570\u5b57\u7684\u81ea\u7136\u76f8\u7b49\u6027\u8fdb\u884c\u6bd4\u8f83\uff08\u57fa\u4e8e\u6574\u6570\u7684 equals()\uff09\uff0c\u56e0\u6b64\u91cd\u590d\u9879\u4f1a\u88ab\u8fc7\u6ee4\u6389\u3002<\/p>\n<p>\u793a\u4f8b 3\uff1a\u4f7f\u7528\u7b80\u5355\u5bf9\u8c61<br \/> \u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a product \u7c7b\u5e76\u6839\u636e\u4ea7\u54c1\u7684 id \u5220\u9664\u91cd\u590d\u9879\u3002<\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<pre>import java.util.list;\nimport java.util.objects;\nimport java.util.stream.collectors;\n\nclass product {\n    private int id;\n    private string name;\n\n    public product(int id, string name) {\n        this.id = id;\n        this.name = name;\n    }\n\n    public int getid() {\n        return id;\n    }\n\n    public string getname() {\n        return name;\n    }\n\n    @override\n    public boolean equals(object o) {\n        if (this == o) return true;\n        if (o == null || getclass() != o.getclass()) return false;\n        product product = (product) o;\n        return id == product.id;\n    }\n\n    @override\n    public int hashcode() {\n        return objects.hash(id);\n    }\n\n    @override\n    public string tostring() {\n        return \"product{id=\" + id + \", name='\" + name + \"'}\";\n    }\n}\n\npublic class main {\n    public static void main(string[] args) {\n        \/\/ list of products with duplicates (based on id)\n        list&lt;product&gt; products = list.of(\n            new product(1, \"laptop\"),\n            new product(2, \"tablet\"),\n            new product(1, \"laptop\"), \/\/ duplicate\n            new product(3, \"smartphone\"),\n            new product(2, \"tablet\")  \/\/ duplicate\n        );\n\n        \/\/ use stream.distinct() to remove duplicates\n        list&lt;product&gt; uniqueproducts = products.stream()\n                                               .distinct()\n                                               .collect(collectors.tolist());\n\n        system.out.println(uniqueproducts);\n        \/\/ output:\n        \/\/ [product{id=1, name='laptop'}, product{id=2, name='tablet'}, product{id=3, name='smartphone'}]\n    }\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8bf4\u660e\uff1a<\/p>\n<p>\u76f8\u7b49\u6027\u68c0\u67e5\uff1a\u91cd\u5199 equals() \u65b9\u6cd5\u4ee5\u786e\u4fdd product \u5bf9\u8c61\u6839\u636e\u5176 id \u88ab\u89c6\u4e3a\u76f8\u7b49\u3002<\/p>\n<p>\u53bb\u91cd\uff1a\u5f53distinct()\u9047\u5230\u4e24\u4e2a\u5177\u6709\u76f8\u540cid\u7684\u4ea7\u54c1\u65f6\uff0c\u53ea\u4fdd\u7559\u7b2c\u4e00\u4e2a\u3002<\/p>\n<p>\u8f93\u51fa\uff1a\u60a8\u83b7\u5f97\u72ec\u7279\u4ea7\u54c1\u7684\u5217\u8868\u3002<\/p>\n<p>\u7b80\u5316\u7406\u89e3<\/p>\n<ol>\n<li>\u5bf9\u4e8e\u539f\u59cb\u6216\u7b80\u5355\u5bf9\u8c61\uff08\u5982\u6574\u6570\u3001\u5b57\u7b26\u4e32\uff09\uff1a<\/li>\n<\/ol>\n<p>distinct() \u901a\u8fc7\u76f4\u63a5\u6bd4\u8f83\u503c\u6765\u5220\u9664\u91cd\u590d\u9879\u3002<\/p>\n<p>\u793a\u4f8b\uff1a[1, 2, 2, 3] \u53d8\u4e3a [1, 2, 3]\u3002<\/p>\n<ol>\n<li>\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff1a<\/li>\n<\/ol>\n<p>\u60a8\u9700\u8981\u4e3a\u7c7b\u5b9e\u73b0 equals() \u548c hashcode() \u65b9\u6cd5\u3002<\/p>\n<p>distinct() \u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u786e\u5b9a\u4e24\u4e2a\u5bf9\u8c61\u662f\u5426\u91cd\u590d\u3002<\/p>\n<p>\u7528\u4f8b\uff1a\u8fc7\u6ee4\u7528\u6237\u8f93\u5165\u4e2d\u7684\u91cd\u590d\u540d\u79f0<br \/> \u60f3\u8c61\u4e00\u4e0b\uff0c\u7528\u6237\u5728\u8868\u5355\u4e2d\u91cd\u590d\u8f93\u5165\u540d\u79f0\uff0c\u5e76\u4e14\u60a8\u5e0c\u671b\u786e\u4fdd\u4ec5\u5b58\u50a8\u552f\u4e00\u7684\u540d\u79f0\u3002<\/p>\n<pre>import java.util.ArrayList;\nimport java.util.List;\nimport java.util.stream.Collectors;\n\npublic class Main {\n    public static void main(String[] args) {\n        \/\/ Simulate user input\n        List&lt;String&gt; userInput = new ArrayList&lt;&gt;();\n        userInput.add(\"John\");\n        userInput.add(\"Mary\");\n        userInput.add(\"John\"); \/\/ Duplicate\n        userInput.add(\"Alice\");\n\n        \/\/ Remove duplicates\n        List&lt;String&gt; uniqueInput = userInput.stream()\n                                            .distinct()\n                                            .collect(Collectors.toList());\n\n        System.out.println(uniqueInput); \/\/ Output: [John, Mary, Alice]\n    }\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u4ec5\u5b58\u50a8\u552f\u4e00\u7684\u540d\u79f0\uff0c\u800c\u4e0d\u9700\u8981\u624b\u52a8\u68c0\u67e5\u91cd\u590d\u9879\u3002<\/p>\n<p><strong>\u6700\u540e\u7684\u6536\u83b7\uff1a<\/strong><\/p>\n<p>distinct() \u5f88\u7b80\u5355\uff1a\u5b83\u4ece\u6d41\u4e2d\u5220\u9664\u91cd\u590d\u9879\u3002<\/p>\n<p>\u5bf9\u4e8e\u57fa\u5143\u6216\u4e0d\u53ef\u53d8\u7c7b\u578b\uff1a\u76f4\u63a5\u4f7f\u7528\u5373\u53ef\u3002<\/p>\n<p>\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff1a\u786e\u4fdd\u6b63\u786e\u7684 equals() \u548c hashcode() \u5b9e\u73b0\u3002<\/p>\n<p>\u5b9e\u7528\u63d0\u793a\uff1a\u7528\u5b83\u6765\u6e05\u7406\u4efb\u4f55\u5f62\u5f0f\u7684\u91cd\u590d\u6570\u636e\uff08\u4f8b\u5982\u5217\u8868\u3001\u7528\u6237\u8f93\u5165\u3001\u6570\u636e\u5e93\u7ed3\u679c\uff09\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava Streamdistinct()\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\u7684 stream.distinct() \u65b9\u6cd5\u7528\u4e8e\u8fc7\u6ee4\u6389\u6d41\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff0c\u786e\u4fdd\u751f\u6210\u7684\u6d41\u4ec5\u5305\u542b\u552f\u4e00\u5143\u7d20\u3002\u5b83\u57fa\u4e8e\u6d41\u4e2d\u5bf9\u8c61\u7684 equals() \u65b9\u6cd5\u5de5\u4f5c\u3002 \u6b64\u65b9\u6cd5\u662f java 8 \u4e2d\u5f15\u5165\u7684 java stream api \u7684\u4e00\u90e8\u5206\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u5177\u6709\u91cd\u590d\u503c\u7684\u96c6\u5408\u6216\u6570\u7ec4\u3002 \u793a\u4f8b 1\uff1a\u4ece\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u5220\u9664\u91cd\u590d\u9879 \u60f3\u8c61\u4e00\u4e0b\u4f60\u6709\u4e00\u4e2a\u540d\u5b57\u5217\u8868\uff0c\u5176\u4e2d\u4e00\u4e9b\u540d\u5b57\u662f\u91cd\u590d\u7684\u3002\u60a8\u60f3\u8981\u4e00\u4e2a\u552f\u4e00\u540d\u79f0\u7684\u5217\u8868\u3002 import java.util.list; import java.util.stream.collectors; public class main { public static void main(string[] args) { \/\/ list of names with duplicates list&lt;string&gt; names = list.of(&#8220;alice&#8221;, &#8220;bob&#8221;, &#8220;alice&#8221;, &#8220;charlie&#8221;, &#8220;bob&#8221;, &#8220;david&#8221;); \/\/ use stream.distinct() to remove duplicates list&lt;string&gt; uniquenames = [&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-39961","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39961","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=39961"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39961\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}