{"id":65751,"date":"2025-05-03T09:10:04","date_gmt":"2025-05-03T01:10:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65751\/"},"modified":"2025-05-03T09:10:04","modified_gmt":"2025-05-03T01:10:04","slug":"java%e4%b8%ad%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8e%bb%e7%a9%ba-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65751\/","title":{"rendered":"java\u4e2d\u6570\u7ec4\u600e\u4e48\u53bb\u7a7a"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u5728 java \u4e2d\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u5143\u7d20\uff1f\u4f7f\u7528 stream api \u8fc7\u6ee4\u7a7a\u5143\u7d20\u3002\u4f7f\u7528 guava \u5e93\u8fc7\u6ee4\u7a7a\u5143\u7d20\u3002\u4f7f\u7528\u5faa\u73af\u548c arraylist \u904d\u5386\u6570\u7ec4\u5e76\u6dfb\u52a0\u975e\u7a7a\u5143\u7d20\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111309100267119.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u6570\u7ec4\u600e\u4e48\u53bb\u7a7a\u63d2\u56fe\" alt=\"java\u4e2d\u6570\u7ec4\u600e\u4e48\u53bb\u7a7a\u63d2\u56fe\" \/><\/p>\n<h2>Java \u4e2d\u6570\u7ec4\u53bb\u7a7a\u7684\u6280\u5de7<\/h2>\n<p><strong>\u5982\u4f55\u4ece Java \u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u5143\u7d20\uff1f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u5143\u7d20\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Stream API<\/strong><\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] arrayWithoutNulls = Arrays.stream(array).filter(Objects::nonNull).toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 Arrays.stream() \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\u3002<\/li>\n<li>\u4f7f\u7528 filter(Objects::nonNull) \u8fc7\u6ee4\u6389\u7a7a\u5143\u7d20\u3002<\/li>\n<li>\u4f7f\u7528 toArray(String[]::new) \u5c06\u8fc7\u6ee4\u540e\u7684\u6d41\u8f6c\u6362\u4e3a\u65b0\u7684\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528 Guava \u5e93<\/strong><\/p>\n<p><strong>\u4f9d\u8d56\uff1a<\/strong><\/p>\n<pre>&lt;dependency&gt;\n    &lt;groupId&gt;com.google.guava&lt;\/groupId&gt;\n    &lt;artifactId&gt;guava&lt;\/artifactId&gt;\n    &lt;version&gt;31.1-jre&lt;\/version&gt;\n&lt;\/dependency&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] arrayWithoutNulls = Iterables.filter(Arrays.asList(array), Predicates.notNull()).toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 Arrays.asList(array) \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/li>\n<li>\u4f7f\u7528 Iterables.filter() \u8fc7\u6ee4\u6389\u7a7a\u5143\u7d20\u3002<\/li>\n<li>\u4f7f\u7528 toArray(String[]::new) \u5c06\u8fc7\u6ee4\u540e\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u65b0\u7684\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><strong>3. \u4f7f\u7528\u5faa\u73af\u548c ArrayList<\/strong><\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>List&lt;String&gt; listWithoutNulls = new ArrayList&lt;&gt;();\nfor (String element : array) {\n    if (element != null) {\n        listWithoutNulls.add(element);\n    }\n}\nString[] arrayWithoutNulls = listWithoutNulls.toArray(new String[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u7ec4\u3002<\/li>\n<li>\u5bf9\u4e8e\u975e\u7a7a\u5143\u7d20\uff0c\u5c06\u5176\u6dfb\u52a0\u5230 ArrayList \u4e2d\u3002<\/li>\n<li>\u4f7f\u7528 toArray(new String[0]) \u5c06 ArrayList \u8f6c\u6362\u4e3a\u65b0\u7684\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u6570\u7ec4\u600e\u4e48\u53bb\u7a7a\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 java \u4e2d\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u5143\u7d20\uff1f\u4f7f\u7528 stream api \u8fc7\u6ee4\u7a7a\u5143\u7d20\u3002\u4f7f\u7528 guava \u5e93\u8fc7\u6ee4\u7a7a\u5143\u7d20\u3002\u4f7f\u7528\u5faa\u73af\u548c arraylist \u904d\u5386\u6570\u7ec4\u5e76\u6dfb\u52a0\u975e\u7a7a\u5143\u7d20\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002 Java \u4e2d\u6570\u7ec4\u53bb\u7a7a\u7684\u6280\u5de7 \u5982\u4f55\u4ece Java \u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u5143\u7d20\uff1f \u5728 Java \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u7a7a\u5143\u7d20\uff1a 1. \u4f7f\u7528 Stream API \u8bed\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] arrayWithoutNulls = Arrays.stream(array).filter(Objects::nonNull).toArray(String[]::new); \u767b\u5f55\u540e\u590d\u5236 \u8bf4\u660e\uff1a \u4f7f\u7528 Arrays.stream() \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\u3002 \u4f7f\u7528 filter(Objects::nonNull) \u8fc7\u6ee4\u6389\u7a7a\u5143\u7d20\u3002 \u4f7f\u7528 toArray(String[]::new) \u5c06\u8fc7\u6ee4\u540e\u7684\u6d41\u8f6c\u6362\u4e3a\u65b0\u7684\u6570\u7ec4\u3002 2. \u4f7f\u7528 Guava \u5e93 \u4f9d\u8d56\uff1a &lt;dependency&gt; &lt;groupId&gt;com.google.guava&lt;\/groupId&gt; &lt;artifactId&gt;guava&lt;\/artifactId&gt; &lt;version&gt;31.1-jre&lt;\/version&gt; &lt;\/dependency&gt; \u767b\u5f55\u540e\u590d\u5236 \u8bed\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] arrayWithoutNulls = [&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-65751","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65751","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=65751"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65751\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}