{"id":38189,"date":"2024-11-26T13:11:20","date_gmt":"2024-11-26T05:11:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38189\/"},"modified":"2024-11-26T13:11:20","modified_gmt":"2024-11-26T05:11:20","slug":"java%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e5%af%b9%e6%95%b0%e7%bb%84%e8%bf%87%e6%bb%a4","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38189\/","title":{"rendered":"java\u600e\u4e48\u5b9e\u73b0\u5bf9\u6570\u7ec4\u8fc7\u6ee4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u5bf9\u6570\u7ec4\u8fc7\u6ee4\u7684\u65b9\u6cd5\u6709\uff1ajava 8 \u4e2d\u7684\u6d41 api\uff1afilter() \u548c distinct()apache commons lang \u7684 arrayutils\uff1afilter() \u548c removeduplicates()\u81ea\u5b9a\u4e49\u8fc7\u6ee4\u51fd\u6570\uff1aarrays.stream() \u548c filter()\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111410250954239.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5b9e\u73b0\u5bf9\u6570\u7ec4\u8fc7\u6ee4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5b9e\u73b0\u5bf9\u6570\u7ec4\u8fc7\u6ee4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u5bf9\u6570\u7ec4\u8fc7\u6ee4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u8fc7\u6ee4\uff1a<\/p>\n<p><strong>1. Java 8 \u4e2d\u7684\u6d41 API<\/strong><\/p>\n<p>\u6d41 API \u63d0\u4f9b\u4e86\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u8fc7\u6ee4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li> <strong>filter()<\/strong>\uff1a\u6839\u636e\u7ed9\u5b9a\u8c13\u8bcd\u8fc7\u6ee4\u5143\u7d20\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6d41\uff0c\u5176\u4e2d\u5305\u542b\u6ee1\u8db3\u8c13\u8bcd\u7684\u5143\u7d20\u3002<\/li>\n<li> <strong>distinct()<\/strong>\uff1a\u4ece\u6d41\u4e2d\u79fb\u9664\u91cd\u590d\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import java.util.Arrays;\n\npublic class ArrayFilter {\n\n    public static void main(String[] args) {\n        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};\n\n        \/\/ \u8fc7\u6ee4\u51fa\u5076\u6570\n        int[] evenNumbers = Arrays.stream(numbers)\n                .filter(n -&gt; n % 2 == 0)\n                .toArray();\n\n        \/\/ \u8fc7\u6ee4\u51fa\u91cd\u590d\u5143\u7d20\n        int[] distinctNumbers = Arrays.stream(numbers)\n                .distinct()\n                .toArray();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. Apache Commons Lang \u7684 ArrayUtils<\/strong><\/p>\n<p>Apache Commons Lang \u63d0\u4f9b\u4e86\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u8fc7\u6ee4\uff1a<\/p>\n<ul>\n<li> <strong>filter()<\/strong>\uff1a\u4f7f\u7528\u7ed9\u5b9a\u7684\u8fc7\u6ee4\u5668\u51fd\u6570\u8fc7\u6ee4\u6570\u7ec4\u5143\u7d20\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import org.apache.commons.lang3.ArrayUtils;\n\npublic class ArrayFilter {\n\n    public static void main(String[] args) {\n        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};\n\n        \/\/ \u8fc7\u6ee4\u51fa\u5076\u6570\n        int[] evenNumbers = ArrayUtils.filter(numbers, n -&gt; n % 2 == 0);\n\n        \/\/ \u8fc7\u6ee4\u51fa\u91cd\u590d\u5143\u7d20\n        int[] distinctNumbers = ArrayUtils.removeDuplicates(numbers);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u81ea\u5b9a\u4e49\u8fc7\u6ee4\u51fd\u6570<\/strong><\/p>\n<p>\u8fd8\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u8fc7\u6ee4\u51fd\u6570\uff0c\u5e76\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u5176\u5e94\u7528\u4e8e\u6570\u7ec4\uff1a<\/p>\n<ul>\n<li> <strong>Arrays.stream()<\/strong>\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\u3002<\/li>\n<li> <strong>filter()<\/strong>\uff1a\u4f7f\u7528\u81ea\u5b9a\u4e49\u8fc7\u6ee4\u51fd\u6570\u8fc7\u6ee4\u6d41\u3002<\/li>\n<\/ul>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public class ArrayFilter {\n\n    public static void main(String[] args) {\n        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};\n\n        \/\/ \u8fc7\u6ee4\u51fa\u5076\u6570\n        int[] evenNumbers = Arrays.stream(numbers)\n                .filter(ArrayFilter::isEven)\n                .toArray();\n\n        \/\/ \u8fc7\u6ee4\u51fa\u5947\u6570\n        int[] oddNumbers = Arrays.stream(numbers)\n                .filter(ArrayFilter::isOdd)\n                .toArray();\n\n        \/\/ \u81ea\u5b9a\u4e49\u8fc7\u6ee4\u51fd\u6570\n        private static boolean isEven(int n) {\n            return n % 2 == 0;\n        }\n\n        private static boolean isOdd(int n) {\n            return n % 2 != 0;\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5b9e\u73b0\u5bf9\u6570\u7ec4\u8fc7\u6ee4\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 \u4e2d\uff0c\u5bf9\u6570\u7ec4\u8fc7\u6ee4\u7684\u65b9\u6cd5\u6709\uff1ajava 8 \u4e2d\u7684\u6d41 api\uff1afilter() \u548c distinct()apache commons lang \u7684 arrayutils\uff1afilter() \u548c removeduplicates()\u81ea\u5b9a\u4e49\u8fc7\u6ee4\u51fd\u6570\uff1aarrays.stream() \u548c filter() Java \u4e2d\u5bf9\u6570\u7ec4\u8fc7\u6ee4 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u8fc7\u6ee4\uff1a 1. Java 8 \u4e2d\u7684\u6d41 API \u6d41 API \u63d0\u4f9b\u4e86\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u8fc7\u6ee4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b filter()\uff1a\u6839\u636e\u7ed9\u5b9a\u8c13\u8bcd\u8fc7\u6ee4\u5143\u7d20\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6d41\uff0c\u5176\u4e2d\u5305\u542b\u6ee1\u8db3\u8c13\u8bcd\u7684\u5143\u7d20\u3002 distinct()\uff1a\u4ece\u6d41\u4e2d\u79fb\u9664\u91cd\u590d\u5143\u7d20\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a import java.util.Arrays; public class ArrayFilter { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5, 6, 7, [&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-38189","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38189","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=38189"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38189\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}