{"id":291880,"date":"2024-05-20T14:02:30","date_gmt":"2024-05-20T06:02:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/291880.html"},"modified":"2024-05-20T14:02:35","modified_gmt":"2024-05-20T06:02:35","slug":"java%e5%a6%82%e4%bd%95%e4%bc%98%e9%9b%85%e7%9a%84%e5%88%a4%e7%a9%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/291880.html","title":{"rendered":"Java\u5982\u4f55\u4f18\u96c5\u7684\u5224\u7a7a"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27134454\/3251ff00-2032-4568-9f42-95a1c4c3d56f.webp\" alt=\"Java\u5982\u4f55\u4f18\u96c5\u7684\u5224\u7a7a\" \/><\/p>\n<p><p><strong>Java\u5224\u7a7a\u64cd\u4f5c\u9700\u8981\u6ce8\u610f\u591a\u79cd\u60c5\u5f62\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u5bf9\u8c61\u4e3anull\u3001\u5b57\u7b26\u4e32\u4e3a\u7a7a\u767d\u5b57\u7b26\u4e32\u3001\u96c6\u5408\u7c7b\u5bf9\u8c61\u4e3a\u7a7a\u6216\u65e0\u5143\u7d20\uff0c\u4ee5\u53ca\u6570\u7ec4\u4e3a\u7a7a\u6216\u65e0\u5143\u7d20\u3002<\/strong> \u5224\u7a7a\u7684\u65b9\u6cd5\u901a\u5e38\u6d89\u53ca\u5230\u4f7f\u7528\u6807\u51c6\u7684\u5e93\u51fd\u6570\uff0c\u6bd4\u5982<code>Objects.isNull()<\/code>\u3001<code>StringUtils.isEmpty()<\/code>\u3001\u4ee5\u53caGoogle Guava\u63d0\u4f9b\u7684<code>Optional<\/code>\u7c7b\u3002\u5728Java 8\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528<code>Optional<\/code>\u7c7b\u6765\u7b80\u5316\u5224\u7a7a\u903b\u8f91\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u5904\u7406\u53ef\u80fd\u4e3anull\u7684\u5bf9\u8c61\uff0c\u540c\u65f6\u80fd\u591f\u907f\u514d\u663e\u5f0fnull\u68c0\u67e5\uff0c\u8fdb\u800c\u51cf\u5c11\u7a7a\u6307\u9488\u5f02\u5e38\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u6807\u51c6\u5e93\u51fd\u6570\u7684\u8fd0\u7528<\/h2>\n<\/p>\n<p><p>\u5728Java\u4e2d\uff0c\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u5e2e\u52a9\u5f00\u53d1\u8005\u8fdb\u884c\u5224\u7a7a\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u5b57\u7b26\u4e32\u5224\u7a7a<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u9700\u8981\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4e3anull\u6216\u8005\u7a7a\u5b57\u7b26\u4e32\uff0c\u53ef\u4f7f\u7528<code>String<\/code>\u7c7b\u7684<code>isEmpty()<\/code>\u65b9\u6cd5\uff0c\u6216\u8005<code>Objects<\/code>\u7c7b\u7684<code>isNull()<\/code>\u65b9\u6cd5\u7ed3\u5408<code>String.trim()<\/code>\u53bb\u9664\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">String str = &quot;...&quot;;<\/p>\n<p>if (str != null &amp;&amp; !str.trim().isEmpty()) {<\/p>\n<p>    \/\/ \u5b57\u7b26\u4e32\u4e0d\u4e3a\u7a7a\u64cd\u4f5c<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u96c6\u5408\u5224\u7a7a<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u7c7b\uff08\u5982List\u3001Set\u3001Map\u7b49\uff09\u53ef\u80fd\u4e3a\u7a7a\u6216\u8005\u6ca1\u6709\u5143\u7d20\uff0c\u901a\u5e38\u6211\u4eec\u4f7f\u7528<code>isEmpty()<\/code>\u6765\u68c0\u67e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">List&lt;String&gt; list = ...;<\/p>\n<p>if (list != null &amp;&amp; !list.isEmpty()) {<\/p>\n<p>    \/\/ \u96c6\u5408\u4e0d\u4e3a\u7a7a\u64cd\u4f5c<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001Optional\u7c7b\u7684\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>Java 8\u5f15\u5165\u4e86<code>Optional<\/code>\u7c7b\uff0c\u8be5\u7c7b\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u7f16\u7a0b\u65f6\u907f\u514d\u76f4\u63a5\u8fd4\u56denull\u503c\uff0c\u51cf\u5c11\u7a7a\u6307\u9488\u5f02\u5e38\u7684\u53d1\u751f\u3002<\/p>\n<\/p>\n<p><h3>Optional\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Optional\u7684\u6807\u51c6\u6a21\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">Optional&lt;String&gt; optionalStr = Optional.ofNullable(string);<\/p>\n<p>optionalStr.ifPresent(value -&gt; {<\/p>\n<p>    \/\/ \u5f53\u5b57\u7b26\u4e32\u975e\u7a7a\u65f6\u6267\u884c\u64cd\u4f5c<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u914d\u5408lambda\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u914d\u5408lambda\u8868\u8fbe\u5f0f\uff0c<code>Optional<\/code>\u4e0d\u4ec5\u80fd\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\u8fd8\u80fd\u7ed3\u5408\u51fd\u6570\u5f0f\u7f16\u7a0b\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">Optional&lt;String&gt; optionalStr = Optional.ofNullable(string);<\/p>\n<p>optionalStr.map(String::trim).filter(str -&gt; !str.isEmpty()).ifPresent(System.out::println);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001Google Guava\u7684\u652f\u6301<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0cGoogle\u7684Guava\u5e93\u63d0\u4f9b\u4e86\u66f4\u591a\u5224\u7a7a\u7684\u5de5\u5177\u7c7b\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Preconditions<\/h3>\n<\/p>\n<p><p>Guava\u7684<code>Preconditions<\/code>\u5de5\u5177\u7c7b\u53ef\u4ee5\u7528\u4e8e\u65b9\u6cd5\u53c2\u6570\u68c0\u67e5\uff0c\u786e\u4fdd\u53c2\u6570\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public void doSomething(String param) {<\/p>\n<p>    Preconditions.checkNotNull(param, &quot;param\u4e0d\u80fd\u4e3anull&quot;);<\/p>\n<p>    \/\/ \u5176\u4ed6\u903b\u8f91<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528Optional<\/h3>\n<\/p>\n<p><p>Guava\u5305\u62ec\u4e86\u81ea\u5df1\u7684<code>Optional<\/code>\u7c7b\u5b9e\u73b0\uff0c\u4e0eJava 8\u7684<code>Optional<\/code>\u4e0d\u540c\uff0c\u5b83\u63d0\u4f9b\u4e86\u989d\u5916\u7684\u4fbf\u5229\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">Optional&lt;String&gt; optionalStr = Optional.fromNullable(string);<\/p>\n<p>if (optionalStr.isPresent()) {<\/p>\n<p>    \/\/ \u5f53\u5b57\u7b26\u4e32\u975e\u7a7a\u65f6\u6267\u884c\u64cd\u4f5c<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001Apache Commons Lang\u5e93<\/h2>\n<\/p>\n<p><p>Apache Commons Lang\u5e93\u63d0\u4f9b\u4e86<code>StringUtils<\/code>\u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u8bb8\u591a\u5904\u7406\u5b57\u7b26\u4e32\u7684\u9759\u6001\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5224\u7a7a\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u5224\u65ad\u7a7a\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p><code>StringUtils<\/code>\u63d0\u4f9b\u4e86<code>isEmpty()<\/code>\u548c<code>isNotEmpty()<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">if (StringUtils.isNotEmpty(str)) {<\/p>\n<p>    \/\/ \u975e\u7a7a\u64cd\u4f5c<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5224\u65ad\u7a7a\u767d\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u53e6\u5916\uff0c<code>StringUtils.isBlank()<\/code>\u548c<code>isNotBlank()<\/code>\u65b9\u6cd5\u53ef\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u4e0d\u662f\u7a7a\u767d\u5b57\u7b26\u4e32\uff0c\u5b83\u4eec\u5728\u5224\u65ad\u7a7a\u7684\u540c\u65f6\u4e5f\u4f1a\u68c0\u6d4b\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">if (StringUtils.isNotBlank(str)) {<\/p>\n<p>    \/\/ \u975e\u7a7a\u64cd\u4f5c<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>In conclusion, Java\u4e2d\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u5224\u7a7a\u64cd\u4f5c\u65b9\u6cd5\uff0c\u8fd8\u6709\u591a\u79cd\u7b2c\u4e09\u65b9\u5e93\u652f\u6301\u4f7f\u5224\u7a7a\u64cd\u4f5c\u66f4\u52a0\u4fbf\u6377\u548c\u53ef\u9760\u3002\u901a\u8fc7\u719f\u7ec3\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5<strong>\u6709\u6548\u907f\u514d\u7a7a\u6307\u9488\u5f02\u5e38\uff0c\u5e76\u5199\u51fa\u66f4\u52a0\u5065\u58ee\u4e14\u4f18\u96c5\u7684\u4ee3\u7801<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5728Java\u4e2d\uff0c\u5982\u4f55\u8fdb\u884c\u4f18\u96c5\u7684\u7a7a\u5224\u65ad\uff1f<\/strong><\/p>\n<p>\u7a7a\u5224\u65ad\u662fJava\u5f00\u53d1\u4e2d\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u4e3a\u4e86\u66f4\u52a0\u4f18\u96c5\u5730\u8fdb\u884c\u7a7a\u5224\u65ad\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528Objects\u7c7b\u7684isNull\u65b9\u6cd5\u8fdb\u884c\u7a7a\u5224\u65ad\uff1a<code>Objects.isNull(object)<\/code><\/li>\n<li>\u4f7f\u7528Optional\u7c7b\u8fdb\u884c\u7a7a\u5224\u65ad\uff1a<code>Optional.ofNullable(object).isPresent()<\/code><\/li>\n<li>\u4f7f\u7528StringUtils\u7c7b\u8fdb\u884c\u7a7a\u5224\u65ad\uff1a<code>StringUtils.isEmpty(object)<\/code><\/li>\n<li>\u4f7f\u7528Java 8 \u4e2d\u7684Optional\u7c7b\u548cLambda\u8868\u8fbe\u5f0f\u8fdb\u884c\u7a7a\u5224\u65ad\uff1a<code>Optional.ofNullable(object).ifPresent(obj -&gt; { \/\/ do something with obj })<\/code><\/li>\n<\/ul>\n<p><strong>2. \u5982\u4f55\u5728Java\u4e2d\u5904\u7406\u5bf9\u8c61\u4e3a\u7a7a\u7684\u60c5\u51b5\uff1f<\/strong><\/p>\n<p>\u5728Java\u4e2d\u5904\u7406\u5bf9\u8c61\u4e3a\u7a7a\u7684\u60c5\u51b5\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528if\u8bed\u53e5\u8fdb\u884c\u7a7a\u5224\u65ad\uff0c\u7136\u540e\u6839\u636e\u60c5\u51b5\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u4f7f\u7528try-catch\u8bed\u53e5\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684NullPointerException\u5f02\u5e38\u3002<\/li>\n<li>\u4f7f\u7528\u65ad\u8a00\uff08assert\uff09\u8fdb\u884c\u7a7a\u5224\u65ad\uff0c\u5e76\u5728\u5931\u8d25\u65f6\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002<\/li>\n<li>\u4f7f\u7528Java 8\u4e2d\u7684Optional\u7c7b\u8fdb\u884c\u7a7a\u5224\u65ad\u548c\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><strong>3. \u5728Java\u4e2d\uff0c\u5982\u4f55\u907f\u514dNullPointerException\u5f02\u5e38\uff1f<\/strong><\/p>\n<p>\u907f\u514dNullPointerException\u5f02\u5e38\u7684\u65b9\u6cd5\u6709\uff1a<\/p>\n<ul>\n<li>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5bf9\u4e8e\u4efb\u4f55\u53ef\u80fd\u4e3anull\u7684\u5bf9\u8c61\u8fdb\u884c\u7a7a\u5224\u65ad\uff0c\u5373\u4f7f\u662f\u5728\u8c03\u7528\u81ea\u5b9a\u4e49\u7684\u65b9\u6cd5\u65f6\u4e5f\u8981\u8fdb\u884c\u7a7a\u5224\u65ad\u3002<\/li>\n<li>\u4f7f\u7528Java 8\u4e2d\u7684Optional\u7c7b\u6765\u8fdb\u884c\u7a7a\u5224\u65ad\u548c\u5904\u7406\u3002<\/li>\n<li>\u4f7f\u7528\u65ad\u8a00\uff08assert\uff09\u8fdb\u884c\u7a7a\u5224\u65ad\uff0c\u5e76\u5728\u5931\u8d25\u65f6\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002<\/li>\n<li>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u7c7b\u8fdb\u884c\u7a7a\u5224\u65ad\uff0c\u4f8b\u5982Apache Commons Lang\u4e2d\u7684StringUtils\u7c7b\u3002<\/li>\n<li>\u5145\u5206\u4e86\u89e3\u9879\u76ee\u4e2d\u5404\u5904\u53ef\u80fd\u51fa\u73b0\u7a7a\u6307\u9488\u5f02\u5e38\u7684\u5730\u65b9\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u548c\u4f18\u5316\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"Java\u5224\u7a7a\u64cd\u4f5c\u9700\u8981\u6ce8\u610f\u591a\u79cd\u60c5\u5f62\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u5bf9\u8c61\u4e3anull\u3001\u5b57\u7b26\u4e32\u4e3a\u7a7a\u767d\u5b57\u7b26\u4e32\u3001\u96c6\u5408\u7c7b\u5bf9\u8c61\u4e3a\u7a7a\u6216\u65e0\u5143\u7d20\uff0c\u4ee5\u53ca [&hellip;]","protected":false},"author":3,"featured_media":291882,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/291880"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=291880"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/291880\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/291882"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=291880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=291880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=291880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}