{"id":37892,"date":"2024-11-26T16:13:25","date_gmt":"2024-11-26T08:13:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37892\/"},"modified":"2024-11-26T16:13:25","modified_gmt":"2024-11-26T08:13:25","slug":"java%e6%80%8e%e4%b9%88%e5%88%a4%e6%96%ad%e6%95%b0%e7%bb%84%e6%9c%89%e5%87%a0%e4%b8%aa%e5%ad%94","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37892\/","title":{"rendered":"java\u600e\u4e48\u5224\u65ad\u6570\u7ec4\u6709\u51e0\u4e2a\u5b54"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u5224\u65ad\u6570\u7ec4\u7a7a\u503c\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\uff1a1. \u4f7f\u7528 null \u67e5\u8be2\uff1b2. \u4f7f\u7528 arrays.stream() \u548c filter()\uff1b3. \u4f7f\u7528 int[] \u548c 0 \u521d\u59cb\u5316\uff1b4. \u4f7f\u7528 optional \u548c ispresent()\uff1b5. \u4f7f\u7528 guava \u7684 iterables.filter()\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111312331892713.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5224\u65ad\u6570\u7ec4\u6709\u51e0\u4e2a\u5b54\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5224\u65ad\u6570\u7ec4\u6709\u51e0\u4e2a\u5b54\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad Java \u6570\u7ec4\u4e2d\u6709\u51e0\u4e2a\u7a7a\u503c<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5224\u65ad\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u4e2a\u7a7a\u503c\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 null \u67e5\u8be2<\/strong><\/p>\n<pre>int countNullValues = 0;\nfor (Object element : array) {\n    if (element == null) {\n        countNullValues++;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.stream() \u548c filter()<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int countNullValues = (int) Arrays.stream(array)\n    .filter(Objects::isNull)\n    .count();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 int[] \u548c 0 \u521d\u59cb\u5316<\/strong><\/p>\n<pre>int[] countNullValues = new int[1];\nArrays.stream(array)\n    .forEach(element -&gt; {\n        if (element == null) {\n            countNullValues[0]++;\n        }\n    });<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 Optional \u548c isPresent()<\/strong><\/p>\n<pre>int countNullValues = 0;\nfor (Object element : array) {\n    Optional&lt;Object&gt; optionalElement = Optional.ofNullable(element);\n    if (!optionalElement.isPresent()) {\n        countNullValues++;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u4f7f\u7528 Guava \u7684 Iterables.size(Iterables.filter(array, Predicates.isNull()))<\/strong><\/p>\n<pre>import com.google.common.collect.Iterables;\nimport com.google.common.base.Predicates;\n\nint countNullValues = Iterables.size(Iterables.filter(array, Predicates.isNull()));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5224\u65ad\u6570\u7ec4\u6709\u51e0\u4e2a\u5b54\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\u5224\u65ad\u6570\u7ec4\u7a7a\u503c\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\uff1a1. \u4f7f\u7528 null \u67e5\u8be2\uff1b2. \u4f7f\u7528 arrays.stream() \u548c filter()\uff1b3. \u4f7f\u7528 int[] \u548c 0 \u521d\u59cb\u5316\uff1b4. \u4f7f\u7528 optional \u548c ispresent()\uff1b5. \u4f7f\u7528 guava \u7684 iterables.filter()\u3002 \u5982\u4f55\u5224\u65ad Java \u6570\u7ec4\u4e2d\u6709\u51e0\u4e2a\u7a7a\u503c \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5224\u65ad\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u4e2a\u7a7a\u503c\uff1a 1. \u4f7f\u7528 null \u67e5\u8be2 int countNullValues = 0; for (Object element : array) { if (element == null) { countNullValues++; } } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 Arrays.stream() \u548c [&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-37892","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37892","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=37892"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37892\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}