{"id":39665,"date":"2024-11-26T10:21:40","date_gmt":"2024-11-26T02:21:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39665\/"},"modified":"2024-11-26T10:21:40","modified_gmt":"2024-11-26T02:21:40","slug":"java%e6%80%8e%e4%b9%88%e5%8e%bb%e9%99%a4%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e7%a9%ba%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39665\/","title":{"rendered":"java\u600e\u4e48\u53bb\u9664\u6570\u7ec4\u4e2d\u7684\u7a7a\u503c"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u4ece\u6570\u7ec4\u5220\u9664\u7a7a\u503c\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u65b9\u6cd5\uff1ajava 8 stream api\uff1a\u4f7f\u7528 filter() \u65b9\u6cd5\u8fc7\u6ee4\u7a7a\u503c\u3002apache commons lang \u5e93\uff1a\u4f7f\u7528 arrayutils.removeelements() \u65b9\u6cd5\u5220\u9664\u7a7a\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111620482716852.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u53bb\u9664\u6570\u7ec4\u4e2d\u7684\u7a7a\u503c\u63d2\u56fe\" alt=\"java\u600e\u4e48\u53bb\u9664\u6570\u7ec4\u4e2d\u7684\u7a7a\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4ece Java \u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u503c<\/strong><\/p>\n<p>\u5728 Java \u4e2d\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u503c\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u4e24\u79cd\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1aJava 8 Stream API<\/strong><\/p>\n<p>\u4f7f\u7528 Java 8 Stream API \u662f\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u503c\u6700\u7b80\u5355\u3001\u6700\u7b80\u6d01\u7684\u65b9\u6cd5\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] array = new String[]{\"a\", null, \"b\", null, \"c\"};\n\n\/\/ \u4f7f\u7528 Stream API \u8fc7\u6ee4\u7a7a\u503c\nString[] filteredArray = Arrays.stream(array)\n        .filter(element -&gt; element != null)\n        .toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 Apache Commons Lang<\/strong><\/p>\n<p>Apache Commons Lang \u5e93\u63d0\u4f9b\u4e86 ArrayUtils.removeElements \u65b9\u6cd5\uff0c\u53ef\u8f7b\u677e\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u503c\u3002<\/p>\n<pre>String[] array = new String[]{\"a\", null, \"b\", null, \"c\"};\n\n\/\/ \u4f7f\u7528 Apache Commons Lang \u5e93\u5220\u9664\u7a7a\u503c\nString[] filteredArray = ArrayUtils.removeElements(array, null);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u8fd9\u4e24\u79cd\u65b9\u6cd5<strong>\u90fd\u4e0d\u4f1a<\/strong>\u4fee\u6539\u539f\u59cb\u6570\u7ec4\u3002\u5b83\u4eec\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u7a7a\u503c\u3002<\/li>\n<li>\u5982\u679c\u6570\u7ec4\u4e2d\u6ca1\u6709\u7a7a\u503c\uff0c\u5219\u8fd9\u4e24\u79cd\u65b9\u6cd5\u8fd4\u56de\u7684\u6570\u7ec4\u4e0e\u539f\u59cb\u6570\u7ec4\u76f8\u540c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u53bb\u9664\u6570\u7ec4\u4e2d\u7684\u7a7a\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 \u4e2d\u4ece\u6570\u7ec4\u5220\u9664\u7a7a\u503c\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u65b9\u6cd5\uff1ajava 8 stream api\uff1a\u4f7f\u7528 filter() \u65b9\u6cd5\u8fc7\u6ee4\u7a7a\u503c\u3002apache commons lang \u5e93\uff1a\u4f7f\u7528 arrayutils.removeelements() \u65b9\u6cd5\u5220\u9664\u7a7a\u503c\u3002 \u5982\u4f55\u4ece Java \u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u503c \u5728 Java \u4e2d\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u503c\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u4e24\u79cd\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff1a \u65b9\u6cd5 1\uff1aJava 8 Stream API \u4f7f\u7528 Java 8 Stream API \u662f\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u503c\u6700\u7b80\u5355\u3001\u6700\u7b80\u6d01\u7684\u65b9\u6cd5\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] array = new String[]{&#8220;a&#8221;, null, &#8220;b&#8221;, null, &#8220;c&#8221;}; \/\/ \u4f7f\u7528 Stream API \u8fc7\u6ee4\u7a7a\u503c String[] filteredArray = Arrays.stream(array) .filter(element -&gt; element != null) .toArray(String[]::new); [&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-39665","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39665","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=39665"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39665\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}