{"id":3014,"date":"2018-08-28T11:20:58","date_gmt":"2018-08-28T03:20:58","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3014"},"modified":"2018-08-28T11:21:36","modified_gmt":"2018-08-28T03:21:36","slug":"java-util-optional-%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3014","title":{"rendered":"java.util.Optional \u8be6\u89e3"},"content":{"rendered":"<p>\n\tjava.util.Optional&lt;T&gt; \u662f java 8 \u4e2d\u63a8\u51fa\u6765\u7684\u4e00\u4e2a\u65b0\u7c7b\uff0c\u4e3b\u8981\u7528\u6765\u89e3\u51b3\u7a7a\u5f02\u5e38 NullPointerException \u7684\u95ee\u9898\uff0c\u4f46\u5b83\u4e5f\u4e0d\u7b97\u771f\u6b63\u7684\u89e3\u51b3\uff0c\u8fd8\u662f\u53ef\u4ee5\u8fd4\u56de null \u548c \u629b\u51fa\u7a7a\u5f02\u5e38\u7684\u3002\u516c\u53f8\u5f88\u591a\u65b0\u4eba\u90fd\u4f1a\u7528\uff0c\u4f46\u4ed6\u4eec\u90fd\u65e0\u6cd5\u8bf4\u660e\u8fd9\u4e2a\u7c7b\u5230\u5e95\u662f\u5e72\u4ec0\u4e48\u7684\uff1f\u4e8e\u662f\u5c31\u6709\u4e86\u672c\u6587\uff0c\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u65b9\u4fbf\u5927\u5bb6\u7406\u89e3\uff01\n<\/p>\n<p>\n\t\u8bf4\u8d77\u7a7a\u5f02\u5e38 NullPointerException \u8fd9\u6ce8\u5b9a\u53c8\u662f\u4e00\u4e2a\u4e00\u8bf4\u5c31\u660e\u767d\uff0c\u4e00\u8bb2\u5c31\u7cca\u6d82\uff0c\u4e00\u8ba8\u8bba\u5c31\u7ffb\u5929\u7684\u95ee\u9898\u3002\u6240\u4ee5\u6211\u65e9\u5c31\u60f3\u6574\u7406\u8fd9\u7bc7\u6587\u7ae0\u4e86\u3002\n<\/p>\n<h2>\n\tOptional \u7c7b<br \/>\n<\/h2>\n<p>\n\tOptional \u7c7b\u662f\u4e00\u4e2a\u53ef\u4ee5\u4e3a null \u7684\u5bb9\u5668\u5bf9\u8c61\u3002\u5b83\u662f\u4e2a\u5bb9\u5668\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u4fdd\u5b58\u7c7b\u578bT\u7684\u503c\uff0c\u6216\u8005\u4ec5\u4ec5\u4fdd\u5b58 null\u3002Optional \u7684\u8bde\u751f\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u4e0d\u7528\u663e\u5f0f\u8fdb\u884c\u7a7a\u503c\u68c0\u6d4b\u3002\n<\/p>\n<p>\n\tOptional \u662f Java \u5b9e\u73b0\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5f3a\u52b2\u4e00\u6b65\uff0c\u5e76\u4e14\u5e2e\u52a9\u5728\u8303\u5f0f\u4e2d\u5b9e\u73b0\u3002\u4f46\u662f Optional \u7684\u610f\u4e49\u663e\u7136\u4e0d\u6b62\u4e8e\u6b64\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npublic final class Optional&lt;T&gt; {}<\/pre>\n<h2>\n\tOptional \u7684\u65b9\u6cd5<br \/>\n<\/h2>\n<p>\n\tJava 8 \u4e2d Optional \u7c7b\u7684\u4e3b\u8981\u65b9\u6cd5\u5927\u6982\u6709 15 \u4e2a\uff0c\u5728 java 9 \u4e2d\uff0c\u53c8\u5f15\u5165\u4e86 3 \u4e2a\u65b0\u65b9\u6cd5\u3002java 8 \u4e2d\u7684\u65b9\u6cd5\uff0c\u5177\u4f53\u5982\u4e0b\uff1a\n<\/p>\n<table>\n<tbody>\n<tr>\n<th>\n\t\t\t\t\u5e8f\u53f7\n\t\t\t<\/th>\n<th>\n\t\t\t\t\u65b9\u6cd5 &amp; \u63cf\u8ff0\n\t\t\t<\/th>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t1\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>static &lt;T&gt; Optional&lt;T&gt; empty()<\/b><\/p>\n<p>\n\t\t\t\t\t\u8fd4\u56de\u7a7a\u7684 Optional \u5b9e\u4f8b\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t2\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>boolean equals(Object obj)<\/b><\/p>\n<p>\n\t\t\t\t\t\u5224\u65ad\u5176\u4ed6\u5bf9\u8c61\u662f\u5426\u7b49\u4e8e Optional\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t3\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>Optional&lt;T&gt; filter(Predicate&lt;? super &lt;T&gt; predicate)<\/b><\/p>\n<p>\n\t\t\t\t\t\u5982\u679c\u503c\u5b58\u5728\uff0c\u5e76\u4e14\u8fd9\u4e2a\u503c\u5339\u914d\u7ed9\u5b9a\u7684 predicate\uff0c\u8fd4\u56de\u4e00\u4e2aOptional\u7528\u4ee5\u63cf\u8ff0\u8fd9\u4e2a\u503c\uff0c\u5426\u5219\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684Optional\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t4\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>&lt;U&gt; Optional&lt;U&gt; flatMap(Function&lt;? super T,Optional&lt;U&gt;&gt; mapper)<\/b><\/p>\n<p>\n\t\t\t\t\t\u5982\u679c\u503c\u5b58\u5728\uff0c\u8fd4\u56de\u57fa\u4e8eOptional\u5305\u542b\u7684\u6620\u5c04\u65b9\u6cd5\u7684\u503c\uff0c\u5426\u5219\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684Optional\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t5\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>T get()<\/b><\/p>\n<p>\n\t\t\t\t\t\u5982\u679c\u5728\u8fd9\u4e2aOptional\u4e2d\u5305\u542b\u8fd9\u4e2a\u503c\uff0c\u8fd4\u56de\u503c\uff0c\u5426\u5219\u629b\u51fa\u5f02\u5e38\uff1aNoSuchElementException\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t6\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>int hashCode()<\/b><\/p>\n<p>\n\t\t\t\t\t\u8fd4\u56de\u5b58\u5728\u503c\u7684\u54c8\u5e0c\u7801\uff0c\u5982\u679c\u503c\u4e0d\u5b58\u5728 \u8fd4\u56de 0\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t7\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>void ifPresent(Consumer&lt;? super T&gt; consumer)<\/b><\/p>\n<p>\n\t\t\t\t\t\u5982\u679c\u503c\u5b58\u5728\u5219\u4f7f\u7528\u8be5\u503c\u8c03\u7528 consumer , \u5426\u5219\u4e0d\u505a\u4efb\u4f55\u4e8b\u60c5\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t8\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>boolean isPresent()<\/b><\/p>\n<p>\n\t\t\t\t\t\u5982\u679c\u503c\u5b58\u5728\u5219\u65b9\u6cd5\u4f1a\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56de false\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t9\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>&lt;U&gt;Optional&lt;U&gt; map(Function&lt;? super T,? extends U&gt; mapper)<\/b><\/p>\n<p>\n\t\t\t\t\t\u5982\u679c\u5b58\u5728\u8be5\u503c\uff0c\u63d0\u4f9b\u7684\u6620\u5c04\u65b9\u6cd5\uff0c\u5982\u679c\u8fd4\u56de\u975enull\uff0c\u8fd4\u56de\u4e00\u4e2aOptional\u63cf\u8ff0\u7ed3\u679c\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t10\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>static &lt;T&gt; Optional&lt;T&gt; of(T value)<\/b><\/p>\n<p>\n\t\t\t\t\t\u8fd4\u56de\u4e00\u4e2a\u6307\u5b9a\u975enull\u503c\u7684Optional\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t11\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>static &lt;T&gt; Optional&lt;T&gt; ofNullable(T value)<\/b><\/p>\n<p>\n\t\t\t\t\t\u5982\u679c\u4e3a\u975e\u7a7a\uff0c\u8fd4\u56de Optional \u63cf\u8ff0\u7684\u6307\u5b9a\u503c\uff0c\u5426\u5219\u8fd4\u56de\u7a7a\u7684 Optional\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t12\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>T orElse(T other)<\/b><\/p>\n<p>\n\t\t\t\t\t\u5982\u679c\u5b58\u5728\u8be5\u503c\uff0c\u8fd4\u56de\u503c\uff0c \u5426\u5219\u8fd4\u56de other\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t13\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>T orElseGet(Supplier&lt;? extends T&gt; other)<\/b><\/p>\n<p>\n\t\t\t\t\t\u5982\u679c\u5b58\u5728\u8be5\u503c\uff0c\u8fd4\u56de\u503c\uff0c \u5426\u5219\u89e6\u53d1 other\uff0c\u5e76\u8fd4\u56de other \u8c03\u7528\u7684\u7ed3\u679c\u3002\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t14\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>&lt;X extends Throwable&gt; T orElseThrow(Supplier&lt;? extends X&gt; exceptionSupplier)<\/b><\/p>\n<p>\n\t\t\t\t\t\u5982\u679c\u5b58\u5728\u8be5\u503c\uff0c\u8fd4\u56de\u5305\u542b\u7684\u503c\uff0c\u5426\u5219\u629b\u51fa\u7531 Supplier \u7ee7\u627f\u7684\u5f02\u5e38\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\t15\n\t\t\t<\/td>\n<td>\n\t\t\t\t<b>String toString()<\/b><\/p>\n<p>\n\t\t\t\t\t\u8fd4\u56de\u4e00\u4e2aOptional\u7684\u975e\u7a7a\u5b57\u7b26\u4e32\uff0c\u7528\u6765\u8c03\u8bd5\n\t\t\t\t<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\n\t\u5728\u4ee5\u524d\uff0c\u6211\u4eec\u5224\u65ad\u4e00\u4e2a\u975e\u7a7a\u7684\u503c\uff0c\u5c42\u7ea7\u591a\u7684\u591a\uff0c\u4ee3\u7801\u5c31\u975e\u5e38\u7684\u6c89\u4f59\uff0c\u96be\u4ee5\u7ef4\u62a4\u3002\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nif(null != xttblog){\r\n\tif(null != xttblog.getName()){\r\n\t\tSystem.out.println(&quot;\u4e1a\u4f59\u8349&quot;);\r\n\t}\r\n}<\/pre>\n<p>\n\t\u5982\u679c\u6211\u4eec\u4f7f\u7528 Optional \u7c7b\uff0c\u5c31\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nOptional.of(xttblog).map(Xttblog::getName).ifPresent(System.out::println);<\/pre>\n<p>\n\tOptional \u7c7b\u7528\u5230\u4e86\u5de5\u5382\u65b9\u6cd5\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u6bd4\u5982 Optional.of(null) \u548c Optional.ofNullable(null)\u3002\n<\/p>\n<p>\n\t\u66f4\u591a\u7684\u7528\u6cd5\u5efa\u8bae\u5927\u5bb6\u591a\u53bb\u5b9e\u8df5\uff01\n<\/p>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/www.cnblogs.com\/xingzc\/p\/5778090.html\" name=\"Java 8 Optional\u7c7b\u6df1\u5ea6\u89e3\u6790\" target=\"_blank\" title=\"Java 8 Optional\u7c7b\u6df1\u5ea6\u89e3\u6790\">Java 8 Optional\u7c7b\u6df1\u5ea6\u89e3\u6790<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/www.oschina.net\/translate\/understanding-accepting-and-leveraging-optional-in\" name=\"\u7406\u89e3\u3001\u5b66\u4e60\u4e0e\u4f7f\u7528 Java \u4e2d\u7684 Optional\" target=\"_blank\" title=\"\u7406\u89e3\u3001\u5b66\u4e60\u4e0e\u4f7f\u7528 Java \u4e2d\u7684 Optional\">\u7406\u89e3\u3001\u5b66\u4e60\u4e0e\u4f7f\u7528 Java \u4e2d\u7684 Optional<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/www.runoob.com\/java\/java8-optional-class.html\" name=\"Java 8 Optional \u7c7b\" target=\"_blank\" title=\"Java 8 Optional \u7c7b\">Java 8 Optional \u7c7b<\/a>\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3014\">java.util.Optional \u8be6\u89e3<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>java.util.Optional&lt;T&gt; \u662f java 8 \u4e2d\u63a8\u51fa\u6765\u7684\u4e00\u4e2a\u65b0\u7c7b\uff0c\u4e3b\u8981\u7528\u6765\u89e3\u51b3\u7a7a\u5f02 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[618,261,266,1471,1621],"class_list":["post-3014","post","type-post","status-publish","format-standard","hentry","category-java","tag-java8","tag-java","tag-optional","tag-stream"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3014"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3014\/revisions"}],"predecessor-version":[{"id":3015,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3014\/revisions\/3015"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}