{"id":3282,"date":"2018-10-26T18:00:12","date_gmt":"2018-10-26T10:00:12","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3282"},"modified":"2018-10-26T18:00:12","modified_gmt":"2018-10-26T10:00:12","slug":"%e8%bf%98%e4%b8%8d%e4%bc%9aoptional%ef%bc%9f%e7%be%8e%e5%85%b6%e5%90%8d%e6%9b%b0jdk8%e6%9c%89bug%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3282","title":{"rendered":"\u8fd8\u4e0d\u4f1aOptional\uff1f\u7f8e\u5176\u540d\u66f0JDK8\u6709bug\uff1f"},"content":{"rendered":"<p>\n\t\u7a0b\u5e8f\u733f\u8981\u4e0e\u65f6\u4ff1\u8fdb\uff01JDK11\u90fd\u51fa\u6765\u4e86\u4f60\u8fd8\u8981\u5b88\u7740jdk6\u548c7\uff1f\u5f53\u4f60\u8ba4\u4e3aJDK8\u6709BUG\u7684\u65f6\u5019\u4f60\u53ef\u66fe\u77e5\u9053\u5f53\u521dJDK6\u3001JDK7\u4e5f\u662f\u8fd9\u6837\u8fc7\u6765\u7684\uff01\n<\/p>\n<p>\n\t\u6700\u8fd1\u6211\u8981\u6279\u8bc4\u4e00\u4e9b\u4eba\u4e86\uff01\u5728\u6211\u9047\u5230\u7684\u4e00\u4e9b\u83dc\u9e1f\u5f53\u4e2d\uff0c\u95ee\u4ed6\u4f1a\u4e0d\u4f1aJDK8\u4e2d\u7684Optional\uff1f\u4ed6\u8bf4\u4e0d\u4f1a\uff01\u6211\u8bf4\u4f60\u4e3a\u4ec0\u4e48\u4e0d\u5b66\uff01\u4ed6\u8bf4\u4ee5\u540e\u518d\uff0cJDK8\u73b0\u5728\u7528\u7684\u4f01\u4e1a\u4e0d\u662f\u5f88\u591a\uff0c\u8fd8\u53ef\u80fd\u5b58\u5728\u4e00\u4e9b\u672a\u77e5\u7684BUG\uff01\n<\/p>\n<p>\n\t\u6211\u5f53\u573a\u5c31\u60f3\u8981\u9a82\u4eba\u4e86\uff0c\u4f60\u6d3b\u8be5\u505a\u83dc\u9e1f\uff01\n<\/p>\n<p>\n\t\u56de\u5f52\u4e3b\u9898\uff0c\u6211\u5e0c\u671b\u5173\u6ce8\u6211\u7684\u8fd8\u4e0d\u4f1a&nbsp;Optional \u7684\u7f51\u53cb\uff0c\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\u53ef\u4ee5\u6559\u80b2\u4f60\u7684\u540c\u4e8b\uff01\n<\/p>\n<p>\n\t\u6bcf\u4e2a\u63a5\u89e6 JAVA \u7684\u7a0b\u5e8f\u5458\u53ef\u80fd\u90fd\u9047\u5230\u8fc7 NPE(NullPointerException) \u95ee\u9898\u3002JDK8 \u4e3a\u4e86\u5e2e\u52a9\u6211\u4eec\u5f88\u597d\u7684\u907f\u514d\u4e86\u8fd9\u4e00\u73b0\u8c61\uff0c\u63a8\u51fa\u4e86 Optiona \u529f\u80fd\uff0c\u5b83\u662f\u4e00\u4e2a\u65b0\u7279\u6027\u3002\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5b83\u7684\u76f8\u5173\u7528\u6cd5\uff01\n<\/p>\n<h2>\n\tof\/empty\/ofNullable<br \/>\n<\/h2>\n<pre class=\"brush:java;\">\r\nXttblog xttblog = null;\/\/ xttblog \u662f\u4e00\u4e2a \u7a7a\u5bf9\u8c61\t\r\n\/**\r\n* \u6e90\u7801\uff1a        \r\n* public static &lt;T&gt; Optional&lt;T&gt; of(T value) {\/\/of\u4f1a\u521b\u5efa\u4e00\u4e2a \u5b9e\u4f8b\u5316\u4e00\u4e2a Optional\r\n*     return new Optional&lt;&gt;(value);\r\n* }\r\n* private Optional(T value) {\t\/\/\u68c0\u67e5value\u662f\u5426\u4e3anull\r\n*\t   this.value = Objects.requireNonNull(value);\r\n* }\r\n* public static &lt;T&gt; T requireNonNull(T obj) {\t\/\/\u4e0d\u505a\u89e3\u91ca\u4e86...\r\n*     if (obj == null)\r\n*         throw new NullPointerException();\r\n*     return obj;\r\n* }\r\n* \u89e3\u91ca\uff1a \u5982\u679c\u5bf9\u8c61\u4e3aNull\u4f7f\u7528of\u4f1a\u629b\u51fajava.lang.NullPointerException\r\n*\/\r\nOptional&lt;Xttblog&gt; op1 = Optional.of(xttblog);\r\nOptional&lt;Xttblog&gt; op2 = Optional.empty();\/\/\u770bAPI\u5c31\u77e5\u9053\u5565\u610f\u601d\u4e86,\u57fa\u672c\u6ca1\u4ec0\u4e48\u592a\u5927\u5375\u7528\r\n\/**\r\n* \u6e90\u7801\uff1a        \r\n* public static &lt;T&gt; Optional&lt;T&gt; ofNullable(T value) {\r\n*     return value == null ? empty() : of(value);\r\n* }\r\n* \u89e3\u91ca\uff1a \u5982\u679c\u5bf9\u8c61\u4e3a\u7a7a\u5c31\u8fd4\u56de empty() \u76f8\u53cd\u5c31  \u8c03\u7528 of(value); \r\n*\/\r\nOptional&lt;Xttblog&gt; op3 = Optional.ofNullable(xttblog);<\/pre>\n<h2>\n\tget\/isPresent<br \/>\n<\/h2>\n<pre class=\"brush:java;\">\r\nOptional&lt;Xttblog&gt; op = Optional.ofNullable(null);\/\/\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u4e3aNull\u7684Optional\r\n\/**\r\n* \u6e90\u7801\uff1a        \r\n* public boolean isPresent() {\r\n*     return value != null;\r\n* }\r\n* \u89e3\u91ca\uff1a \u56e0\u4e3a value == null \u6e90\u7801\u662f value != null \u56e0\u6b64\u8fd4\u56defalse\r\n*\/\r\n System.out.println(op.isPresent());\r\n\/**\r\n* \u6e90\u7801\uff1a        \r\n*  public T get() {\r\n*     if (value == null) {\r\n*         throw new NoSuchElementException(&quot;No value present&quot;);\r\n*     }\r\n*     return value;\r\n* }\r\n* \u89e3\u91ca\uff1a \u5982\u679c value \u4e3a\u7a7a null \u629b\u51fa NoSuchElementException \u5426\u5219\u8fd4\u56de T\r\n*\/\r\nSystem.out.println(op.get());<\/pre>\n<h2>\n\torElse\/orElseGet\/orElseThrow<br \/>\n<\/h2>\n<pre class=\"brush:java;\">\r\nXttblog xttblog = null;\r\n\/**\r\n* \u6e90\u7801\uff1a\r\n* public T orElseGet(Supplier&lt;? extends T&gt; other) {\r\n*      return value != null ? value : other.get();\r\n* }\r\n* \u89e3\u91ca\uff1a \u5bf9\u8c61\u4e3a\u7a7a\u8fd4\u56denull,\u5426\u5219\u8fd4\u56de\u5bf9\u8c61\r\n*\/\r\nXttblog s1 = Optional.ofNullable(xttblog).orElseGet(Xttblog::new);\r\nSystem.out.println(s1);\r\n\/**\r\n* \u6e90\u7801\uff1a\r\n* public T orElse(T other) {\r\n*      return value != null ? value : other;\r\n* }\r\n* \u89e3\u91ca\uff1a \u5982\u679c T \u4e3anull\u5219\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\r\n*\/\r\nXttblog s2 = (Xttblog) Optional.ofNullable(xttblog).orElse(new Xttblog());\r\nSystem.out.println(s2);\r\n\/**\r\n* \u6e90\u7801\uff1a\r\n* public &lt;X extends Throwable&gt; T orElseThrow(Supplier&lt;? extends X&gt; exceptionSupplier) throws X {\r\n*      if (value != null) {\r\n*          return value;\r\n*      } else {\r\n*          throw exceptionSupplier.get();\r\n*      }\r\n* }\r\n* \u89e3\u91ca\uff1a \u5982\u679cT\u4e3anull\u90a3\u4e48\u629b\u51fa\u5f02\u5e38\uff08\u672c\u5b9d\u5b9d\u975e\u5e38\u559c\u6b22\uff09\r\n*\/\r\nXttblog s3 = Optional.ofNullable(xttblog).orElseThrow(() -&gt; new RuntimeException(&quot;www.xttblog.com&quot;));\r\nSystem.out.println(s3);\/\/\u629b\u51fa\uff1ajava.lang.RuntimeException: www.xttblog.com<\/pre>\n<h2>\n\tfiter\/map<br \/>\n<\/h2>\n<pre class=\"brush:java;\">\r\n\/\/\u53ef\u4ee5\u52a0if\u5224\u65ad\u6761\u4ef6\u8fc7\u6ee4\r\nString filter = Optional.ofNullable(&quot;\u4e1a\u4f59\u8349&quot;).filter((value) -&gt; value.length() &gt; 5).get();\r\nSystem.out.println(filter);\r\n\/\/\u5927\u5c0f\u5199\u8f6c\u6362\u7b49\u7b49..\r\nString map = Optional.ofNullable(&quot;www.xttblog.com&quot;).map(String::toUpperCase).get();\r\nSystem.out.println(map);<\/pre>\n<h2>\n\tOptional\u8f6c\u96c6\u5408<br \/>\n<\/h2>\n<p>\n\tOptional\u662f\u4e00\u4e2a\u96c6\u5408\uff0c\u867d\u7136\u91cc\u9762\u53ea\u67090\u6216\u80051\u4e2a\u5143\u7d20\uff0c\u4f46\u5b83\u4e00\u6837\u662f\u4e00\u4e2a\u96c6\u5408\u3002\u5982\u679c\u8981\u8f6c\u4e3aList\u6216\u8005Set\uff0c\u4e00\u53ef\u4ee5\u6709\u5982\u4e0b\u51e0\u79cd\u505a\u6cd5\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic static &lt;T&gt; List&lt;T&gt; toList(Optional&lt;T&gt; option) {\r\n    return option.\r\n            map(Collections::singletonList).\r\n            orElse(Collections.emptyList());\r\n}\r\n\/\/\u6216\u8005\u66f4\u4f20\u7edf\u7684\u5199\u6cd5\uff1a\r\npublic static &lt;T&gt; List&lt;T&gt; toList(Optional&lt;T&gt; option) {\r\n    if (option.isPresent())\r\n        return Collections.singletonList(option.get());\r\n    else\r\n        return Collections.emptyList();\r\n}\r\n\/\/\u4f46\u662f\u5728java8\u91cc\uff0c\u5176\u5b9e\u53ea\u9700\u8981\u8fd9\u4e48\u5199\uff1a\r\nimport static java.util.stream.Collectors.*;\r\n\/\/\u8f6c\u4e3aList\r\nList&lt;String&gt; list = collect(opt, toList());\r\n\/\/\u8f6c\u4e3aSet\r\nSet&lt;String&gt;  set  = collect(opt, toSet());<\/pre>\n<p>\n\t\u5176\u5b9e\u5c31\u8fd9\u4e48\u7b80\u5355\uff0c\u4f60\u5e72\u561b\u8981\u7ed9\u81ea\u5df1\u627e\u501f\u53e3\u5462\uff1f\u4f60\u8bf4\u662f\u4e0d\u662f\uff01\u591a\u5b66\u70b9\u77e5\u8bc6\u6ca1\u5bb3\u5904\uff01<\/p>\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=3282\">\u8fd8\u4e0d\u4f1aOptional\uff1f\u7f8e\u5176\u540d\u66f0JDK8\u6709bug\uff1f<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u7a0b\u5e8f\u733f\u8981\u4e0e\u65f6\u4ff1\u8fdb\uff01JDK11\u90fd\u51fa\u6765\u4e86\u4f60\u8fd8\u8981\u5b88\u7740jdk6\u548c7\uff1f\u5f53\u4f60\u8ba4\u4e3aJDK8\u6709BUG\u7684\u65f6\u5019\u4f60\u53ef\u66fe\u77e5\u9053\u5f53\u521dJDK [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[615,1334,267,1471,288],"class_list":["post-3282","post","type-post","status-publish","format-standard","hentry","category-yyzt","tag-jdk1-7","tag-jdk8","tag-nullpointerexception","tag-optional","tag-bug"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3282","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=3282"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3282\/revisions"}],"predecessor-version":[{"id":3283,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3282\/revisions\/3283"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}