{"id":10006635,"date":"2022-09-29T19:06:13","date_gmt":"2022-09-29T11:06:13","guid":{"rendered":"https:\/\/ityet.com\/?p=10006635"},"modified":"2022-09-29T19:06:13","modified_gmt":"2022-09-29T11:06:13","slug":"optional%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90%e4%b8%8e%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006635","title":{"rendered":"Optional\u6e90\u7801\u89e3\u6790\u4e0e\u5b9e\u8df5"},"content":{"rendered":"<h3>1 \u5bfc\u8bfb<\/h3>\n<p>NullPointerException\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u9047\u5230\uff0c\u7a0d\u6709\u4e0d\u614e\u5c0fBUG\u5c31\u51fa\u73b0\u4e86\uff0c\u5982\u679c\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u5462\uff0cOptional\u5c31\u662f\u4e13\u95e8\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u7c7b\uff0c\u90a3\u4e48Optional\u5982\u4f55\u4f7f\u7528\u5462\uff1f\u8ba9\u6211\u4eec\u4e00\u8d77\u63a2\u7d22\u4e00\u4e0b\u5427\uff01<\/p>\n<h3>2 \u6e90\u7801\u89e3\u6790<\/h3>\n<h4>2.1 Optional\u5b9a\u4e49<\/h4>\n<p>Optional\u7c7b\u662fJava8\u4e3a\u4e86\u89e3\u51b3null\u503c\u5224\u65ad\u95ee\u9898\u800c\u521b\u5efa\u7684\u5bb9\u5668\u7c7b\uff0c\u5728java.util \u4e0b,\u4f7f\u7528Optional\u7c7b\u53ef\u4ee5\u907f\u514d\u663e\u5f0f\u7684null\u503c\u5224\u65ad\uff0c\u907f\u514dnull\u5bfc\u81f4\u7684NullPointerException\u3002\u9996\u5148\uff0cOptional\u662f\u4e00\u4e2a\u5bb9\u5668\uff0c\u5b83\u53ef\u4ee5\u4fdd\u5b58\u7c7b\u578bT\u7684\u503c\uff0c\u4e5f\u53ef\u4ee5\u4e3anull\u7684\u5bb9\u5668\u5bf9\u8c61\u3002Optional\u5bb9\u5668\u53ea\u80fd\u5b58\u4e00\u4e2a\u503c\u3002<\/p>\n<h4>2.2 Optional\u7684\u5c5e\u6027<\/h4>\n<p>1\uff09\u6e90\u7801\uff1a<\/p>\n<pre><code>\/**\n * Common instance for {@code empty()}.\n *\/\nprivate static final Optional&lt;?&gt; EMPTY = new Optional&lt;&gt;();\n \n \n\/**\n * If non-null, the value; if null, indicates no value is present\n *\/\nprivate final T value;<\/code><\/pre>\n<p>\u6839\u636e\u6e90\u7801\u53ef\u4ee5\u770b\u5230Optional\u6709\u4e24\u4e2a\u5c5e\u6027\uff0c\u4e00\u4e2a\u662f\u4e3a\u7a7a\u503c\u51c6\u5907\u7684EMPTY\u548c\u6cdb\u578b\u503cvalue;<\/p>\n<h4>2.3 Optional\u7684\u65b9\u6cd5<\/h4>\n<p>Optional\u9664toString()\u3001hashCode() \u3001equals()\u7b49Object\u7684\u65b9\u6cd5\u5916\uff0c\u8fd8\u5305\u542b\u4ee5\u4e0b\u65b9\u6cd5\u3002<\/p>\n<p><strong>2.3.1 \u79c1\u6709\u6784\u9020\u65b9\u6cd5<\/strong><\/p>\n<pre><code>\/**\n * Constructs an empty instance.\n *\n * @implNote Generally only one empty instance, {@link Optional#EMPTY},\n * should exist per VM.\n *\/\nprivate Optional() {\n    this.value = null;\n}\n \n \n\/**\n* Constructs an instance with the value present.\n*\n* @param value the non-null value to be present\n* @throws NullPointerException if value is null\n*\/\nprivate Optional(T value) {\n    this.value = Objects.requireNonNull(value);\n}<\/code><\/pre>\n<p>\u5206\u522b\u662f\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b9e\u4f8b\u548c\u6784\u9020\u4e00\u4e2a\u5177\u6709\u5f53\u524d\u503c\u7684\u5b9e\u4f8b\u3002<\/p>\n<p><strong>2.3.2 \u521b\u5efa\u65b9\u6cd5<\/strong><br \/>1\uff09\u6e90\u7801<\/p>\n<pre><code>\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}\npublic static &lt;T&gt; Optional&lt;T&gt; of(T value) {\n     return new Optional&lt;&gt;(value);\n}\npublic static &lt;T&gt; Optional&lt;T&gt; ofNullable(T value) {\n     return value == null ? empty() : of(value);\n}<\/code><\/pre>\n<p>2\uff09\u65b9\u6cd5\u8bf4\u660e<\/p>\n<ul>\n<li>empty(\uff09: \u521b\u5efa\u4e00\u4e2a\u7a7a\u7684 Optional \u5b9e\u4f8b<\/li>\n<li>of(T t) : \u521b\u5efa\u4e00\u4e2a Optional \u5b9e\u4f8b\uff0c\u5f53 t\u4e3anull\u65f6\u629b\u51fa\u5f02\u5e38<\/li>\n<li>ofNullable(T t): \u521b\u5efa\u4e00\u4e2a Optional \u5b9e\u4f8b\uff0c\u4f46\u5f53 t\u4e3anull\u65f6\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684\u5b9e\u4f8b<\/li>\n<\/ul>\n<p>3\uff09\u6d4b\u8bd5\u4ee3\u7801<\/p>\n<pre><code>\npublic static void main(String[] args) {\n    Integer value1 = null;\n    Integer value2 = 1;\n    try {\n        Optional&lt;Integer&gt; optional1 = Optional.empty();\n        System.out.println(&quot;optional1\u521b\u5efa\u4e86&quot;);\n    }catch (Exception e){\n        System.out.println(&quot;optional1\u5931\u8d25\u4e86&quot;);\n    }\n    try {\n        Optional&lt;Integer&gt; optional2 = Optional.of(value1);\n        System.out.println(&quot;optional2\u521b\u5efa\u4e86&quot;);\n    }catch (Exception e){\n        System.out.println(&quot;optional2\u5931\u8d25\u4e86&quot;);\n    }\n    try {\n        Optional&lt;Integer&gt; optional3 = Optional.ofNullable(value1);\n        System.out.println(&quot;optional3\u521b\u5efa\u4e86&quot;);\n    }catch (Exception e){\n        System.out.println(&quot;optional3\u5931\u8d25\u4e86&quot;);\n    }\n    try {\n        Optional&lt;Integer&gt; optional4 = Optional.of(value2);\n        System.out.println(&quot;optional4\u521b\u5efa\u4e86&quot;);\n    }catch (Exception e){\n        System.out.println(&quot;optional4\u5931\u8d25\u4e86&quot;);\n    }\n    try {\n        Optional&lt;Integer&gt; optional5 = Optional.ofNullable(value2);\n        System.out.println(&quot;optional5\u521b\u5efa\u4e86&quot;);\n    }catch (Exception e){\n        System.out.println(&quot;optional5\u5931\u8d25\u4e86&quot;);\n    }\n}\n<\/code><\/pre>\n<p>4\uff09\u8fd0\u884c\u7ed3\u679c<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2J6u\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p><strong>2.3.3 \u503c\u83b7\u53d6\u65b9\u6cd5<\/strong><br \/>1\uff09\u6e90\u7801<\/p>\n<pre><code>\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>2\uff09\u65b9\u6cd5\u8bf4\u660e<\/p>\n<p>get(): \u5982\u679cOptional\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de\u8be5Optional\u5bb9\u5668\u4e2d\u7684\u503c\uff0c\u5426\u5219\u629b\u51faNoSuchElementExceptio \u3002<\/p>\n<p>3\uff09\u6d4b\u8bd5\u4ee3\u7801<\/p>\n<pre><code>\npublic static void main(String[] args) {\n    Integer value1 = null;\n    Integer value2 = 1;\n    Optional&lt;Integer&gt; optional1 = Optional.ofNullable(value1);\n    Optional&lt;Integer&gt; optional2 = Optional.of(value2);\n    try {\n        Integer result=optional1.get();\n        System.out.println(&quot;optional1\u7684\u503c\u662f\uff1a&quot;+result);\n    }catch (Exception e){\n        System.out.println(&quot;optional1\u7684\u503c\u83b7\u53d6\u5931\u8d25,\u539f\u56e0:&quot;+e.getMessage());\n    }\n    try {\n        Integer result=optional2.get();\n        System.out.println(&quot;optional2\u7684\u503c\u662f\uff1a&quot;+result);\n    }catch (Exception e){\n        System.out.println(&quot;optional2\u7684\u503c\u83b7\u53d6\u5931\u8d25,\u539f\u56e0:&quot;+e.getMessage());\n    }\n}\n<\/code><\/pre>\n<p>4\uff09\u8fd0\u884c\u7ed3\u679c<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2J6B\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p><strong>2.3.4 \u5224\u65ad\u65b9\u6cd5<\/strong><br \/>1\uff09\u6e90\u7801<\/p>\n<pre><code>\npublic boolean isPresent() {\n    return value != null;\n}\npublic void ifPresent(Consumer&lt;? super T&gt; consumer) {\n    if (value != null)\n        consumer.accept(value);\n}\npublic T orElse(T other) {\n    return value != null ? value : other;\n }   \npublic T orElseGet(Supplier&lt;? extends T&gt; other) {\n     return value != null ? value : other.get();\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   }\n}\n<\/code><\/pre>\n<p>2\uff09\u65b9\u6cd5\u8bf4\u660e<\/p>\n<ul>\n<li>isPresent(): \u5224\u65adoptional\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u7a7a\u5219\u8fd4\u56defalse\uff0c\u5426\u5219\u8fd4\u56detrue<\/li>\n<li>ifPresent(Consumer c): \u5982\u679coptional\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06optional\u4e2d\u7684\u5bf9\u8c61\u4f20\u7ed9Comsumer\u51fd\u6570<\/li>\n<li>orElse(T other): \u5982\u679coptional\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56deoptional\u4e2d\u7684\u5bf9\u8c61\uff1b\u5982\u679c\u4e3anull\uff0c\u5219\u8fd4\u56de other \u8fd9\u4e2a\u5bf9\u8c61\u3002<\/li>\n<li>orElseGet(Supplier other): \u5982\u679coptional\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56deoptional\u4e2d\u7684\u5bf9\u8c61\uff1b\u5982\u679c\u4e3anull\uff0c\u5426\u5219\u8c03\u7528\u5176\u4ed6\u51fd\u6570\u5e76\u8fd4\u56de\u8c03\u7528\u7684\u7ed3\u679c<\/li>\n<li>orElseThrow(Supplier exception): \u5982\u679coptional\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56deoptional\u4e2d\u7684\u5bf9\u8c61\uff1b\u5982\u679c\u4e3anull\uff0c\u5219\u629b\u51faSupplier\u51fd\u6570\u751f\u6210\u7684\u5f02\u5e38<\/li>\n<\/ul>\n<p>3\uff09\u6d4b\u8bd5\u4ee3\u7801<\/p>\n<pre><code>\npublic static void main(String[] args) {\n    Integer value1 = null;\n    Integer value2 = 1;\n    Optional&lt;Integer&gt; optional1 = Optional.ofNullable(value1);\n    Optional&lt;Integer&gt; optional2 = Optional.of(value2);\n    try {\n        if(optional1.isPresent()){\n            System.out.println(&quot;optional1\u7684isPresent\u7ed3\u679c\u4e0d\u4e3a\u7a7a&quot;);\n        }else{\n            System.out.println(&quot;optional1\u7684isPresent\u7ed3\u679c\u4e3a\u7a7a&quot;);\n        }\n    }catch (Exception e){\n        System.out.println(&quot;optional1\u7684isPresent\u5224\u7a7a\u5931\u8d25,\u539f\u56e0:&quot;+e.getMessage());\n    }\n    try {\n        if(optional2.isPresent()){\n            System.out.println(&quot;optional2\u7684isPresent\u7ed3\u679c\u4e0d\u4e3a\u7a7a&quot;);\n        }else{\n            System.out.println(&quot;optional2\u7684isPresent\u7ed3\u679c\u4e3a\u7a7a&quot;);\n        }\n    }catch (Exception e){\n        System.out.println(&quot;optional2\u7684isPresent\u5224\u7a7a\u5931\u8d25,\u539f\u56e0:&quot;+e.getMessage());\n    }\n \n \n    optional1.ifPresent(t-&gt;{\n        int i =t+1;\n        System.out.println(&quot;optional1\u5904\u7406\u540e\u7684\u503c\u662f&quot;+i);\n    });\n    optional2.ifPresent(t-&gt;{\n        int i =t+1;\n        System.out.println(&quot;optional2\u5904\u7406\u540e\u7684\u503c\u662f&quot;+i);});\n \n \n    Integer value3 = 2;\n    Integer result = optional1.orElse(value3);\n    System.out.println(&quot;optional1\u6267\u884corElse\u5904\u7406\u540e\u7684\u503c\u662f&quot;+result);\n \n \n    result = optional2.orElse(value3);\n    System.out.println(&quot;optional2\u6267\u884corElse\u5904\u7406\u540e\u7684\u503c\u662f&quot;+result);\n \n \n    result = optional1.orElseGet(()-&gt; new Integer(-1));\n    System.out.println(&quot;optional1\u6267\u884corElseGet\u5904\u7406\u540e\u7684\u503c\u662f&quot;+result);\n \n \n    result = optional2.orElseGet(()-&gt; new Integer(-1));\n    System.out.println(&quot;optional2\u6267\u884corElseGet\u5904\u7406\u540e\u7684\u503c\u662f&quot;+result);\n    try {\n    result = optional1.orElseThrow (()-&gt; new RuntimeException(&quot;\u503c\u662f\u7a7a\u7684&quot;));\n    System.out.println(&quot;optional1\u6267\u884corElseThrow\u5904\u7406\u540e\u7684\u503c\u662f&quot;+result);\n    }catch (Exception e){\n        System.out.println(&quot;optional1\u7684orElseThrow\u629b\u51fa\u5f02\u5e38:&quot;+e.getMessage());\n    }\n    try {\n    result = optional2.orElseThrow (()-&gt; new RuntimeException(&quot;\u503c\u662f\u7a7a\u7684&quot;));\n    System.out.println(&quot;optional2\u6267\u884corElseThrow\u5904\u7406\u540e\u7684\u503c\u662f&quot;+result);\n    }catch (Exception e){\n        System.out.println(&quot;optional2\u7684orElseThrow\u629b\u51fa\u5f02\u5e38:&quot;+e.getMessage());\n<\/code><\/pre>\n<p>4\uff09\u8fd0\u884c\u7ed3\u679c<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2J68\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p>2.3.5 \u8fc7\u6ee4\u65b9\u6cd5<br \/>1\uff09\u6e90\u7801<\/p>\n<pre><code>\npublic Optional&lt;T&gt; filter(Predicate&lt;? super T&gt; predicate) {\n    Objects.requireNonNull(predicate);\n    if (!isPresent())\n        return this;\n    else\n        return predicate.test(value) ? this : empty();\n}<\/code><\/pre>\n<p>2\uff09\u65b9\u6cd5\u8bf4\u660e<\/p>\n<p>filter(Predicate p): \u5982\u679coptional\u4e0d\u4e3a\u7a7a\uff0c\u5219\u6267\u884cPredicate p\uff0c\u5982\u679cp\u7684\u7ed3\u679c\u4e3atrue\uff0c\u5219\u8fd4\u56de\u539f\u672c\u7684optional\uff0c\u5426\u5219\u8fd4\u56de\u7a7a\u7684optional<\/p>\n<p>3\uff09\u6d4b\u8bd5\u4ee3\u7801<\/p>\n<pre><code>\npublic static void main(String[] args) {\n    Integer value1 = 5;\n    Integer value2 = 6;\n    Optional&lt;Integer&gt; optional1 = Optional.ofNullable(value1);\n    Optional&lt;Integer&gt; optional2 = Optional.of(value2);\n \n \n    Optional&lt;Integer&gt; result =optional1.filter(t-&gt;t &gt; 5);\n    System.out.println(&quot;optional1\u7684filter\u540e\u7684\u503c:&quot;+result);\n     result =optional2.filter(t-&gt;t &gt; 5);\n    System.out.println(&quot;optional2\u7684filter\u540e\u7684\u503c:&quot;+result);<\/code><\/pre>\n<p>4\uff09\u8fd0\u884c\u7ed3\u679c<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2J7g\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p><strong>2.3.6 \u6620\u5c04\u65b9\u6cd5<\/strong><br \/>1\uff09\u6e90\u7801<\/p>\n<pre><code>\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}\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}\n<\/code><\/pre>\n<p>2\uff09\u65b9\u6cd5\u8bf4\u660e<\/p>\n<ul>\n<li>map(Function mapper): \u5982\u679c\u5b58\u5728\u4e00\u4e2a\u503c\uff0c\u5219\u5bf9\u5176\u5e94\u7528\u63d0\u4f9b\u7684\u6620\u5c04\u51fd\u6570\uff0c\u5982\u679c\u7ed3\u679c\u975e\u7a7a\uff0c\u5219\u8fd4\u56de\u63cf\u8ff0\u7ed3\u679c\u7684Optional\u3002 \u5426\u5219\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684Optional\u3002<\/li>\n<li>flatMap(Function&lt; T,Optional&gt; mapper): \u5982\u679c\u6709\u503c\uff0c\u5219\u5bf9\u5176\u5e94\u7528\u63d0\u4f9b\u7684\u53ef\u9009\u6620\u5c04\u51fd\u6570\uff0c\u8fd4\u56de\u7ed3\u679c\uff0c\u5426\u5219\u8fd4\u56de\u7a7a\u7684\u53ef\u9009\u51fd\u6570\u3002 \u8fd9\u4e2a\u65b9\u6cd5\u7c7b\u4f3c\u4e8emap(Function)\uff0c\u4f46\u662f\u63d0\u4f9b\u7684\u6620\u5c04\u5668\u7684\u7ed3\u679c\u5df2\u7ecf\u662f\u4e00\u4e2a\u53ef\u9009\u7684\uff0c\u5982\u679c\u8c03\u7528\uff0cflatMap\u4e0d\u4f1a\u7528\u989d\u5916\u7684\u53ef\u9009\u7684\u5305\u88c5\u5b83\u3002<\/li>\n<li>\u533a\u522b\uff1amap\u4f1a\u81ea\u52a8\u5c06u\u653e\u5230optional\u4e2d\uff0c\u800cflatMap\u5219\u9700\u8981\u624b\u52a8\u7ed9u\u521b\u5efa\u4e00\u4e2aoptional<\/li>\n<\/ul>\n<p>3\uff09\u6d4b\u8bd5\u4ee3\u7801<\/p>\n<pre><code>\n    public static void main(String[] args) {\n        User user1 = null;\n        User user2 = new User(&quot;user2\u540d\u5b57&quot;,19);\n        Optional&lt;User&gt; optional1 = Optional.ofNullable(user1);\n        Optional&lt;User&gt; optional2 = Optional.of(user2);\n        System.out.println(&quot;=========map==========&quot;);\n        System.out.println(&quot;optional1\u7684map\u524d\u7684\u503c:&quot;+optional1);\n        Optional&lt;String&gt; result =optional1.map(t-&gt;t.getName());\n        System.out.println(&quot;optional1\u7684map\u540e\u7684\u503c:&quot;+result);\n \n \n        System.out.println(&quot;optional2\u7684map\u524d\u7684\u503c:&quot;+optional2);\n        result =optional2.map(t-&gt;t.getName());\n        System.out.println(&quot;optional2\u7684map\u540e\u7684\u503c:&quot;+result);\n \n \n        System.out.println(&quot;===========flatMap========&quot;);\n \n \n        System.out.println(&quot;optional1\u7684flatMap\u524d\u7684\u503c:&quot;+optional1);\n        Optional&lt;Integer&gt; result2 =optional1.flatMap(t-&gt;Optional.ofNullable(t.getAge()));\n        System.out.println(&quot;optional1\u7684flatMap\u540e\u7684\u503c:&quot;+result2);\n \n \n        System.out.println(&quot;optional2\u7684flatMap\u524d\u7684\u503c:&quot;+optional2);\n        result2 =optional2.flatMap(t-&gt;Optional.ofNullable(t.getAge()));\n        System.out.println(&quot;optional2\u7684flatMap\u540e\u7684\u503c:&quot;+result2);\n \n    }\npublic class User {\n    String name;\n    Integer age;\n    public User(String name,Integer age){\n        this.name = name;\n        this.age=age;\n    }\n \n \n    public String getName() {\n        return name;\n    }\n \n \n    public Integer getAge() {\n        return age;\n<\/code><\/pre>\n<p>4\uff09\u8fd0\u884c\u7ed3\u679c<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2J7p\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h3>3 \u5e94\u7528\u5b9e\u4f8b<\/h3>\n<h4>3.1 \u9519\u8bef\u7528\u6cd5<\/h4>\n<ul>\n<li>\u7531\u4e8eOptional\u5e76\u6ca1\u6709\u5b9e\u73b0Serializable\u63a5\u53e3,\u6240\u4ee5\u4e0d\u80fd\u4f5c\u4e3a\u7c7b\u7684\u5c5e\u6027\u3002<\/li>\n<li>\u4e0d\u8981\u628aOptional\u4f5c\u4e3a\u65b9\u6cd5\u7684\u53c2\u6570\u3002<\/li>\n<li>\u628aif(x!=null)\u76f4\u63a5\u6362\u6210Optional.ofNullable(x).isPresent(),\u8fd9\u6837\u6709\u8fc7\u5ea6\u7f16\u7801\u7684\u5acc\u7591\u3002<\/li>\n<li>\u76f4\u63a5\u4f7f\u7528Optional.get()\u7684\u8fd4\u56de\u503c\u8fdb\u884c\u64cd\u4f5c\uff0cString result =Optional.ofNullable(null).get().toString()\uff1b\u8fd9\u6837\u8fd8\u662f\u4f1a\u629b\u51fa\u5f02\u5e38\u7684\u3002<\/li>\n<\/ul>\n<h4>3.2 \u5efa\u8bae\u7528\u6cd5<\/h4>\n<p>A\u7c7b\u6709\u5c5e\u6027B\u7c7b\uff0cB\u7c7b\u6709\u5c5e\u6027C\u7c7b,C\u7c7b\u6709name\u8fd9\u4e2a\u5b57\u6bb5\u3002<br \/>\u4f7f\u7528Optional\u4e4b\u524d:<\/p>\n<pre><code>\nif(atest!=null){\n    Btest btest =atest.getBtest();\n    if(btest!=null){\n      Ctest ctest = btest.getCtest();\n      if (ctest != null) {\n          name =ctest.getName();\n      }\n    }\n}<\/code><\/pre>\n<p>\u4f7f\u7528Optional\u4e4b\u540e:<\/p>\n<pre><code>\nname = Optional.ofNullable(atest).map(t-&gt;t.getBtest()).map(t-&gt;t.getCtest()).map(t-&gt;t.getName()).orElse(&quot;\u9ed8\u8ba4\u503c&quot;);<\/code><\/pre>\n<p>\u4ee3\u7801\u662f\u4e0d\u662f\u770b\u4e0a\u53bb\u66f4\u6574\u6d01\u4e86\u5462\uff1f<\/p>\n<h3>4 \u603b\u7ed3<\/h3>\n<p>\u901a\u8fc7\u5bf9Optional\u6e90\u7801\u89e3\u6790\u548c\u7528\u4f8b\u6d4b\u8bd5\u4ee3\u7801\u7684\u8fd0\u884c\u7ed3\u679c\uff0c\u53ef\u4ee5\u770b\u51fa\u4f7f\u7528Optional\u53ef\u4ee5\u4f18\u5316null\u503c\u5224\u65ad\u4ee3\u7801\uff0c\u8ba9\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u4f18\u96c5\u548c\u6574\u6d01\u3002<\/p>\n<p>\u4f5c\u8005\uff1a\u9648\u660c\u6d69<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042560626\">https:\/\/segmentfault.com\/a\/1190000042560626<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 \u5bfc\u8bfb NullPointerException\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u9047\u5230\uff0c\u7a0d\u6709\u4e0d\u614e\u5c0fBUG\u5c31\u51fa\u73b0\u4e86\uff0c\u5982\u679c\u907f\u514d\u8fd9\u4e2a [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006635","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006635","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"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006635"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006635\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}