{"id":10006823,"date":"2022-11-07T17:01:31","date_gmt":"2022-11-07T09:01:31","guid":{"rendered":"https:\/\/ityet.com\/?p=10006823"},"modified":"2022-11-07T17:01:31","modified_gmt":"2022-11-07T09:01:31","slug":"%e4%ba%ac%e4%b8%9c%e4%ba%91%e5%bc%80%e5%8f%91%e8%80%85%ef%bd%9c%e6%b7%b1%e5%85%a5jdk%e4%b8%ad%e7%9a%84optional","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006823","title":{"rendered":"\u4eac\u4e1c\u4e91\u5f00\u53d1\u8005\uff5c\u6df1\u5165JDK\u4e2d\u7684Optional"},"content":{"rendered":"<p><strong>\u6982\u8ff0<\/strong>\uff1aOptional\u6700\u65e9\u662fGoogle\u516c\u53f8Guava\u4e2d\u7684\u6982\u5ff5\uff0c\u4ee3\u8868\u7684\u662f\u53ef\u9009\u503c\u3002Optional\u7c7b\u4eceJava8\u7248\u672c\u5f00\u59cb\u52a0\u5165\u8c6a\u534e\u5957\u9910\uff0c\u4e3b\u8981\u4e3a\u4e86\u89e3\u51b3\u7a0b\u5e8f\u4e2d\u7684NPE\u95ee\u9898\uff0c\u4ece\u800c\u4f7f\u5f97\u66f4\u5c11\u7684\u663e\u5f0f\u5224\u7a7a\uff0c\u9632\u6b62\u4ee3\u7801\u6c61\u67d3\uff0c\u53e6\u4e00\u65b9\u9762\uff0c\u4e5f\u4f7f\u5f97\u9886\u57df\u6a21\u578b\u4e2d\u6240\u9690\u85cf\u7684\u77e5\u8bc6\uff0c\u5f97\u4ee5\u663e\u5f0f\u4f53\u73b0\u5728\u4ee3\u7801\u4e2d\u3002Optional\u7c7b\u4f4d\u4e8ejava.util\u5305\u4e0b\uff0c\u5bf9\u94fe\u5f0f\u7f16\u7a0b\u98ce\u683c\u6709\u4e00\u5b9a\u7684\u652f\u6301\u3002\u5b9e\u9645\u4e0a\uff0cOptional\u66f4\u50cf\u662f\u4e00\u4e2a\u5bb9\u5668\uff0c\u5176\u4e2d\u5b58\u653e\u7684\u6210\u5458\u53d8\u91cf\u662f\u4e00\u4e2aT\u7c7b\u578b\u7684value\uff0c\u53ef\u503c\u53efNull\uff0c\u4f7f\u7528\u7684\u662fWrapper\u6a21\u5f0f\uff0c\u5bf9value\u64cd\u4f5c\u8fdb\u884c\u4e86\u5305\u88c5\u4e0e\u8bbe\u8ba1\u3002\u672c\u6587\u5c06\u4eceOptional\u6240\u89e3\u51b3\u7684\u95ee\u9898\u5f00\u59cb\uff0c\u9010\u5c42\u89e3\u5256\uff0c\u7531\u6d45\u5165\u6df1\uff0c\u6587\u4e2d\u4f1a\u51fa\u73b0Optioanl\u65b9\u6cd5\u4e4b\u95f4\u7684\u5bf9\u6bd4\uff0c\u5b9e\u8df5\uff0c\u8bef\u7528\u60c5\u51b5\u5206\u6790\uff0c\u4f18\u7f3a\u70b9\u7b49\u3002\u4e0e\u5927\u5bb6\u4e00\u8d77\uff0c\u5bf9\u8fd9\u9879Java8\u4e2d\u7684\u65b0\u7279\u6027\uff0c\u8fdb\u884c\u7406\u89e3\u548c\u6df1\u5165\u3002<\/p>\n<h2>1\u3001\u89e3\u51b3\u7684\u95ee\u9898<\/h2>\n<p>\u81ed\u540d\u662d\u8457\u7684\u7a7a\u6307\u9488\u5f02\u5e38\uff0c\u662f\u6bcf\u4e2a\u7a0b\u5e8f\u5458\u90fd\u4f1a\u9047\u5230\u7684\u4e00\u79cd\u5e38\u89c1\u5f02\u5e38\uff0c\u4efb\u4f55\u8bbf\u95ee\u5bf9\u8c61\u7684\u65b9\u6cd5\u4e0e\u5c5e\u6027\u7684\u8c03\u7528\uff0c\u90fd\u53ef\u80fd\u4f1a\u51fa\u73b0NullPointException\uff0c\u5982\u679c\u8981\u786e\u4fdd\u4e0d\u89e6\u53d1\u5f02\u5e38\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u8fdb\u884c\u5bf9\u8c61\u7684\u5224\u7a7a\u64cd\u4f5c\u3002<\/p>\n<p>\u4e3e\u4e2a\u6817\u5b50\uff0c\u6709\u4e00\u4e2a\u4eba\uff08Shopper\uff09\u8fdb\u8d85\u5e02\u8d2d\u7269\uff0c\u53ef\u80fd\u4f1a\u7528\u8d2d\u7269\u8f66\uff08Trolley\uff09\u4e5f\u53ef\u80fd\u4f1a\u7528\u5176\u5b83\u65b9\u5f0f\uff0c\u8d2d\u7269\u8f66\u91cc\u53ef\u80fd\u4f1a\u6709\u4e00\u888b\u6817\u5b50\uff08Chestnut\uff09\uff0c\u4e5f\u53ef\u80fd\u6ca1\u6709\u3002\u4e09\u8005\u5b9a\u4e49\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic class Shopper {\n  private Trolley trolley;\n  public Trolley getTrolley(){\n    return trolley;\n  }\n}\npublic class Trolley {\n  private Chestnut chestnut;\n  public Chestnut getChestnut(){\n     return chestnut;\n  }\n}\npublic class Chestnut {\n  private String name;\n  public String getName(){\n    return name;\n  }\n}<\/code><\/pre>\n<p>\u8fd9\u65f6\u60f3\u8981\u83b7\u5f97\u8d2d\u7269\u8f66\u4e2d\u6817\u5b50\u7684\u540d\u79f0\uff0c\u50cf\u4e0b\u9762\u8fd9\u4e48\u5199\uff0c\u5c31\u53ef\u80fd\u4f1a\u89c1\u5230\u6211\u4eec\u7684\u201c\u8001\u670b\u53cb\u201d\uff08NPE\uff09<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic String result(Shopper shopper){\n  return shopper.getTrolley().getChestnut().getName();\n}<\/code><\/pre>\n<p>\u4e3a\u4e86\u80fd\u907f\u514d\u51fa\u73b0\u7a7a\u6307\u9488\u5f02\u5e38\uff0c\u901a\u5e38\u7684\u5199\u6cd5\u4f1a\u9010\u5c42\u5224\u7a7a\uff08\u591a\u5c42\u5d4c\u5957\u6cd5\uff09\uff0c\u5982\u4e0b<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic String result(Shopper shopper) {\n        if (shopper != null) {\n            Trolley trolley = shopper.getTrolley();\n            if (trolley != null) {\n                Chestnut chestnut = trolley.getChestnut();\n                if (chestnut != null) {\n                    return chestnut.getName();\n                }\n            }\n        }\n        return &quot;\u83b7\u53d6\u5931\u8d25\u8fbd&quot;;\n    }<\/code><\/pre>\n<p>\u591a\u5c42\u5d4c\u5957\u7684\u65b9\u6cd5\u5728\u5bf9\u8c61\u7ea7\u8054\u5173\u7cfb\u6bd4\u8f83\u6df1\u7684\u65f6\u5019\u4f1a\u770b\u7684\u773c\u82b1\u7f2d\u4e71\u7684\uff0c\u5c24\u5176\u662f\u90a3\u4e00\u5c42\u4e00\u5c42\u7684\u62ec\u53f7\uff1b\u53e6\u5916\u51fa\u9519\u7684\u539f\u56e0\u4e5f\u56e0\u4e3a\u7f3a\u4e4f\u5bf9\u5e94\u4fe1\u606f\u800c\u88ab\u6a21\u7cca\uff08\u4f8b\u5982trolley\u4e3a\u7a7a\u65f6\u4e5f\u53ea\u8fd4\u56de\u4e86\u6700\u540e\u7684\u83b7\u53d6\u5931\u8d25\u3002\u5f53\u7136\u4e5f\u53ef\u4ee5\u5728\u6bcf\u4e00\u5c42\u589e\u52a0return\uff0c\u76f8\u5e94\u7684\u4ee3\u7801\u6709\u4f1a\u53d8\u5f97\u5f88\u5197\u957f\uff09\uff0c\u6240\u4ee5\u6b64\u65f6\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u9047\u7a7a\u5219\u8fd4\u56de\u7684\u536b\u8bed\u53e5\u8fdb\u884c\u6539\u5199\u3002<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic String result(Shopper shopper) {\n    if (shopper == null) {\n        return &quot;\u8d2d\u7269\u8005\u4e0d\u5b58\u5728&quot;;\n    }\n    Trolley trolley = shopper.getTrolley();\n    if (trolley == null) {\n        return &quot;\u8d2d\u7269\u8f66\u4e0d\u5b58\u5728&quot;;\n    }\n    Chestnut chestnut = trolley.getChestnut();\n    if (chestnut == null) {\n        return &quot;\u6817\u5b50\u4e0d\u5b58\u5728&quot;;\n    }\n    return chestnut.getName();\n}<\/code><\/pre>\n<p>\u4e3a\u4e86\u53d6\u4e00\u4e2a\u540d\u5b57\u8fdb\u884c\u4e86\u4e09\u6b21\u663e\u793a\u5224\u7a7a\u64cd\u4f5c\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u5f53\u7136\u6ca1\u6709\u95ee\u9898\uff0c\u4f46\u662f\u4f18\u79c0\u7684\u5de5\u7a0b\u5e08\u4eec\u603b\u662f\u5e0c\u671b\u80fd\u83b7\u5f97\u66f4\u4f18\u96c5\u7b80\u6d01\u7684\u4ee3\u7801\u3002Optional\u5c31\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e86\u8fd9\u6837\u7684\u671f\u671b\u3002<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic String result(Shopper shopper){\n  return Optional.ofNullable(shopper)\n                .map(Shopper::getTrolley)\n                .map(Trolley::getChestnut)\n .map(Chestnut::getName)\n .orElse(&quot;\u83b7\u53d6\u5931\u8d25\u8fbd&quot;);\n}<\/code><\/pre>\n<h2>2\u3001\u5e38\u7528\u65b9\u6cd5<\/h2>\n<h3>1\uff09\u83b7\u5f97Optional\u5bf9\u8c61<\/h3>\n<p>Optional\u7c7b\u4e2d\u6709\u4e24\u4e2a\u6784\u9020\u65b9\u6cd5\uff1a\u5e26\u53c2\u548c\u4e0d\u5e26\u53c2\u7684\u3002\u5e26\u53c2\u7684\u5c06\u4f20\u5165\u7684\u53c2\u6570\u8d4b\u503cvalue\uff0c\u4ece\u800c\u6784\u5efaOptional\u5bf9\u8c61\uff1b\u4e0d\u5e26\u53c2\u7684\u7528null\u521d\u59cb\u5316value\u6784\u5efa\u5bf9\u8c61\u3002<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nprivate Optional() {}\nprivate Optional(T value) {}<\/code><\/pre>\n<p>\u4f46\u662f\u4e24\u8005\u90fd\u662f\u79c1\u6709\u65b9\u6cd5\uff0c\u800c\u5b9e\u9645\u4e0aOptional\u7684\u5bf9\u8c61\u90fd\u662f\u901a\u8fc7<strong>\u9759\u6001\u5de5\u5382\u6a21\u5f0f<\/strong>\u7684\u65b9\u5f0f\u6784\u5efa\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u4e09\u4e2a\u51fd\u6570<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic static &lt;T&gt; Optional&lt;T&gt; of(T value) {}\npublic static &lt;T&gt; Optional&lt;T&gt; ofNullable(T value) {}\npublic static &lt;T&gt; Optional&lt;T&gt; empty() {}<\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a\u4e00\u5b9a\u4e0d\u4e3a\u7a7a\u7684Optional\u5bf9\u8c61\uff0c\u56e0\u4e3a\u5982\u679c\u4f20\u5165\u7684\u53c2\u6570\u4e3a\u7a7a\u4f1a\u629b\u51faNPE<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nChestnut chestnut = new Chestnut();\nOptional&lt;Chestnut&gt; opChest = Optional.of(chestnut);<\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684Optional\u5bf9\u8c61<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nOptional&lt;Chestnut&gt; opChest = Optional.empty();<\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a\u53ef\u7a7a\u7684Optional\u5bf9\u8c61<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nChestnut chestnut = null;\nOptional&lt;Chestnut&gt; opChest = Optional.ofNullable(chestnut);<\/code><\/pre>\n<h3>2\uff09\u6b63\u5e38\u4f7f\u7528<\/h3>\n<p>\u6b63\u5e38\u4f7f\u7528\u7684\u65b9\u6cd5\u53ef\u4ee5\u88ab\u5927\u81f4\u5206\u4e3a\u4e09\u79cd\u7c7b\u578b\uff0c<strong>\u5224\u65ad\u7c7b<\/strong>\u3001<strong>\u64cd\u4f5c\u7c7b<\/strong>\u548c<strong>\u53d6\u503c\u7c7b<\/strong><\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\n\/\/\u5224\u65ad\u7c7b\npublic boolean isPresent() {}\n\/\/\u64cd\u4f5c\u7c7b\npublic void ifPresent(Consumer&lt;? super T&gt; consumer) {}\n\/\/\u53d6\u503c\u7c7b\npublic T get() {}\npublic T orElse(T other) {}\npublic T orElseGet(Supplier&lt;? extends T&gt; other) {}\npublic &lt;X extends Throwable&gt; T orElseThrow(Supplier&lt;? extends X&gt; exceptionSupplier) throws X {}<\/code><\/pre>\n<p><strong>isPresent()<\/strong>\u65b9\u6cd5\u50cf\u4e00\u4e2a\u5b89\u5168\u9600\uff0c\u63a7\u5236\u7740\u5bb9\u5668\u4e2d\u7684value\u503c\u662f\u7a7a\u8fd8\u662f\u6709\u503c\uff0c\u7528\u6cd5\u4e0e\u539f\u672c\u7684null != obj\u7684\u7528\u6cd5\u76f8\u4f3c\u3002\u5f53obj\u6709\u503c\u8fd4\u56detrue\uff0c\u4e3a\u7a7a\u8fd4\u56defalse\uff08\u5373value\u503c\u5b58\u5728\u4e3a\u771f\uff09\u3002\u4f46\u4e00\u822c\u5b9e\u73b0\u5224\u65ad\u7a7a\u6216\u4e0d\u4e3a\u7a7a\u7684\u903b\u8f91\uff0c\u4f7f\u7528Optional\u5176\u4ed6\u7684\u65b9\u6cd5\u5904\u7406\u4f1a\u66f4\u4e3a\u5e38\u89c1\u3002\u5982\u4e0b\u4ee3\u7801\u5c06\u4f1a\u6253\u5370\u51fa\u6ca1\u6709\u6817\u5b50\u7684\u60b2\u60e8\u4e8b\u5b9e\u3002<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nOptional&lt;Chestnut&gt; opChest = Optional.empty();\nif (!opChest.isPresent()){\n  System.out.println(&quot;\u5bb9\u5668\u91cc\u6ca1\u6709\u6817\u5b50&quot;);\n}<\/code><\/pre>\n<p><strong>ifPresent()<\/strong>\u65b9\u6cd5\u662f\u4e00\u4e2a\u64cd\u4f5c\u7c7b\u7684\u65b9\u6cd5\uff0c\u4ed6\u7684\u53c2\u6570\u662f\u4e00\u6bb5\u76ee\u6807\u7c7b\u578b\u4e3aConsumer\u7684\u51fd\u6570\uff0c\u5f53value\u4e0d\u4e3a\u7a7a\u65f6\uff0c\u81ea\u52a8\u6267\u884cconsumer\u4e2d\u7684accept()\u65b9\u6cd5\uff08\u4f20\u5165\u65f6\u5b9e\u73b0\uff09\uff0c\u4e3a\u7a7a\u5219\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002\u6bd4\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u6211\u4eec\u4f20\u5165\u4e86\u4e00\u6bb5\u8f93\u51favalue\u7684lamda\u8868\u8fbe\u5f0f\uff0c\u6253\u5370\u51fa\u4e86\u201c\u8fc1\u897f\u677f\u6817\u201d\u3002<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nOptional&lt;Chestnut&gt; opChest = Optional.ofNullable(new Chestnut(&quot;\u8fc1\u897f\u677f\u6817&quot;));\nopChest.ifPresent(c -&gt; System.out.println(c.getName()));<\/code><\/pre>\n<p><strong>get()<\/strong>\u65b9\u6cd5\u6e90\u7801\u5982\u4e0b\uff0c\u53ef\u4ee5\u770b\u51fa\uff0cget\u7684\u4f5c\u7528\u662f\u76f4\u63a5\u8fd4\u56de\u5bb9\u5668\u4e2d\u7684value\u3002\u4f46\u5982\u6b64\u7c97\u66b4\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528\u524d\u5982\u679c\u4e0d\u5224\u7a7a\uff0c\u5728value\u4e3a\u7a7a\u65f6\uff0c\u4fbf\u4f1a\u6beb\u4e0d\u7559\u60c5\u5730\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic T get() {\n   if (value == null) {\n      throw new NoSuchElementException(&quot;No value present&quot;);\n   }\n   return value;\n}<\/code><\/pre>\n<p>\u4e09\u4e2aorElse\u65b9\u6cd5\u4e0eget\u76f8\u4f3c\uff0c\u4e5f\u90fd\u5c5e\u4e8e\u53d6\u503c\u7684\u64cd\u4f5c\u3002\u4e0eget\u4e0d\u540c\u4e4b\u5904\u5728\u4e8eorElse\u65b9\u6cd5\u4e0d\u7528\u989d\u5916\u7684\u5224\u7a7a\u8bed\u53e5\uff0c\u64b0\u5199\u903b\u8f91\u65f6\u6bd4\u8f83\u6109\u5feb\u3002\u4e09\u4e2aorElse\u7684\u76f8\u540c\u4e4b\u5904\u662f\u5f53value\u4e0d\u4e3a\u7a7a\u65f6\u90fd\u4f1a\u8fd4\u56devalue\u3002\u5f53\u4e3a\u7a7a\u65f6\uff0c\u5219\u53e6\u6709\u5404\u81ea\u7684\u64cd\u4f5c\uff1a<strong>orElse()<\/strong>\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4f20\u5165\u7684other\u5b9e\u4f8b\uff08\u4e5f\u53ef\u4ee5\u4e3aSupplier\u7c7b\u578b\u7684\u51fd\u6570\uff09\uff1b<strong>orElseGet()<\/strong>\u65b9\u6cd5\u4f1a\u81ea\u52a8\u6267\u884cSupplier\u7c7b\u578b\u5b9e\u4f8b\u7684get()\u65b9\u6cd5\uff1b<strong>orElseThrow()<\/strong>\u65b9\u6cd5\u4f1a\u629b\u51fa\u4e00\u4e2a\u81ea\u5b9a\u7684\u5f02\u5e38\u3002\u66f4\u5177\u4f53\u7684\u5dee\u522b\u4f1a\u5728\u540e\u9762\u7684<strong>\u65b9\u6cd5\u5bf9\u6bd4<\/strong>\u4e2d\u63cf\u8ff0\u3002<\/p>\n<p>\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5728\u6ca1\u6709\u6817\u5b50\u7684\u65f6\u5019\uff0c\u5982\u4f55\u5410\u51fa\u201c\u4fe1\u9633\u677f\u6817\u201d\u3001\u201c\u9547\u5b89\u677f\u6817\u201d\uff0c\u4ee5\u53ca\u629b\u51fa\u201c\u62b9\u6cb9\u6817\u5b50\u201d\u7684\u8b66\u544a\u3002<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nOptional&lt;Chestnut&gt; opChest = Optional.empty();\nSystem.out.println(opChest.orElse(new Chestnut(&quot;\u4fe1\u9633\u677f\u6817&quot;)));\nSystem.out.println(opChest.orElseGet(() -&gt; new Chestnut(&quot;\u9547\u5b89\u677f\u6817&quot;)));\ntry {\n   opChest.orElseThrow(() -&gt; new RuntimeException(&quot;\u62b9\u6cb9\u6817\u5b50\u5440&quot;));\n}catch (RuntimeException e){\n   System.out.println(e.getMessage());\n}<\/code><\/pre>\n<h3>3\uff09\u8fdb\u9636\u4f7f\u7528<\/h3>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic Optional&lt;T&gt; filter(Predicate&lt;? super T&gt; predicate) {}\npublic&lt;U&gt; Optional&lt;U&gt; map(Function&lt;? super T, ? extends U&gt; mapper) {}\npublic&lt;U&gt; Optional&lt;U&gt; flatMap(Function&lt;? super T, Optional&lt;U&gt;&gt; mapper) {}<\/code><\/pre>\n<p><strong>filter()<\/strong>\u65b9\u6cd5\u63a5\u53d7\u8c13\u8bcd\u4e3aPredicate\u7c7b\u578b\u7684\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5982\u679cvalue\u503c\u4e0d\u4e3a\u7a7a\u5219\u81ea\u52a8\u6267\u884cpredicate\u7684test()\u65b9\u6cd5\uff08\u4f20\u5165\u65f6\u5b9e\u73b0\uff09\uff0c\u6765\u5224\u65ad\u662f\u5426\u6ee1\u8db3\u6761\u4ef6\uff0c\u6ee1\u8db3\u5219\u4f1a\u8fd4\u56de\u81ea\u8eabOptional\uff0c\u4e0d\u6ee1\u8db3\u4f1a\u8fd4\u56de\u7a7aOptional\uff1b\u5982\u679cvalue\u503c\u4e3a\u7a7a\uff0c\u5219\u4f1a\u8fd4\u56de\u81ea\u8eabOptional\uff08\u5176\u5b9e\u8ddf\u7a7aOptional\u4e5f\u5dee\u4e0d\u591a\uff09\u3002\u5982\u4e0b\u4ee3\u7801\uff0c\u7b2c\u4e8c\u53e5\u4e2d\u7b5b\u9009\u6761\u4ef6\u201c\u90b5\u5e97\u677f\u6817\u201d\u4e0eopChest\u4e2d\u7684\u677f\u6817\u540d\u4e0d\u7b26\uff0c\u6ca1\u6709\u901a\u8fc7\u8fc7\u6ee4\u3002\u800c\u7b2c\u4e09\u53e5\u7684\u7b5b\u9009\u6761\u4ef6\u4e0eopChest\u4e00\u81f4\uff0c\u6240\u4ee5\u6700\u540e\u6253\u5370\u51fa\u6765\u7684\u662f\u201c\u5bbd\u57ce\u677f\u6817\u201d\u3002<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nOptional&lt;Chestnut&gt; opChest = Optional.ofNullable(new Chestnut(&quot;\u5bbd\u57ce\u677f\u6817&quot;));\nopChest.filter(c -&gt; &quot;\u90b5\u5e97\u677f\u6817&quot;.equals(c.getName())).ifPresent(System.out::println);\nopChest.filter(c -&gt; &quot;\u5bbd\u57ce\u677f\u6817&quot;.equals(c.getName())).ifPresent(System.out::println);<\/code><\/pre>\n<p><strong>map()<\/strong>\u548c<strong>flatmap()<\/strong>\u65b9\u6cd5\u4f20\u5165\u7684\u90fd\u662f\u4e00\u4e2aFunction\u7c7b\u578b\u7684\u51fd\u6570\uff0cmap\u5728\u8fd9\u91cc\u7ffb\u8bd1\u4e3a\u201c\u6620\u5c04\u201d\uff0c\u5f53value\u503c\u4e0d\u4e3a\u7a7a\u65f6\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\uff0c\u8fd4\u56de\u7684\u503c\u662f\u7ecf\u8fc7mapper\u7684apply()\u65b9\u6cd5\u5904\u7406\u540e\u7684Optional\u7c7b\u578b\u7684\u503c\uff0c\u4e24\u4e2a\u65b9\u6cd5\u7684\u7ed3\u679c\u4e00\u81f4\uff0c\u5904\u7406\u8fc7\u7a0b\u4e2d\u4f1a\u6709\u5dee\u522b\u3002\u5982\u4e0b\u4ee3\u7801\uff0c\u4eceopChest\u4e2d\u83b7\u53d6\u4e86\u677f\u6817\u540d\u540e\uff0c\u91cd\u65b0new\u4e86\u4e00\u4e2a\u677f\u6817\u53d6\u540d\u201c\u90a2\u53f0\u677f\u6817\u201d\uff0c\u5e76\u6253\u5370\u51fa\u6765\uff0c\u4e24\u8005\u8f93\u51fa\u4e00\u81f4\uff0c\u5904\u7406\u5f62\u5f0f\u4e0a\u6709\u5dee\u5f02\uff0c\u8fd9\u4e2a\u5728\u540e\u9762\u7684<strong>\u65b9\u6cd5\u5bf9\u6bd4<\/strong>\u4e2d\u4f1a\u518d\u6b21\u8bf4\u5230\u3002<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nOptional&lt;Chestnut&gt; opChest = Optional.ofNullable(new Chestnut(&quot;\u90a2\u53f0\u677f\u6817&quot;));\nSystem.out.println(opChest.map(c -&gt; new Chestnut(c.getName())));\nSystem.out.println(opChest.flatMap(c -&gt; Optional.ofNullable(new Chestnut(c.getName()))));<\/code><\/pre>\n<h3>4\uff091.9\u65b0\u589e<\/h3>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic void ifPresentOrElse(Consumer&lt;? super T&gt; action, Runnable emptyAction) {}\npublic Optional&lt;T&gt; or(Supplier&lt;? extends Optional&lt;? extends T&gt;&gt; supplier) {}\npublic Stream&lt;T&gt; stream() {}<\/code><\/pre>\n<p>JDK1.9\u4e2d\u589e\u52a0\u4e86\u4e09\u4e2a\u65b9\u6cd5\uff1a<strong>i<\/strong><strong>fPresentOrElse()<\/strong>\u3001<strong>or()<\/strong>\u548c<strong>stream()<\/strong>\u65b9\u6cd5\u3002<\/p>\n<p>1.8\u65f6\uff0cifPresent()\u4ec5\u63d0\u4f9b\u4e86if(obj != null)\u7684\u65b9\u6cd5\uff0c\u5e76\u672a\u63d0\u4f9bif(obj != null)else{}\u7684\u64cd\u4f5c\uff0c\u6240\u4ee5\u57281.9\u4e2d\u589e\u52a0\u4e86\u4e00\u4e2a<strong>ifPresentElse()<\/strong>\u65b9\u6cd5\uff0c\u63d0\u4f9b\u4e86\u8fd9\u65b9\u9762\u7684\u652f\u6301\u3002\u8be5\u65b9\u6cd5\u63a5\u6536\u4e24\u4e2a\u53c2\u6570Consumer\u548cRunnable\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u5f53value\u4e0d\u4e3a\u7a7a\uff0c\u8c03\u7528action\u7684accept()\u65b9\u6cd5\uff0c\u8fd9\u70b9\u4e0eifPresent()\u4e00\u81f4\uff0c\u5f53value\u4e3a\u7a7a\u65f6\uff0c\u4f1a\u8c03\u7528emptyAction\u7684run()\u65b9\u6cd5\uff0c\u6267\u884celse\u8bed\u4e49\u7684\u903b\u8f91\u3002\u5982\u4e0b\u9762\u4ee3\u7801\uff0c\u4f1a\u6253\u5370\u51fa\u201c\u6728\u6709\u6817\u5b50\u201d\u7684\u63d0\u793a\u3002<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nOptional&lt;Chestnut&gt; opChest = Optional.empty();\nopChest.ifPresentElse(c -&gt; System.out.println(c.getName()),c -&gt; System.out.println(&quot;\u6728\u6709\u6817\u5b50\u5440&quot;));<\/code><\/pre>\n<p><strong>or()<\/strong>\u65b9\u6cd5\u662f\u4f5c\u4e3aorElse()\u548corElseGet()\u65b9\u6cd5\u7684\u6539\u8fdb\u800c\u51fa\u73b0\u7684\uff0c\u4f7f\u7528\u65b9\u6cd5\u4e00\u81f4\uff0c\u4f46\u540e\u4e24\u4e2a\u65b9\u6cd5\u5728\u6267\u884c\u5b8c\u6210\u540e\u8fd4\u56de\u7684\u5e76\u975e\u5305\u88c5\u503c\u3002\u5982\u679c\u9700\u8981\u6267\u884c\u4e00\u4e9b\u903b\u8f91\u5e76\u8fd4\u56deOptional\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528or()\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f20\u5165Supplier\u63a5\u53e3\u7684\u5b9e\u4f8b\uff0c\u5f53value\u6709\u503c\u65f6\u76f4\u63a5\u8fd4\u56de\u81ea\u8eabOptional\uff0c\u5f53\u4e3a\u7a7a\u65f6\uff0c\u81ea\u52a8\u6267\u884csuuplier\u7684get()\u65b9\u6cd5\uff0c\u5e76\u5305\u88c5\u6210Optional\u8fd4\u56de\uff0c\u5176\u6e90\u7801\u4e2d\u5305\u88c5\u7684\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nOptional&lt;T&gt; r = (Optional&lt;T&gt;) supplier.get();\nreturn Objects.requireNonNull(r);<\/code><\/pre>\n<p><strong>stream()<\/strong>\u65b9\u6cd5\u5219\u4e0d\u7528\u591a\u8bf4\uff0c\u662f\u4e00\u4e2a\u63d0\u4f9b\u7ed9\u6d41\u5f0f\u7f16\u7a0b\u4f7f\u7528\u7684\u65b9\u6cd5\uff0c\u529f\u80fd\u4e0a\u662f\u4e00\u4e2a<strong>\u9002\u914d\u5668<\/strong>\uff0c\u5c06Optional\u8f6c\u6362\u6210Stream\uff1a\u6ca1\u6709\u503c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684stream\uff0c\u6216\u8005\u5305\u542b\u4e00\u4e2aOptional\u7684stream\u3002\u5176\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nif (!isPresent()) {\n  return Stream.empty();\n} else {\n  return Stream.of(value);\n}<\/code><\/pre>\n<h2>3\u3001\u65b9\u6cd5\u5bf9\u6bd4\u548c\u603b\u7ed3<\/h2>\n<p>Optional\u5c01\u88c5\u7684\u65b9\u6cd5\u8f83\u591a\uff0c\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u65b9\u6cd5\u7684\u524d\u63d0\u662f\u8981\u4e86\u89e3\u5404\u81ea\u9002\u7528\u7684\u573a\u666f\u548c\u5f02\u540c<\/p>\n<h3>1\uff09\u521b\u5efa\u65b9\u6cd5\u7684\u5bf9\u6bd4<\/h3>\n<p>\u7531\u4e8e\u6784\u9020\u5668\u4e3a\u79c1\u6709\u65b9\u6cd5\uff0c\u521b\u5efa\u5bf9\u8c61\u53ea\u80fd\u901a\u8fc7\u9759\u6001\u5de5\u5382\u7684\u65b9\u5f0f\u521b\u5efa\u3002of()\u3001ofNullable()\u548cempty()\u65b9\u6cd5\u662f\u4e09\u4e2a\u9759\u6001\u65b9\u6cd5\u3002\u5148\u4e0a\u6e90\u7801\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\n\/\/\u5de5\u5382\u65b9\u6cd5\npublic static &lt;T&gt; Optional&lt;T&gt; of(T value) {\n  return new Optional&lt;&gt;(value);\n}\n\n\npublic static &lt;T&gt; Optional&lt;T&gt; ofNullable(T value) {\n  return value == null ? empty() : of(value);\n}\n\n\npublic static&lt;T&gt; Optional&lt;T&gt; empty() {\n  @SuppressWarnings(&quot;unchecked&quot;)\n  Optional&lt;T&gt; t = (Optional&lt;T&gt;) EMPTY;\n  return t;\n}\n\/\/\u6784\u9020\u65b9\u6cd5\nprivate Optional() {\n  this.value = null;\n}\nprivate Optional(T value) {\n  this.value = Objects.requireNonNull(value);\n}\n\/\/\u9759\u6001\u5e38\u91cf\nprivate static final Optional&lt;?&gt; EMPTY = new Optional&lt;&gt;()<\/code><\/pre>\n<p><strong>of()<\/strong>\u65b9\u6cd5\u901a\u8fc7\u8c03\u7528\u5e26\u53c2\u6784\u9020\uff0cnew\u51fa\u4e00\u4e2aOptional\u5bf9\u8c61\uff0c\u6b63\u5e38\u5f62\u53c2\u5e26\u503c\u662f\u4e0d\u4f1a\u6709\u95ee\u9898\u7684\uff0c\u4f46\u662f\u5f53\u5f62\u53c2\u4e3a\u7a7a\u65f6\uff0c\u8bbe\u7f6evalue\u524d\u7684Objects.requireNonNull()\u975e\u7a7a\u6821\u9a8c\uff0c\u5c31\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic static &lt;T&gt; T requireNonNull(T obj) {\n  if (obj == null)\n    throw new NullPointerException();\n  return obj;\n}<\/code><\/pre>\n<p>requireNonNull()\u65b9\u6cd5\u662fjava.util\u5305\u4e0bObjects\u7c7b\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4f5c\u7528\u662f\u68c0\u67e5\u4f20\u5165\u7684\u53c2\u6570\u662f\u5426\u4e3a\u7a7a\uff0c\u4e3a\u7a7a\u4f1a\u629b\u51fa\u4e00\u4e2aNPE\uff0c\u5728Optional\u7c7b\u4e2d\u7528\u5230\u7684\u5730\u65b9\u8fd8\u6709\u5f88\u591a\u3002\u6240\u4ee5\u53ea\u6709\u786e\u4fe1\u6784\u9020Optional\u6240\u4f20\u5165\u7684\u53c2\u6570\u4e0d\u4e3a\u7a7a\u65f6\u624d\u53ef\u4f7f\u7528of()\u65b9\u6cd5\u3002<\/p>\n<p>\u4e0eof()\u76f8\u5bf9\u7684\u8fd8\u6709\u4e00\u4e2a<strong>ofNullable()<\/strong>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5141\u8bb8\u63a5\u53d7null\u503c\u6784\u9020Optional\uff0c\u5f53\u5f62\u53c2\u4e3anull\u65f6\uff0c\u8c03\u7528empty()\u65b9\u6cd5\uff0c\u800c<strong>empty()<\/strong>\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u7f16\u8bd1\u671f\u5c31\u786e\u5b9a\u7684\u5e38\u91cfEMPTY\uff0cEMPTY\u53d6\u503c\u662f\u65e0\u53c2\u6784\u9020\u5668\u521b\u5efa\u5bf9\u8c61\uff0c\u6700\u7ec8\u5f97\u5230\u7684\u662f\u4e00\u4e2avalue\u4e3a\u7a7a\u7684Optional\u5bf9\u8c61\u3002<\/p>\n<h3>2\uff09\u4f7f\u7528\u65b9\u6cd5\u7684\u5bf9\u6bd4<\/h3>\n<p>2.2)\u4e2d\u8bf4\u5230\uff0c\u6b63\u5e38\u4f7f\u7528\u7684\u65b9\u6cd5\u4e2d\u6709\u5c5e\u4e8e\u53d6\u503c\u7c7b\u7684\u65b9\u6cd5\uff0corElse()\u3001orElseGet()\u548corElseThrow()\uff0c\u8fd9\u4e09\u4e2a\u65b9\u6cd5\u5728\u975e\u7a7a\u65f6\u5747\u8fd4\u56devalue\uff0c\u4f46\u662f\u4e3a\u7a7a\u65f6\u7684\u5904\u7406\u5404\u4e0d\u76f8\u540c\u3002\u5148\u4e0a\u6e90\u7801\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic T orElse(T other) {\n  return value != null ? value : other;\n}\n\n\npublic T orElseGet(Supplier&lt;? extends T&gt; other) {\n  return value != null ? value : other.get();\n}\n\n\npublic &lt;X extends Throwable&gt; T orElseThrow(Supplier&lt;? extends X&gt; exceptionSupplier) throws X {\n  if (value != null) {\n    return value;\n  } else {\n    throw exceptionSupplier.get();\n  }<\/code><\/pre>\n<p>orElse()\u548corElseGet()\u65b9\u6cd5\u6700\u76f4\u89c2\u7684\u5dee\u5f02\u662f\u5f62\u53c2\u7684\u4e0d\u540c\uff0c\u770b\u4e0b\u9762\u4e00\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\n\/\/\u6d4b\u8bd5\u8bed\u53e5\nOptional&lt;Chestnut&gt; opChest = Optional.ofNullable(new Chestnut(&quot;\u6850\u67cf\u677f\u6817&quot;));\n\/\/Optional&lt;Chestnut&gt; opChest = Optional.empty();\nopChest.orElse(print(&quot;orELse&quot;));\nopChest.orElseGet(()-&gt;print(&quot;orElseGet&quot;));\n\/\/\u8c03\u7528\u65b9\u6cd5\nprivate static Chestnut print(String method){\n  System.out.println(&quot;\u71d5\u5c71\u677f\u6817\u6700\u597d\u5403----&quot;+method);\n  return new Chestnut(&quot;\u71d5\u5c71\u677f\u6817&quot;);\n}<\/code><\/pre>\n<p>\u7b2c\u4e00\u6b21\uff0cnew\u51fa\u4e00\u4e2a\u201c\u6850\u67cf\u677f\u6817\u201d\u7684Optional\uff0c\u5206\u522b\u8c03\u7528orElse()\u548corElseGet()\u65b9\u6cd5\uff0c\u7ed3\u679c\u51fa\u73b0\u4e86\u4e24\u884c\u7684\u201c\u71d5\u5c71\u677f\u6817\u6700\u597d\u5403\u201d\u7684\u8f93\u51fa\uff0c\u56e0\u4e3a\u4e24\u4e2a\u65b9\u6cd5\u5728value\u4e0d\u4e3anull\u65f6\u90fd\u4f1a\u6267\u884c\u5f62\u53c2\u4e2d\u7684\u65b9\u6cd5\uff1b<\/p>\n<p>\u7b2c\u4e8c\u6b21\uff0c\u901a\u8fc7empty()\u65b9\u6cd5\u83b7\u5f97\u4e00\u4e2a\u7a7a\u6817\u5b50\u7684Optional\uff0c\u518d\u8c03\u7528orElse()\u548corElseGet()\u65b9\u6cd5\uff0c\u7ed3\u679c\u5c45\u7136\u8fd8\u51fa\u73b0\u4e86\u4e00\u884c\u201c\u71d5\u5c71\u677f\u6817\u6700\u597d\u5403\u201d\u7684\u8f93\u51fa\u3002<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\n\u7b2c\u4e00\u6b21\u8f93\u51fa\uff1a\n\u71d5\u5c71\u677f\u6817\u6700\u597d\u5403----orELse\n\u71d5\u5c71\u677f\u6817\u6700\u597d\u5403----orElseGet\n\u7b2c\u4e8c\u6b21\u8f93\u51fa\uff1a\n\u71d5\u5c71\u677f\u6817\u6700\u597d\u5403----orELse<\/code><\/pre>\n<p>\u5176\u539f\u56e0\u662f<strong>orElseGet()<\/strong>\u7684\u53c2\u6570\u662fSupplier\u76ee\u6807\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u7b80\u5355\u6765\u8bf4\uff0cSuppiler\u63a5\u53e3\u7c7b\u4f3cSpring\u7684\u61d2\u52a0\u8f7d\uff0c\u58f0\u660e\u4e4b\u540e\u5e76\u4e0d\u4f1a\u5360\u7528\u5185\u5b58\uff0c\u53ea\u6709\u6267\u884c\u4e86get()\u65b9\u6cd5\u4e4b\u540e\uff0c\u624d\u4f1a\u8c03\u7528\u6784\u9020\u65b9\u6cd5\u521b\u5efa\u51fa\u5bf9\u8c61\uff0c\u800c<strong>orElse()<\/strong>\u662f\u5feb\u52a0\u8f7d\uff0c\u5373\u4f7f\u6ca1\u6709\u8c03\u7528\uff0c\u4e5f\u4f1a\u5b9e\u9645\u7684\u8fd0\u884c\u3002<\/p>\n<p>\u8fd9\u4e2a\u7279\u6027\u5728\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u4e0a\u5dee\u8ddd\u4e0d\u5927\uff0c\u4f46\u662f\u5f53\u65b9\u6cd5\u662f\u4e00\u4e9b\u6267\u884c\u5bc6\u96c6\u578b\u7684\u8c03\u7528\u65f6\uff0c\u6bd4\u5982\u8fdc\u7a0b\u8c03\u7528\uff0c\u8ba1\u7b97\u7c7b\u6216\u8005\u67e5\u8be2\u7c7b\u65b9\u6cd5\u65f6\uff0c\u4f1a\u635f\u8017\u4e00\u5b9a\u7684\u6027\u80fd\u3002<\/p>\n<p><strong>orElseThrow()<\/strong>\u65b9\u6cd5\u4e0eorElseGet()\u65b9\u6cd5\u7684\u53c2\u6570\u90fd\u662f\u51fd\u6570\u7c7b\u578b\u7684\uff0c\u8fd9\u610f\u5473\u7740\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u662f\u61d2\u52a0\u8f7d\uff0c\u4f46\u9488\u5bf9\u4e8e\u5fc5\u987b\u8981\u4f7f\u7528\u5f02\u5e38\u63a7\u5236\u6d41\u7a0b\u7684\u573a\u666f\uff0corElseThrow()\u4f1a\u66f4\u52a0\u5408\u9002\uff0c\u56e0\u4e3a\u53ef\u4ee5\u63a7\u5236\u5f02\u5e38\u7c7b\u578b\uff0c\u4f7f\u5f97\u76f8\u6bd4NPE\u4f1a\u6709\u66f4\u4e30\u5bcc\u7684\u8bed\u4e49\u3002<\/p>\n<h3>3\uff09\u5176\u4ed6\u65b9\u6cd5\u7684\u5bf9\u6bd4<\/h3>\n<p>a\u3001map\u4e0efilterMap<\/p>\n<p>\u5148\u4e0a\u6e90\u7801\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic&lt;U&gt; Optional&lt;U&gt; map(Function&lt;? super T, ? extends U&gt; mapper) {\n  Objects.requireNonNull(mapper);\n  if (!isPresent())\n    return empty();\n  else {\n    return Optional.ofNullable(mapper.apply(value));\n  }\n}\n\n\npublic&lt;U&gt; Optional&lt;U&gt; flatMap(Function&lt;? super T, Optional&lt;U&gt;&gt; mapper) {\n  Objects.requireNonNull(mapper);\n  if (!isPresent())\n    return empty();\n  else {\n    return Objects.requireNonNull(mapper.apply(value));\n  }\n}<\/code><\/pre>\n<p><strong>map()<\/strong>\u4e0e<strong>filterMap()<\/strong>\u7684\u76f8\u540c\u70b9\u662f\uff0c\u90fd\u63a5\u53d7\u4e00\u4e2aFunction\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u5e76\u4e14\u8fd4\u56de\u503c\u90fd\u662fOptional\u7c7b\u578b\u7684\u6570\u636e\u3002\u4f46\u662f\u4ece\u6e90\u7801\u4e2d\u6211\u4eec\u4e5f\u80fd\u770b\u51fa\uff1a<\/p>\n<p><strong>\u9996\u5148\uff0c<\/strong>map()\u5728\u8fd4\u56de\u65f6\uff0c\u4f7f\u7528\u4e86ofNullable()\u51fd\u6570\u5bf9\u8fd4\u56de\u503c\u5305\u4e86\u4e00\u5c42\uff0c\u8fd9\u4e2a\u51fd\u6570\u57282.1)\u5df2\u7ecf\u8bf4\u8fc7\uff0c\u662f\u4e00\u4e2aOptional\u7684\u5de5\u5382\u51fd\u6570\uff0c\u4f5c\u7528\u662f\u5c06\u4e00\u4e2a\u6570\u636e\u5305\u88c5\u6210Optional\uff1b\u800cfilterMap()\u8fd4\u56de\u65f6\u53ea\u662f\u505a\u4e86\u975e\u7a7a\u6821\u9a8c\uff0c\u5728\u5e94\u7528mapper.apply\u65f6\u5c31\u5df2\u7ecf\u662f\u4e00\u4e2aOptional\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u5176\u6b21<\/strong>\uff0c\u4ece\u7b7e\u540d\u4e2d\u4e5f\u53ef\u4ee5\u770b\u51fa\uff0cmap()\u7684Function\u7684\u8f93\u51fa\u503c\u662f&#8221;\uff1f extends U&#8221;\uff0c\u8fd9\u610f\u5473\u7740\u5728mapper.apply()\u5904\u7406\u5b8c\u6210\u540e\uff0c\u53ea\u8981\u5410\u51fa\u4e00\u4e2aU\u7c7b\u578b\u6216\u8005U\u7c7b\u578b\u7684\u5b50\u7c7b\u5373\u53ef\uff1b\u800cfilterMap()\u7684Functional\u7684\u8f93\u51fa\u503c\u662f\u201cOptional\/&lt;U&gt;\u201d\uff0c\u5219\u5728mapper.apply()\u5904\u7406\u5b8c\u6210\u4e4b\u540e\uff0c\u8fd4\u56de\u7684\u5fc5\u987b\u662f\u4e00\u4e2aOptional\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<p>b\u3001ifPresent\u4e0eifPresentOrElse<\/p>\n<p><strong>ifPresentOrElse()<\/strong>\u65b9\u6cd5\u662f\u4f5c\u4e3a<strong>ifPresent()<\/strong>\u7684\u6539\u8fdb\u65b9\u6cd5\u51fa\u73b0\u7684\u3002\u5148\u770b\u6e90\u7801\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic void ifPresent(Consumer&lt;? super T&gt; action) {\n  if (value != null) {\n    action.accept(value);\n  }\n}\npublic void ifPresentOrElse(Consumer&lt;? super T&gt; action, Runnable emptyAction) {\n  if (value != null) {\n    action.accept(value);\n  } else {\n    emptyAction.run();\n  }\n}<\/code><\/pre>\n<p>\u4ece\u6e90\u7801\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0cifPresentOrElse()\u53c2\u6570\u589e\u52a0\u4e86\u4e00\u4e2aRunnable\u7c7b\u578b\u7684\u51fd\u6570emptyAction\uff0c\u5728value != null\u65f6\uff0c\u90fd\u6fc0\u6d3b\u4e86action.accept()\u65b9\u6cd5\u3002\u53ea\u662f\u5f53value == null\u65f6\uff0cifPresentOrElse()\u65b9\u6cd5\u8fd8\u4f1a\u8c03\u7528emptyAction.run()\u65b9\u6cd5\u3002\u6240\u4ee5\u603b\u7684\u6765\u8bf4\uff0cjdk1.9\u52a0\u5165ifPresentOrElse()\u65b9\u6cd5\uff0c\u662f\u4f5c\u4e3aifPreset\u5728if-else\u9886\u57df\u7684\u8865\u5145\u51fa\u73b0\u7684\u3002<\/p>\n<p>c\u3001or\u4e0eorElse<\/p>\n<p>\u540c\u6837\u4f5c\u4e3a\u6539\u8fdb\u7684or()\u65b9\u6cd5\u4e5f\u662f\u4e3a\u4e86\u89e3\u51b3orElse\u7cfb\u5217\u65b9\u6cd5\u7684\u201c\u5c0f\u7f3a\u70b9\u201d\u51fa\u73b0\u7684\uff0c\u5148\u770b\u6e90\u7801\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic Optional&lt;T&gt; or(Supplier&lt;? extends Optional&lt;? extends T&gt;&gt; supplier) {\n  Objects.requireNonNull(supplier);\n  if (isPresent()) {\n    return this;\n  } else {\n  @SuppressWarnings(&quot;unchecked&quot;)\n  Optional&lt;T&gt; r = (Optional&lt;T&gt;) supplier.get();\n  return Objects.requireNonNull(r);\n  }\n}\npublic T orElse(T other) {\n  return value != null ? value : other;\n}\npublic T orElseGet(Supplier&lt;? extends T&gt; supplier) {\n  return value != null ? value : supplier.get();\n}<\/code><\/pre>\n<p><strong>or()<\/strong>\u65b9\u6cd5\u5728\u7b7e\u540d\u5f62\u5f0f\u4e0a\u66f4\u63a5\u8fd1<strong>orElseGet()<\/strong>\uff0c\u5373\u5f62\u53c2\u90fd\u662fSupplier\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u4f46\u662f\u4e0e\u5176\u4e0d\u540c\u7684\u662f\uff0cor()\u65b9\u6cd5\u5728\u5f62\u53c2\u4e2d\uff0c\u6307\u5b9a\u4e86Supplier\u8fd4\u56de\u7684\u7c7b\u578b\u5fc5\u987b\u4e3aOptional\u7c7b\u578b\uff0c\u4e14value\u7684\u7c7b\u578b\u5fc5\u987b\u4e3aT\u6216\u8005T\u7684\u5b50\u7c7b\u3002orElse\u7cfb\u5217\u7684\u65b9\u6cd5\uff0c\u66f4\u50cf\u662f\u4e00\u79cd\u6d88\u8d39\u7684\u65b9\u6cd5\uff0c\u4ece\u4e00\u4e2aOptional\u7684\u5b9e\u4f8b\u4e2d\u201c\u53d6\u51fa\u201cvalue\u7684\u503c\u8fdb\u5165\u4e0b\u4e00\u6b65\u64cd\u4f5c\uff0c\u800cor()\u65b9\u6cd5\u5219\u50cf\u662f<strong>\u5efa\u9020\u8005\u6a21\u5f0f<\/strong>\uff0c\u5bf9value\u6709\u4e00\u5b9a\u7684\u64cd\u4f5c\u4e4b\u540e\uff0c\u91cd\u65b0\u5410\u51fa\u7684\u8fd8\u662fOptional\u7c7b\u578b\u7684\u6570\u636e\uff0c\u6240\u4ee5\u4f7f\u7528\u65f6\u53ef\u4ee5\u4e32\u8054\u5728\u4e00\u8d77\uff0c\u540e\u4e00\u4e2aor\u5904\u7406\u524d\u4e00\u4e2aor\u5410\u51fa\u7684Optional\u3002<\/p>\n<h3>4\uff09\u201c\u5371\u9669\u201d\u65b9\u6cd5\u7684\u5bf9\u6bd4<\/h3>\n<p>\u8fd9\u91cc\u6307\u7684\u201c<strong>\u5371\u9669<\/strong>\u201d\u6307\u7684\u662f\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u6bd5\u7adf\u5f15\u8fdbOptional\u7c7b\u7684\u76ee\u7684\u5c31\u662f\u53bb\u9664\u5bf9NPE\u7684\u5224\u65ad\uff0c\u5982\u679c\u6b64\u65f6\u518d\u629b\u51fa\u4e00\u4e2aNPE\u6216\u8005\u5176\u4ed6\u7684\u5f02\u5e38\uff0c\u6ca1\u6709\u5904\u7406\u597d\u5c31\u4f1a\u4e3a\u7a0b\u5e8f\u5f15\u5165\u4e0d\u5c0f\u7684\u9ebb\u70e6\u3002\u6240\u4ee5\u5bf9Optional\u4e2d\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\u505a\u4e00\u4e2a\u603b\u7ed3\u3002<\/p>\n<p><strong>\u9996\u5148<\/strong>\uff0c\u6700\u76f4\u89c2\u7684\u4f1a\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\u5c31\u662fof()\u65b9\u6cd5\uff0c\u56e0\u4e3aof\u65b9\u6cd5\u4f1a\u8c03\u7528\u5e26\u53c2\u6784\u9020\u521b\u5efa\u5b9e\u4f8b\uff0c\u800c\u5e26\u53c2\u6784\u9020\u4e2d\u6709\u5bf9value\u975e\u7a7a\u7684\u68c0\u67e5\uff0c\u5982\u679c\u7a7a\u4f1a\u629b\u51fa<strong>NPE<\/strong>\u5f02\u5e38\uff1b<\/p>\n<p><strong>\u5176\u6b21<\/strong>\uff0cget()\u65b9\u6cd5\u4e5f\u662f\u4e00\u4e2a\u201c\u5371\u9669\u201d\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5f53\u4e0d\u5224\u7a7a\u76f4\u63a5\u4f7f\u7528get\u53d6\u503c\u65f6\uff0c\u4f1a\u89e6\u53d1get\u4e2d<strong>NoSuchElementException<\/strong>\u5f02\u5e38\uff1b<\/p>\n<p><strong>\u518d\u6b21<\/strong>\uff0corElseThrow()\u65b9\u6cd5\u4e5f\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u4f46\u662f\u8fd9\u79cd\u5f02\u5e38\u5c5e\u4e8e<strong>\u4eba\u4e3a\u6307\u5b9a<\/strong>\u7684\u5f02\u5e38\uff0c\u662f\u4e3a\u4e86\u4f7f\u5f97\u5f02\u5e38\u60c5\u51b5\u7684\u8bed\u4e49\u66f4\u52a0\u4e30\u5bcc\uff0c\u800c\u4eba\u4e3a\u8bbe\u7f6e\u7684\uff0c\u662f\u4e00\u79cd\u53ef\u63a7\u7684\u5f02\u5e38\uff1b<\/p>\n<p><strong>\u6700\u540e<\/strong>\uff0c\u5728\u4e00\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u8bbe\u7f6e\u4e86\u53c2\u6570\u975e\u7a7a\u68c0\u67e5\uff08Objects.requireNonNull()\uff09\uff0c\u8fd9\u79cd\u68c0\u67e5\u4f1a\u629b\u51fa<strong>NPE<\/strong>\u5f02\u5e38\uff0c\u9664\u53bb\u5df2\u7ecf\u63d0\u5230\u7684\u5e26\u53c2\u6784\u9020\u5668\uff0c\u8fd8\u6709filter\u3001map\u3001flatMap\u3001or\u8fd9\u56db\u4e2a\u65b9\u6cd5\uff0c\u5982\u679c\u4f20\u5165\u7684\u63a5\u53e3\u5b9e\u4f8b\u662fNull\u503c\u5c31\u4f1a\u968f\u65f6\u5f15\u7206NPE\u3002<\/p>\n<h2>4\u3001\u8bef\u7528\u5f62\u5f0f\u4e0eBest practice<\/h2>\n<h3>1\uff09\u8bef\u7528\u5f62\u5f0f<\/h3>\n<p>a\u3001\u521d\u59cb\u5316\u4e3anull<\/p>\n<p>\u7b2c\u4e00\u79cd\u8bef\u7528\u5f62\u5f0f\u662f\u7ed9Optional\u7c7b\u578b\u7684\u53d8\u91cf\u521d\u59cb\u5316\u7684\u65f6\u5019.Optional\u7c7b\u578b\u53d8\u91cf\u662f\u9ed8\u8ba4\u4e0d\u4e3a\u7a7a\u7684\uff0c\u6240\u4ee5\u5728\u53d6\u65b9\u6cd5\u6267\u884c\u7684\u65f6\u5019\u624d\u53ef\u4ee5\u8086\u65e0\u5fcc\u60ee&#8221;\u70b9&#8221;\u51fa\u53bb\uff0c\u5982\u679c\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019\u51fa\u73b0\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nOptional&lt;Chestnut&gt; chest = null;<\/code><\/pre>\n<p>\u5e76\u4e14\u4e0d\u53ca\u65f6\u4e3achest\u8d4b\u503c\uff0c\u5219\u8fd8\u662f\u5bb9\u6613\u51fa\u73b0NPE\uff0c\u6b63\u786e\u7684\u521d\u59cb\u5316\u65b9\u5f0f\u5e94\u8be5\u662f\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nOptional&lt;Chestnut&gt; chest = Optional.empty();<\/code><\/pre>\n<p>b\u3001\u7b80\u5355\u5224\u7a7a<\/p>\n<p>\u7b2c\u4e8c\u79cd\u6bd4\u8f83\u5e38\u89c1\u7684\u8bef\u7528\u5f62\u5f0f\u5e94\u8be5\u662f\u4f7f\u7528isPresent()\u505a\u7b80\u5355\u5224\u7a7a\u3002\u539f\u672c\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic String getName(Chestnut chestnut){\n    if(chestnut == null){\n        return &quot;\u6817\u5b50\u4e0d\u5b58\u5728&quot;;\n    }else return chestnut.name();\n}<\/code><\/pre>\n<p>\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u68c0\u67e5chestnut == null\u6765\u5904\u7406\u4e3a\u7a7a\u65f6\u7684\u60c5\u51b5\uff0c\u7b80\u5355\u4f7f\u7528isPresent()\u65b9\u6cd5\u5224\u7a7a\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic String getName(Chestnut chestnut){\n    Optional&lt;Chestnut&gt; opChest = Optional.ofNullable(chestnut);\n    if(!opChest.isPresent()){\n        return &quot;\u6817\u5b50\u4e0d\u5b58\u5728&quot;;\n    }else return opChest.getname();\n}<\/code><\/pre>\n<p>\u9171\u5a76\u513f\u5e76\u6ca1\u6709\u592a\u5927\u5dee\u522b\uff0c\u6240\u4ee5\u5728\u4f7f\u7528Optional\u65f6\uff0c\u9996\u5148\u5e94\u907f\u514d\u4f7f\u7528<code>**Optional.isPresent()**<\/code>\u6765\u68c0\u67e5\u5b9e\u4f8b\u662f\u5426\u5b58\u5728\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u5f0f\u548c<code>**null!= obj**<\/code>\u6ca1\u6709\u533a\u522b\u4e5f\u6ca1\u4ec0\u4e48\u610f\u4e49\u3002<\/p>\n<p>c\u3001\u7b80\u5355get<\/p>\n<p>\u7b2c\u4e09\u79cd\u6bd4\u8f83\u5e38\u89c1\u7684\u8bef\u7528\u5f62\u5f0f\u662f\u4f7f\u7528Optional.get()\u65b9\u5f0f\u6765\u83b7\u53d6Optional\u4e2dvalue\u7684\u503c\uff0cget()\u65b9\u6cd5\u4e2d\u5bf9value==null\u7684\u60c5\u51b5\u6709\u629b\u51fa\u5f02\u5e38\uff0c\u6240\u4ee5\u5e94\u8be5\u5728\u505a\u5b8c\u975e\u7a7a\u6821\u9a8c\u4e4b\u540e\u518d\u4eceget\u53d6\u503c\uff0c\u6216\u8005\u5341\u5206\u786e\u5b9avalue\u4e00\u5b9a\u4e0d\u4e3a\u7a7a\uff0c\u5426\u5219\u4f1a\u51fa\u73b0NoSuchElementException\u7684\u5f02\u5e38\u3002\u76f8\u5bf9\u7684\uff0c\u5982\u679c\u4e0d\u662f\u5f88\u786e\u4fe1\uff0c\u5219\u4f7f\u7528orElse()\uff0corElseGet()\uff0corElseThrow()\u83b7\u5f97\u4f60\u7684\u7ed3\u679c\u4f1a\u66f4\u52a0\u5408\u9002\u3002<\/p>\n<p>d\u3001\u4f5c\u4e3a\u5c5e\u6027\u5b57\u6bb5\u548c\u65b9\u6cd5\u53c2\u6570<\/p>\n<p>\u7b2c\u56db\u79cd\u8bef\u7528\u5f62\u5f0f\u5728\u521d\u5b66Optional\u7684\u65f6\u5019\u5bb9\u6613\u78b0\u5230\uff0c\u5f53\u6307\u5b9a\u67d0\u4e2a\u7c7b\u4e2d\u7684\u5c5e\u6027\uff0c\u6216\u8005\u65b9\u6cd5\u7684\u53c2\u6570\u4e3aOptional\u7684\u65f6\u5019\uff0cidea\u4f1a\u7ed9\u51fa\u5982\u4e0b\u63d0\u793a\uff1a<\/p>\n<p>Reports any uses of\u00a0<strong>java.util.Optional\/&lt;T&gt;<\/strong>,\u00a0<strong>java.util.OptionalDouble<\/strong>,\u00a0<strong>java.util.OptionalInt<\/strong>,\u00a0<strong>java.util.OptionalLong<\/strong>or\u00a0<strong>com.google.common.base.Optional<\/strong>as the type for a field or parameter. Optional was designed to provide a limited mechanism for library method return types where there needed to be a clear way to represent &#8220;no result&#8221;. Using a field with type\u00a0<strong>java.util.Optional<\/strong>is also problematic if the class needs to be\u00a0<strong>Serializable<\/strong>, which\u00a0<strong>java.util.Optional<\/strong>is not.<\/p>\n<p>\u5927\u610f\u662f\u4e0d\u5efa\u8bae\u5982\u6b64\u4f7f\u7528Optional\u3002\u7b2c\u4e00\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528Optional\u4f5c\u4e3a\u5b57\u6bb5\u6216\u53c2\u6570\uff0c\u5176\u8bbe\u8ba1\u662f\u4e3a\u5e93\u65b9\u6cd5<strong>\u8fd4\u56de\u7c7b\u578b<\/strong>\u63d0\u4f9b\u4e00\u79cd\u6709\u9650\u7684\u673a\u5236\uff0c\u800c\u8fd9\u79cd\u673a\u5236\u53ef\u4ee5\u6e05\u6670\u7684\u8868\u793a\u201c\u6ca1\u6709\u7ed3\u679c\u201d\u7684\u8bed\u4e49\uff1b\u7b2c\u4e8c\uff0cOptional\u6ca1\u6709\u5b9e\u73b0Serilazable\uff0c\u662f\u4e0d\u53ef\u88ab\u5e8f\u5217\u5316\u7684\u3002<\/p>\n<p>\u8fd9\u79cd\u8bef\u7528\u65b9\u6cd5\u6bd4\u8f83\u660e\u663e\uff0c\u590d\u73b0\u548c\u907f\u514d\u4e5f\u6bd4\u8f83\u7b80\u5355\u3002\u4f46\u7b14\u8005\u8fd8\u60f3\u5c31\u8fd9\u4e24\u4e2a\u5efa\u8bae\u7684\u539f\u56e0\u505a\u8fdb\u4e00\u6b65\u7684\u63a2\u7a76\uff0c\u6240\u4ee5\u67e5\u9605\u4e86\u4e00\u4e9b\u8d44\u6599\uff0c\u5927\u4f53\u7684\u539f\u56e0\u5982\u4e0b\uff1a<\/p>\n<p><strong>\u7b2c\u4e00\u4e2a\u539f\u56e0<\/strong>\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u9002\u5408\u505a\u5c5e\u6027\u5b57\u6bb5\u548c\u65b9\u6cd5\u53c2\u6570\uff1f\u76f4\u767d\u7684\u8bf4\uff0c\u5c31\u662f\u9ebb\u70e6\u3002\u4e3a\u4e86\u5f15\u5165Optional\uff0c\u5374\u9700\u8981\u52a0\u5165\u591a\u6bb5\u6837\u677f\u4ee3\u7801\uff0c\u6bd4\u5982\u5224\u7a7a\u64cd\u4f5c\u3002\u4f7f\u5f97\u5728\u4e0d\u5408\u9002\u7684\u4f4d\u7f6e\u4f7f\u7528Optional\u4e0d\u4ec5\u6ca1\u6709\u7ed9\u6211\u4eec\u5e26\u6765\u4fbf\u5229\uff0c\u53cd\u800c\u7ea6\u675f\u4e86\u5199\u4ee3\u7801\u7684\u903b\u8f91\u3002<\/p>\n<p>\u5199\u4ee5\u4e0b\u57df\u6a21\u578b\u4ee3\u7801<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic class Chestnut {\n  private String firstName;\n  private Optional&lt;String&gt; midName = Optional.empty();\n  private String lastName;\n  public void setMidName(Optional&lt;String&gt; midName) {\n    this.midName = midName;\n  }\n  public String getFullName() {\n    String fullName = firstName;\n    if(midName != null) {\n      if(midName.isPresent()){\n        fullName = fullName.concat(&quot;.&quot; + midName.get());\n      }\n      return fullName.concat(&quot;.&quot; + lastName);\n    }\n  }\n}<\/code><\/pre>\n<p>\u53ef\u89c1\u5728setter\u65b9\u6cd5\u4e2d\u6ca1\u6709\u5bf9\u5f62\u53c2\u505a\u76f8\u5e94\u7684\u6821\u9a8c\uff0c\u90a3\u4e48\u5219\u9700\u8981\u5728\u4f7f\u7528\u7684getFullName()\u65b9\u6cd5\u4e2d\uff0c\u589e\u52a0\u5bf9\u5c5e\u6027midName\u7684\u5224\u7a7a\u64cd\u4f5c\uff0c\u56e0\u4e3a\u5b8c\u5168\u53ef\u80fd\u901a\u8fc7setter\u65b9\u6cd5\u4f7f\u5f97\u5c5e\u6027\u4e3anull\u3002\u5982\u679c\u628a\u5224\u7a7a\u79fb\u5230setter\u65b9\u6cd5\u4e2d\uff0c\u4e5f\u5e76\u6ca1\u6709\u51cf\u5c11\u5224\u7a7a\uff0c\u4f7f\u5f97\u5e73\u767d\u6324\u8fdb\u4e86\u4e00\u6bb5\u6837\u677f\u4ee3\u7801\u3002\u53e6\u5916\u5728\u4f20\u5165\u65b9\u6cd5\u65f6\uff0c\u4e5f\u9700\u8981\u5bf9\u539f\u672c\u7684value\u5305\u88c5\u4e00\u5c42\u540e\u518d\u4f20\u5165\uff0c\u4f7f\u5f97\u4ee3\u7801\u663e\u5f97\u7d2f\u8d58\u4e86\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nchest.setMidName(Optional.empty());\nchest.setMidName(Optional.of(&quot;\u963f\u6817&quot;));<\/code><\/pre>\n<p>\u5728\u5c5e\u6027\u4e0d\u4e3aOptional\u7684\u65f6\u5019\uff0c\u5982\u679c\u7ed9\u5c5e\u6027\u8d4b\u503c\uff0c\u9700\u8981\u4f7f\u7528\u201c\u6d88\u8d39\u201d\u64cd\u4f5c\uff0c\u6bd4\u5982orElse()\uff0c\u53d6\u51fa\u503c\u518d\u8d4b\u7ed9\u5c5e\u6027\uff0c\u76f8\u6bd4\u76f4\u63a5\u4f20\u5165String\u7c7b\u578b\u7684\u503c\u4f5c\u4e3a\u5b57\u6bb5\u548c\u5f62\u53c2\u53ef\u4ee5\u51cf\u5c11\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u540e\u8005\u53cd\u800c\u66f4\u52a0\u5408\u9002\u3002<\/p>\n<p><strong>\u7b2c\u4e8c\u4e2a\u539f\u56e0<\/strong>\uff0c\u4e3a\u4ec0\u4e48\u6ca1\u6709\u5b9e\u73b0\u5e8f\u5217\u5316\uff1f\u76f8\u5173\u53ef\u4ee5\u53c2\u89c1Java Lamda\u7684\u4e13\u5bb6\u7ec4<a href=\"https:\/\/link.segmentfault.com\/?enc=QFsETWk4PS9RjHnl7G6D1g%3D%3D.7AHlVDxgv7oC%2BEjgASFtwlT2h6v4JtGhwDf8JUMSu3cKxfqBA7b3Yyc%2FQ0PH4RJIfqUoSdfye0htmaUK6ugimRDyAKlWcSkWM%2BuaPatIrq0%3D\" rel=\"nofollow\">\u8ba8\u8bba<\/a>\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042752316\" alt=\"up-1cdaf1083af5f8ce092b4f0fe942efd0d92.png\" title=\"up-1cdaf1083af5f8ce092b4f0fe942efd0d92.png\" loading=\"lazy\"><\/p>\n<p>JDK\u5728\u5e8f\u5217\u5316\u4e0a\u6bd4\u8f83\u7279\u6b8a\uff0c\u9700\u8981\u540c\u65f6\u517c\u987e\u5411\u524d\u548c\u5411\u540e\u517c\u5bb9\uff0c\u6bd4\u5982\u5728JDK7\u4e2d\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\u5e94\u8be5\u80fd\u591f\u5728JDK8\u4e2d\u53cd\u5e8f\u5217\u5316\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u5e76\u4e14\uff0c\u5e8f\u5217\u5316\u4f9d\u8d56\u4e8e\u5bf9\u8c61\u7684identity\u4fdd\u6301\u552f\u4e00\u6027\u3002\u5f53\u524dOptional\u662f\u5f15\u7528\u7c7b\u578b\u7684\uff0c\u4f46\u5176\u88ab\u6807\u8bb0\u4e3avalue-based class\uff08\u57fa\u4e8e\u503c\u7684\u7c7b\uff09\uff0c\u5e76\u4e14\u6709\u8ba1\u5212\u5728\u4eca\u540e\u7684\u67d0\u4e00\u4e2aJDK\u7248\u672c\u4e2d\u5b9e\u73b0\u4e3avalue-based class\uff0c\u53ef\u89c1\u4e0a\u56fe\u3002\u5982\u679c\u88ab\u8bbe\u8ba1\u4e3a\u53ef\u5e8f\u5217\u5316\uff0c\u5c31\u5c06\u51fa\u73b0\u4e24\u4e2a\u77db\u76fe\u70b9\uff1a1\uff09\u5982\u679cOptional\u53ef\u5e8f\u5217\u5316\uff0c\u5c31\u4e0d\u80fd\u5c06Optional\u5b9e\u73b0\u4e3avalue-based class\uff0c\u800c\u5fc5\u987b\u662f\u5f15\u7528\u7c7b\u578b\uff0c2\uff09\u5426\u5219\u5c06value-based class\u52a0\u5165\u540c\u4e00\u6027\u7684\u654f\u611f\u64cd\u4f5c\uff08\u5305\u542b\u5f15\u7528\u7684\u76f8\u7b49\u6027\u5982:==\uff0c\u540c\u4e00\u6027\u7684hashcode\u6216\u8005\u540c\u6b65\u7b49\uff09\uff0c\u4f46\u662f\u8fd9\u4e2a\u4e0e\u5f53\u524d\u5df2\u53d1\u5e03\u7684JDK\u7248\u672c\u90fd\u662f\u51b2\u7a81\u7684\u3002\u6240\u4ee5\u7efc\u4e0a\uff0c\u8003\u8651\u5230\u672a\u6765JDK\u7684\u89c4\u5212\u548c\u5b9e\u73b0\u7684\u51b2\u7a81\uff0c\u4e00\u5f00\u59cb\u5c31\u5c06Optional\u8bbe\u7f6e\u4e3a\u4e0d\u53ef\u5e8f\u5217\u5316\u7684\uff0c\u5e94\u8be5\u662f\u6700\u5408\u9002\u7684\u65b9\u6848\u4e86\u3002<\/p>\n<p>Value-Based Classes\uff08\u57fa\u4e8e\u503c\u7684\u7c7b\uff09\uff0c\u4ee5\u4e0b\u662f\u6765\u81eaJava <a href=\"https:\/\/link.segmentfault.com\/?enc=Ao9n1pSPlDsVUDSvK6ScPQ%3D%3D.TYjTRT5WVcbU9Wc1xlBP%2BVSgrtSv5qQUrWkTHZ0%2F3RKHs0IcXCuhp4C0B1cLYTa6\" rel=\"nofollow\">doc<\/a>\u7684\u89e3\u91ca\uff1a<\/p>\n<p>Value-based Classes<\/p>\n<p>Some classes, such as\u00a0<code>java.util.Optional<\/code>and\u00a0<code>java.time.LocalDateTime<\/code>, are\u00a0value-based. Instances of a value-based class:<\/p>\n<p>1\u3001are final and immutable (though may contain references to mutable objects);<\/p>\n<p>2\u3001have implementations of\u00a0<code>equals<\/code>,\u00a0<code>hashCode<\/code>, and\u00a0<code>toString<\/code>which are computed solely from the instance&#8217;s state and not from its identity or the state of any other object or variable;<\/p>\n<p>3\u3001make no use of identity-sensitive operations such as reference equality (<code>==<\/code>) between instances, identity hash code of instances, or synchronization on an instances&#8217;s intrinsic lock;<\/p>\n<p>4\u3001are considered equal solely based on\u00a0<code>equals()<\/code>, not based on reference equality (<code>==<\/code>);<\/p>\n<p>5\u3001do not have accessible constructors, but are instead instantiated through factory methods which make no committment as to the identity of returned instances;<\/p>\n<p>6\u3001are\u00a0freely substitutable\u00a0when equal, meaning that interchanging any two instances\u00a0<code>x<\/code>and\u00a0<code>y<\/code>that are equal according to\u00a0<code>equals()<\/code>in any computation or method invocation should produce no visible change in behavior.<\/p>\n<p>A program may produce unpredictable results if it attempts to distinguish two references to equal values of a value-based class, whether directly via reference equality or indirectly via an appeal to synchronization, identity hashing, serialization, or any other identity-sensitive mechanism. Use of such identity-sensitive operations on instances of value-based classes may have unpredictable effects and should be avoided.<\/p>\n<p>2\uff09Best practice<\/p>\n<p>\u5b9e\u8df5\u4e2d\u5e38\u5e38\u7ec4\u5408\u4f7f\u7528\u4ee5\u4e0a\u5404\u79cd\u65b9\u6cd5\uff0c\u4e14\u5f88\u591a\u65b9\u6cd5\u5e38\u4e0eLambda\u8868\u8fbe\u5f0f\u7ed3\u5408\uff0c\u83b7\u53d6\u60f3\u8981\u7684\u7ed3\u679c\uff0c\u8fd9\u91cc\u5217\u51fa\u4e24\u4e2a\u5e38\u89c1\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c<strong>\u503c\u7c7b\u578b\u8f6c\u6362<\/strong>\u548c<strong>\u96c6\u5408\u5143\u7d20\u8fc7\u6ee4<\/strong>\u3002<\/p>\n<p>a\u3001\u793a\u4f8b\u4e00<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nChestnut chestnut = new Chestnut(&quot;\u9525\u6817\u677f\u6817&quot;);\nif(chestnut != null){\n  String chestName = chestnut.getName();\n  if(chestName != null){\n    return chestName.concat(&quot;\u597d\u597d\u5403\uff01&quot;);\n  }\n}else{\n  return null;\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u7b80\u5316\u6210\uff1a<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\nChestnut chestnut = new Chestnut(&quot;\u9525\u6817\u677f\u6817&quot;);\nreturn Optional.ofNullable(chestnut)\n              .map(Chestnut::getName)\n              .map(name-&gt;name.concat(&quot;\u597d\u597d\u5403\uff01&quot;))\n              .orElse(null);<\/code><\/pre>\n<p>b\u3001\u793a\u4f8b\u4e8c<\/p>\n<pre><code>P lainJavascriptJavaHTML\/XMLMarkdownMakefileGoJSONSQLObjective-cYAMLBashPHPPython\npublic static void main(String[] args) {\n  \/\/ \u521b\u5efa\u4e00\u4e2a\u6817\u5b50\u96c6\u5408\n  List&lt;Chestnut&gt; chestList = new ArrayList&lt;&gt;();\n  \/\/ \u521b\u5efa\u51e0\u4e2a\u6817\u5b50\n  Chestnut chest1 = new Chestnut(&quot;abc&quot;);\n  Chestnut chest2 = new Chestnut(&quot;efg&quot;);\n  Chestnut chest3 = null;\n  \/\/ \u5c06\u6817\u5b50\u52a0\u5165\u96c6\u5408\n  chestList.add(chest1);\n  chestList.add(chest2);\n  chestList.add(chest3);\n  \/\/ \u521b\u5efa\u7528\u4e8e\u5b58\u50a8\u6817\u5b50\u540d\u7684\u96c6\u5408\n  List&lt;String&gt; nameList = new ArrayList();\n  \/\/ \u5faa\u73af\u6817\u5b50\u5217\u8868\u83b7\u53d6\u6817\u5b50\u4fe1\u606f\uff0c\u503c\u83b7\u53d6\u4e0d\u4e3a\u7a7a\u4e14\u6817\u5b50\u540d\u4ee5\u2018a\u2019\u5f00\u5934\n  \/\/ \u5982\u679c\u4e0d\u7b26\u5408\u6761\u4ef6\u5c31\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u6700\u540e\u5c06\u7b26\u5408\u6761\u4ef6\u7684\u6817\u5b50\u540d\u52a0\u5165\u6817\u5b50\u540d\u96c6\u5408\n  for (Chestnut chest : chestList) {\n      nameList.add(Optional.ofNullable(chest)\n             .map(Chestnut::getName)\n             .filter(value -&gt; value.startsWith(&quot;a&quot;))\n             .orElse(&quot;\u672a\u586b\u5199&quot;));\n  }\n  \/\/ \u8f93\u51fa\u6817\u5b50\u540d\u96c6\u5408\u4e2d\u7684\u503c\n  System.out.println(&quot;\u901a\u8fc7 Optional \u8fc7\u6ee4\u7684\u96c6\u5408\u8f93\u51fa\uff1a&quot;);\n  nameList.stream().forEach(System.out::println);\n}<\/code><\/pre>\n<h3>5\u3001\u603b\u7ed3<\/h3>\n<p>\u672c\u6587<strong>\u9996\u5148\uff0c<\/strong>\u4ece\u6240\u89e3\u51b3\u7684\u95ee\u9898\u5f00\u59cb\uff0c\u4ecb\u7ecd\u4e86\u5f53\u524dNPE\u5904\u7406\u6240\u9047\u5230\u7684\u95ee\u9898\uff1b<strong>\u7136\u540e<\/strong>\uff0c\u5206\u7c7b\u5730\u4ecb\u7ecd\u4e86Optional\u7c7b\u4e2d\u7684\u65b9\u6cd5\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u793a\u4f8b\uff1b<strong>\u63a5\u7740<\/strong>\uff0c\u4ece\u6e90\u7801\u5c42\u9762\u5bf9\u51e0\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\u8fdb\u884c\u4e86\u5bf9\u6bd4\uff1b<strong>\u6700\u540e<\/strong>\uff0c\u5217\u4e3e\u51fa\u4e86\u51e0\u4e2a\u5e38\u89c1\u7684\u8bef\u7528\u5f62\u5f0f\u548cBest practice\uff0c\u7ed3\u675f\u4e86\u5168\u6587\u3002<\/p>\n<p>Optional\u7c7b\u5177\u6709\uff1a\u53ef\u4ee5\u663e\u5f0f\u4f53\u73b0\u503c\u53ef\u80fd\u4e3a\u7a7a\u7684\u8bed\u4e49\u548c\u9690\u85cf\u53ef\u80fd\u5b58\u5728\u7a7a\u6307\u9488\u7684\u4e0d\u786e\u5b9a\u6027\u7684<strong>\u4f18\u70b9<\/strong>\uff0c\u4f46\u662f\u540c\u65f6\u4e5f\u5177\u6709\uff0c\u9002\u7528\u8303\u56f4\u4e0d\u662f\u5f88\u5e7f\uff08\u5efa\u8bae\u4f7f\u7528\u4e8e\u8fd4\u56de\u503c\u548cNPE\u903b\u8f91\u5904\u7406\uff09\u4ee5\u53ca\u4f7f\u7528\u65f6\u9700\u8981\u66f4\u591a\u8003\u91cf\u7684<strong>\u7f3a\u70b9<\/strong>\u3002<\/p>\n<p>\u4f46\u662f\u603b\u4f53\u770b\u6765\uff0cOptional\u7c7b\u662f\u4f34\u968fJava8\u51fd\u6570\u5f0f\u7f16\u7a0b\u51fa\u73b0\u7684\u4e00\u9879\u65b0\u7279\u6027\u3002\u4e3a\u5904\u7406\u903b\u8f91\u7684\u5b9e\u73b0\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u9009\u62e9\uff0c\u672a\u6765\u671f\u5f85\u66f4\u591a\u7684\u5b9e\u8df5\u548cbest practice\u51fa\u73b0\uff0c\u4e3aOptional\u5e26\u6765\u66f4\u591a\u51fa\u573a\u7684\u673a\u4f1a\u3002<\/p>\n<p>6\u3001\u53c2\u8003<\/p>\n<p>[1]<a href=\"https:\/\/link.segmentfault.com\/?enc=G0MWAWDSdnWoxtKFLRNn0w%3D%3D.9YO5eDeUvyiZDeIyUnpqea07OETmqjkczcrWpZ6hkHjBt6LUC8IP2lsQwGFFLKswm8ffKzP4l0PwCt%2BcCyXGvA%3D%3D\" rel=\"nofollow\">https:\/\/www.runoob.com\/java\/java8-optional-class.html<\/a>\u6765\u6e90:\u83dc\u9e1f\u6559\u7a0b<\/p>\n<p>[2] <a href=\"https:\/\/link.segmentfault.com\/?enc=cm0cZAAUmQiQgNUqeIFDrg%3D%3D.2B1%2BR3EoWX88C7rUK7NhzmMCZmo5ph7lmQKO3arcpkyC7wfrHvmRGMK684NM7AGGzMF9ym04ajJPtNn2jWAA4w%3D%3D\" rel=\"nofollow\">https:\/\/blog.csdn.net\/qq\/_40741855\/article\/details\/103251436<\/a>\u6765\u6e90:CSDN<\/p>\n<p>[3] <a href=\"https:\/\/link.segmentfault.com\/?enc=VrzdzUIcMXFnwZfxG7aTbA%3D%3D.d5s3dB10Z0yWNJkou2gQlg%2BjCcJKJWXOvCiDsIkrifH4JKOPYIDx3MwQyAximw%2Fv3BkK%2F5kBRTpu6wfsZnifx0YSymysid3PuBUlvkhs9RM%3D\" rel=\"nofollow\">https:\/\/yanbin.blog\/java8-optional-several-common-incorrect-usages\/#more-8824<\/a>\u6765\u6e90:blog<\/p>\n<p>[4] <a href=\"https:\/\/link.segmentfault.com\/?enc=5NpF1QNfaG74mUqyuQ6Lew%3D%3D.9KMNG1TYOr1ApKQlZ8egLJJngNpwfcj4X%2F4AGpm3b%2Bif5NvryYyKWCrBRvUAyZ7sgWnu5DubvdNZjEJ2wI7G9U07IC2JZ1dVK25J2RRKVG2lj%2BTC%2FsntFQkztQTZuNCmgB%2B1mwD8PdatngkMzg52dw%3D%3D\" rel=\"nofollow\">https:\/\/www.javaspecialists.eu\/archive\/Issue238-java.util.Optional&#8212;Short-Tutorial-by-Example.html<\/a>\u6765\u6e90:java specialists<\/p>\n<p>[5] <a href=\"https:\/\/link.segmentfault.com\/?enc=jHvUbRNAT0QyReIU6myOTA%3D%3D.fUeialTuClkefLHcaKb8mD5Z%2BRceXAFaSpqTPRDbXz7TO9Hh2yLOQ9Khcvf%2FhaA7\" rel=\"nofollow\">Java\u6838\u5fc3\u6280\u672f \u5377II<\/a>&#8211; Java8\u7684\u6d41\u5e93 &#8211; Optional\u7c7b\u578b<\/p>\n<p>[6] <a href=\"https:\/\/link.segmentfault.com\/?enc=3ql%2FKZbZNorJh2Fe%2Bf5tEw%3D%3D.BcQLyFJrectsPIF8Tt3LmInO4IoExN9gxogkN66pfhk6bSUKcBU0RBKU328WEVePTAYvQMqLwat2T9TKoQfDFA%3D%3D\" rel=\"nofollow\">https:\/\/www.zhihu.com\/question\/444199629\/answer\/1729637041<\/a>\u6765\u6e90:\u77e5\u4e4e<\/p>\n<p>\u5982\u6709\u4e0d\u5f53\u4e4b\u5904\uff0c\u671b\u6307\u6b63\uff5e<\/p>\n<p><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042752268\">https:\/\/segmentfault.com\/a\/1190000042752268<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0\uff1aOptional\u6700\u65e9\u662fGoogle\u516c\u53f8Guava\u4e2d\u7684\u6982\u5ff5\uff0c\u4ee3\u8868\u7684\u662f\u53ef\u9009\u503c\u3002Optional\u7c7b\u4eceJava8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006823","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006823"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006823\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}