{"id":670,"date":"2019-05-19T11:55:02","date_gmt":"2019-05-19T03:55:02","guid":{"rendered":"http:\/\/prayerlaputa.com\/?p=670"},"modified":"2019-09-19T11:55:46","modified_gmt":"2019-09-19T03:55:46","slug":"google-guava%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b%e7%b3%bb%e5%88%97%ef%bc%882%ef%bc%89-%e4%bd%bf%e7%94%a8%e4%b8%8e%e9%81%bf%e5%85%8d%e4%bd%bf%e7%94%a8null","status":"publish","type":"post","link":"http:\/\/prayerlaputa.com\/?p=670","title":{"rendered":"google guava\u4f7f\u7528\u6559\u7a0b\u7cfb\u5217\uff082\uff09-\u4f7f\u7528\u4e0e\u907f\u514d\u4f7f\u7528null"},"content":{"rendered":"<h1>\u6709\u5173null\u7684\u4f7f\u7528<\/h1>\n<div class=\"mume markdown-preview \">\n<p>\u4e0d\u8981\u5728Set\u4e2d\u4f7f\u7528null\uff0c\u6216\u8005\u628anull\u4f5c\u4e3amap\u7684\u952e\u503c\u3002\u4f7f\u7528\u7279\u6b8a\u503c\u4ee3\u8868null\u4f1a\u8ba9\u67e5\u627e\u64cd\u4f5c\u7684\u8bed\u4e49\u66f4\u6e05\u6670\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u628anull\u4f5c\u4e3amap\u4e2d\u67d0\u6761\u76ee\u7684\u503c\uff0c\u66f4\u597d\u7684\u529e\u6cd5\u662f \u4e0d\u628a\u8fd9\u4e00\u6761\u76ee\u653e\u5230map\u4e2d\uff0c\u800c\u662f\u5355\u72ec\u7ef4\u62a4\u4e00\u4e2a\u201d\u503c\u4e3anull\u7684\u952e\u96c6\u5408\u201d (null keys)\u3002Map \u4e2d\u5bf9\u5e94\u67d0\u4e2a\u952e\u7684\u503c\u662fnull\uff0c\u548cmap\u4e2d\u6ca1\u6709\u5bf9\u5e94\u67d0\u4e2a\u952e\u7684\u503c\uff0c\u662f\u975e\u5e38\u5bb9\u6613\u6df7\u6dc6\u7684\u4e24\u79cd\u60c5\u51b5\u3002\u56e0\u6b64\uff0c\u6700\u597d\u628a\u503c\u4e3anull\u7684\u952e\u5206\u79bb\u5f00\uff0c\u5e76\u4e14\u4ed4\u7ec6\u60f3\u60f3\uff0cnull\u503c\u7684\u952e\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u5230\u5e95\u8868\u8fbe\u4e86\u4ec0\u4e48\u8bed\u4e49\u3002<\/p>\n<p>\u5982\u679c\u4f60\u9700\u8981\u5728\u5217\u8868\u4e2d\u4f7f\u7528null\uff0c\u5e76\u4e14\u8fd9\u4e2a\u5217\u8868\u7684\u6570\u636e\u662f\u7a00\u758f\u7684\uff0c\u4f7f\u7528Map&lt;Integer, E&gt;\u53ef\u80fd\u4f1a\u66f4\u9ad8\u6548\uff0c\u5e76\u4e14\u66f4\u51c6\u786e\u5730\u7b26\u5408\u4f60\u7684\u6f5c\u5728\u9700\u6c42\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u8003\u8651\u4e00\u4e0b\u4f7f\u7528\u81ea\u7136\u7684null\u5bf9\u8c61\u2014\u2014\u7279\u6b8a\u503c\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4e3a\u67d0\u4e2aenum\u7c7b\u578b\u589e\u52a0\u7279\u6b8a\u7684\u679a\u4e3e\u503c\u8868\u793anull\uff0c\u6bd4\u5982java.math.RoundingMode\u5c31\u5b9a\u4e49\u4e86\u4e00\u4e2a\u679a\u4e3e\u503cUNNECESSARY\uff0c\u5b83\u8868\u793a\u4e00\u79cd\u4e0d\u505a\u4efb\u4f55\u820d\u5165\u64cd\u4f5c\u7684\u6a21\u5f0f\uff0c\u7528\u8fd9\u79cd\u6a21\u5f0f\u505a\u820d\u5165\u64cd\u4f5c\u4f1a\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p>\u5982\u679c\u4f60\u771f\u7684\u9700\u8981\u4f7f\u7528null\u503c\uff0c\u4f46\u662fnull\u503c\u4e0d\u80fd\u548cGuava\u4e2d\u7684\u96c6\u5408\u5b9e\u73b0\u4e00\u8d77\u5de5\u4f5c\uff0c\u4f60\u53ea\u80fd\u9009\u62e9\u5176\u4ed6\u5b9e\u73b0\u3002\u6bd4\u5982\uff0c\u7528JDK\u4e2d\u7684Collections.unmodifiableList\u66ff\u4ee3Guava\u7684ImmutableList<\/p>\n<h1 id=\"optional%E7%9A%84%E4%BD%BF%E7%94%A8\" class=\"mume-header\">Optional\u7684\u4f7f\u7528<\/h1>\n<p>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5f00\u53d1\u4eba\u5458\u4f7f\u7528null\u8868\u660e\u7684\u662f\u67d0\u79cd\u7f3a\u5931\u60c5\u5f62\uff1a\u53ef\u80fd\u662f\u5df2\u7ecf\u6709\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u6216\u6ca1\u6709\u503c\uff0c\u6216\u627e\u4e0d\u5230\u503c\u3002\u4f8b\u5982\uff0cMap.get\u8fd4\u56denull\u5c31\u8868\u793a\u627e\u4e0d\u5230\u7ed9\u5b9a\u952e\u5bf9\u5e94\u7684\u503c\u3002<br \/>\n<!--more--><br \/>\nGuava\u7528Optional\u8868\u793a\u53ef\u80fd\u4e3anull\u7684T\u7c7b\u578b\u5f15\u7528\u3002\u4e00\u4e2aOptional\u5b9e\u4f8b\u53ef\u80fd\u5305\u542b\u975enull\u7684\u5f15\u7528\uff08<code>isPresent<\/code>\uff09\uff0c\u4e5f\u53ef\u80fd\u4ec0\u4e48\u4e5f\u4e0d\u5305\u62ec\uff08<code>absent<\/code>\uff09\u3002\u5b83\u4ece\u4e0d\u8bf4\u5305\u542bnull\u503c\uff0c\u800c\u662f\u7528\u5b58\u5728\u6216\u7f3a\u5931\u6765\u8868\u793a\u3002<\/p>\n<pre class=\"language-\" data-role=\"codeBlock\" data-info=\"\"><code>Optional&lt;Integer&gt; possible = Optional.of(5);\r\npossible.isPresent(); \/\/ returns true\r\npossible.get(); \/\/ returns 5\r\n<\/code><\/pre>\n<p>Optional\u65e0\u610f\u76f4\u63a5\u6a21\u62df\u5176\u4ed6\u7f16\u7a0b\u73af\u5883\u4e2d\u7684\u201d\u53ef\u9009\u201d or \u201c\u53ef\u80fd\u201d\u8bed\u4e49\uff0c\u4f46\u5b83\u4eec\u7684\u786e\u6709\u76f8\u4f3c\u4e4b\u5904\u3002<\/p>\n<h2 id=\"%E5%88%9B%E5%BB%BAoptional%E5%AE%9E%E4%BE%8B%E4%BB%A5%E4%B8%8B%E9%83%BD%E6%98%AF%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95\" class=\"mume-header\">\u521b\u5efaOptional\u5b9e\u4f8b\uff08\u4ee5\u4e0b\u90fd\u662f\u9759\u6001\u65b9\u6cd5\uff09<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u65b9\u6cd5<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Optional.of(T)<\/td>\n<td>\u521b\u5efa\u6307\u5b9a\u5f15\u7528\u7684Optional\u5b9e\u4f8b\uff0c\u82e5\u5f15\u7528\u4e3anull\u5219\u5feb\u901f\u5931\u8d25<\/td>\n<\/tr>\n<tr>\n<td>Optional.absent()<\/td>\n<td>\u521b\u5efa\u5f15\u7528\u7f3a\u5931\u7684Optional\u5b9e\u4f8b<\/td>\n<\/tr>\n<tr>\n<td>Optional.fromNullable(T)<\/td>\n<td>\u521b\u5efa\u6307\u5b9a\u5f15\u7528\u7684Optional\u5b9e\u4f8b\uff0c\u82e5\u5f15\u7528\u4e3anull\u5219\u8868\u793a\u7f3a\u5931<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"%E7%94%A8optional%E5%AE%9E%E4%BE%8B%E6%9F%A5%E8%AF%A2%E4%BB%A5%E4%B8%8B%E9%83%BD%E6%98%AF%E9%9D%9E%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95\" class=\"mume-header\">\u7528Optional\u5b9e\u4f8b\u67e5\u8be2\uff08\u4ee5\u4e0b\u90fd\u662f\u975e\u9759\u6001\u65b9\u6cd5\uff09<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u65b9\u6cd5<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>boolean isPresent()<\/td>\n<td>\u5982\u679cOptional\u5305\u542b\u975enull\u7684\u5f15\u7528\uff08\u5f15\u7528\u5b58\u5728\uff09\uff0c\u8fd4\u56detrue<\/td>\n<\/tr>\n<tr>\n<td>T get()<\/td>\n<td>\u8fd4\u56deOptional\u6240\u5305\u542b\u7684\u5f15\u7528\uff0c\u82e5\u5f15\u7528\u7f3a\u5931\uff0c\u5219\u629b\u51fajava.lang.IllegalStateException<\/td>\n<\/tr>\n<tr>\n<td>T or(T)<\/td>\n<td>\u8fd4\u56deOptional\u6240\u5305\u542b\u7684\u5f15\u7528\uff0c\u82e5\u5f15\u7528\u7f3a\u5931\uff0c\u8fd4\u56de\u6307\u5b9a\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>T orNull()<\/td>\n<td>\u8fd4\u56deOptional\u6240\u5305\u542b\u7684\u5f15\u7528\uff0c\u82e5\u5f15\u7528\u7f3a\u5931\uff0c\u8fd4\u56denull<\/td>\n<\/tr>\n<tr>\n<td>Set<br \/>\nasSet()<\/td>\n<td>\u8fd4\u56deOptional\u6240\u5305\u542b\u5f15\u7528\u7684\u5355\u4f8b\u4e0d\u53ef\u53d8\u96c6\uff0c\u5982\u679c\u5f15\u7528\u5b58\u5728\uff0c\u8fd4\u56de\u4e00\u4e2a\u53ea\u6709\u5355\u4e00\u5143\u7d20\u7684\u96c6\u5408\uff0c\u5982\u679c\u5f15\u7528\u7f3a\u5931\uff0c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u96c6\u5408\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"%E4%BD%BF%E7%94%A8optional%E7%9A%84%E6%84%8F%E4%B9%89\" class=\"mume-header\">\u4f7f\u7528Optional\u7684\u610f\u4e49<\/h2>\n<p>\u4f7f\u7528Optional\u9664\u4e86\u8d4b\u4e88null\u8bed\u4e49\uff0c\u589e\u52a0\u4e86\u53ef\u8bfb\u6027\uff0c\u6700\u5927\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u662f\u4e00\u79cd\u50bb\u74dc\u5f0f\u7684\u9632\u62a4\u3002Optional\u8feb\u4f7f\u4f60\u79ef\u6781\u601d\u8003\u5f15\u7528\u7f3a\u5931\u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u4f60\u5fc5\u987b\u663e\u5f0f\u5730\u4eceOptional\u83b7\u53d6\u5f15\u7528\u3002\u76f4\u63a5\u4f7f\u7528null\u5f88\u5bb9\u6613\u8ba9\u4eba\u5fd8\u6389\u67d0\u4e9b\u60c5\u5f62\uff0c\u5c3d\u7ba1FindBugs\u53ef\u4ee5\u5e2e\u52a9\u67e5\u627enull\u76f8\u5173\u7684\u95ee\u9898\uff0c\u4f46\u662f\u6211\u4eec\u8fd8\u662f\u8ba4\u4e3a\u5b83\u5e76\u4e0d\u80fd\u51c6\u786e\u5730\u5b9a\u4f4d\u95ee\u9898\u6839\u6e90\u3002<\/p>\n<p>\u5982\u540c\u8f93\u5165\u53c2\u6570\uff0c\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u4e5f\u53ef\u80fd\u662fnull\u3002\u548c\u5176\u4ed6\u4eba\u4e00\u6837\uff0c\u4f60\u7edd\u5bf9\u5f88\u53ef\u80fd\u4f1a\u5fd8\u8bb0\u522b\u4eba\u5199\u7684\u65b9\u6cd5method(a,b)\u4f1a\u8fd4\u56de\u4e00\u4e2anull\uff0c\u5c31\u597d\u50cf\u5f53\u4f60\u5b9e\u73b0method(a,b)\u65f6\uff0c\u4e5f\u5f88\u53ef\u80fd\u5fd8\u8bb0\u8f93\u5165\u53c2\u6570a\u53ef\u4ee5\u4e3anull\u3002\u5c06\u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b\u6307\u5b9a\u4e3aOptional\uff0c\u4e5f\u53ef\u4ee5\u8feb\u4f7f\u8c03\u7528\u8005\u601d\u8003\u8fd4\u56de\u7684\u5f15\u7528\u7f3a\u5931\u7684\u60c5\u5f62\u3002<\/p>\n<h2 id=\"%E5%A4%84%E7%90%86null%E7%9A%84%E7%AE%80%E4%BE%BF%E6%96%B9%E6%B3%95\" class=\"mume-header\">\u5904\u7406null\u7684\u7b80\u4fbf\u65b9\u6cd5<\/h2>\n<p>\u5f53\u4f60\u9700\u8981\u7528\u4e00\u4e2a\u9ed8\u8ba4\u503c\u6765\u66ff\u6362\u53ef\u80fd\u7684null\uff0c\u8bf7\u4f7f\u7528Objects.firstNonNull(T, T) \u65b9\u6cd5\u3002\u5982\u679c\u4e24\u4e2a\u503c\u90fd\u662fnull\uff0c\u8be5\u65b9\u6cd5\u4f1a\u629b\u51faNullPointerException\u3002Optional\u4e5f\u662f\u4e00\u4e2a\u6bd4\u8f83\u597d\u7684\u66ff\u4ee3\u65b9\u6848\uff0c\u4f8b\u5982\uff1aOptional.of(first).or(second).<\/p>\n<p>\u8fd8\u6709\u5176\u5b83\u4e00\u4e9b\u65b9\u6cd5\u4e13\u95e8\u5904\u7406null\u6216\u7a7a\u5b57\u7b26\u4e32\uff1aemptyToNull(String)\uff0cnullToEmpty(String)\uff0cisNullOrEmpty(String)\u3002\u6211\u4eec\u60f3\u8981\u5f3a\u8c03\u7684\u662f\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4e3b\u8981\u7528\u6765\u4e0e\u6df7\u6dc6null\/\u7a7a\u7684API\u8fdb\u884c\u4ea4\u4e92\u3002\u5f53\u6bcf\u6b21\u4f60\u5199\u4e0b\u6df7\u6dc6null\/\u7a7a\u7684\u4ee3\u7801\u65f6\uff0cGuava\u56e2\u961f\u90fd\u6cea\u6d41\u6ee1\u9762\u3002\uff08\u597d\u7684\u505a\u6cd5\u662f\u79ef\u6781\u5730\u628anull\u548c\u7a7a\u533a\u5206\u5f00\uff0c\u4ee5\u8868\u793a\u4e0d\u540c\u7684\u542b\u4e49\uff0c\u5728\u4ee3\u7801\u4e2d\u628anull\u548c\u7a7a\u540c\u7b49\u5bf9\u5f85\u662f\u4e00\u79cd\u4ee4\u4eba\u4e0d\u5b89\u7684\u574f\u5473\u9053\u3002<\/p>\n<h1 id=\"%E5%8F%82%E8%80%83%E6%96%87%E7%AB%A0\" class=\"mume-header\">\u53c2\u8003\u6587\u7ae0<\/h1>\n<ul>\n<li><a href=\"http:\/\/ifeve.com\/google-guava-using-and-avoiding-null\/\">[Google Guava] 1.1-\u4f7f\u7528\u548c\u907f\u514dnull<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u5173null\u7684\u4f7f\u7528 \u4e0d\u8981\u5728Set\u4e2d\u4f7f\u7528null\uff0c\u6216\u8005\u628anull\u4f5c\u4e3amap\u7684\u952e\u503c\u3002\u4f7f\u7528\u7279\u6b8a\u503c\u4ee3\u8868null\u4f1a\u8ba9\u67e5\u627e <a href='http:\/\/prayerlaputa.com\/?p=670' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[83,6,14],"tags":[84,33,85],"_links":{"self":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/670"}],"collection":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=670"}],"version-history":[{"count":1,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/670\/revisions"}],"predecessor-version":[{"id":671,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/670\/revisions\/671"}],"wp:attachment":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=670"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}