{"id":335,"date":"2023-02-25T06:48:03","date_gmt":"2023-02-24T22:48:03","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=335"},"modified":"2023-04-30T14:50:05","modified_gmt":"2023-04-30T06:50:05","slug":"java-8-optional-class","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/java-8-optional-class\/","title":{"rendered":"Java 8 Optional \u7c7b"},"content":{"rendered":"<ul>\n<li>Optional \u7c7b\u662f\u4e00\u4e2a\u53ef\u4ee5\u4e3anull\u7684\u5bb9\u5668\u5bf9\u8c61\u3002\u5982\u679c\u503c\u5b58\u5728\u5219<code>isPresent()<\/code>\u65b9\u6cd5\u4f1a\u8fd4\u56detrue\uff0c\u8c03\u7528<code>get()<\/code>\u65b9\u6cd5\u4f1a\u8fd4\u56de\u8be5\u5bf9\u8c61<\/li>\n<li>Optional \u662f\u4e2a\u5bb9\u5668\uff1a\u5b83\u53ef\u4ee5\u4fdd\u5b58\u7c7b\u578bT\u7684\u503c\uff0c\u6216\u8005\u4ec5\u4ec5\u4fdd\u5b58null\u3002Optional\u63d0\u4f9b\u5f88\u591a\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u4e0d\u7528\u663e\u5f0f\u8fdb\u884c\u7a7a\u503c\u68c0\u6d4b<\/li>\n<li>Optional \u7c7b\u7684\u5f15\u5165\u5f88\u597d\u7684\u89e3\u51b3\u7a7a\u6307\u9488\u5f02\u5e38<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h2>\u7c7b\u58f0\u660e<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a<code>java.util.Optional&lt;T&gt;<\/code>\u7c7b\u7684\u58f0\u660e\uff1a<\/p>\n<pre><code class=\"language-java\">public final class Optional&lt;T&gt;\nextends Object<\/code><\/pre>\n<h2>\u7c7b\u65b9\u6cd5<\/h2>\n<p>\uff081\uff09<code>static &lt;T&gt; Optional&lt;T&gt; empty()<\/code><\/p>\n<p>\u8fd4\u56de\u7a7a\u7684 Optional \u5b9e\u4f8b<\/p>\n<p>\uff082\uff09<code>boolean equals(Object obj)<\/code><\/p>\n<p>\u5224\u65ad\u5176\u4ed6\u5bf9\u8c61\u662f\u5426\u7b49\u4e8e Optional<\/p>\n<p>\uff083\uff09<code>Optional&lt;T&gt; filter(Predicate&lt;? super &lt;T&gt; predicate)<\/code><\/p>\n<p>\u5982\u679c\u503c\u5b58\u5728\uff0c\u5e76\u4e14\u8fd9\u4e2a\u503c\u5339\u914d\u7ed9\u5b9a\u7684 predicate\uff0c\u8fd4\u56de\u4e00\u4e2a Optional \u7528\u4ee5\u63cf\u8ff0\u8fd9\u4e2a\u503c\uff0c\u5426\u5219\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684 Optional<\/p>\n<p>\uff084\uff09<code>&lt;U&gt; Optional&lt;U&gt; flatMap(Function&lt;? super T, Optional&lt;U&gt;&gt; mapper)<\/code><\/p>\n<p>\u5982\u679c\u503c\u5b58\u5728\uff0c\u8fd4\u56de\u57fa\u4e8e Optional \u5305\u542b\u7684\u6620\u5c04\u65b9\u6cd5\u7684\u503c\uff0c\u5426\u5219\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684 Optional<\/p>\n<p>\uff085\uff09<code>T get()<\/code><\/p>\n<p>\u5982\u679c\u5728\u8fd9\u4e2a Optional \u4e2d\u5305\u542b\u8fd9\u4e2a\u503c\uff0c\u8fd4\u56de\u503c\uff0c\u5426\u5219\u629b\u51fa\u5f02\u5e38\uff1aNoSuchElementException<\/p>\n<p>\uff086\uff09<code>int hashCode()<\/code><\/p>\n<p>\u8fd4\u56de\u5b58\u5728\u503c\u7684\u54c8\u5e0c\u7801\uff0c\u5982\u679c\u503c\u4e0d\u5b58\u5728\u8fd4\u56de 0<\/p>\n<p>\uff087\uff09<code>void ifPresent(Consumer&lt;? super T&gt; consumer)<\/code><\/p>\n<p>\u5982\u679c\u503c\u5b58\u5728\u5219\u4f7f\u7528\u8be5\u503c\u8c03\u7528 Consumer\uff0c\u5426\u5219\u4e0d\u505a\u4efb\u4f55\u4e8b\u60c5<\/p>\n<p>\uff088\uff09<code>boolean isPresent()<\/code><\/p>\n<p>\u5982\u679c\u503c\u5b58\u5728\u5219\u65b9\u6cd5\u4f1a\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false<\/p>\n<p>\uff089\uff09<code>&lt;U&gt;Optional&lt;U&gt; map(Function&lt;? super T, ? extends U&gt; mapper)<\/code><\/p>\n<p>\u5982\u679c\u6709\u503c\uff0c\u5219\u5bf9\u5176\u6267\u884c\u8c03\u7528\u6620\u5c04\u51fd\u6570\u5f97\u5230\u8fd4\u56de\u503c\u3002\u5982\u679c\u8fd4\u56de\u503c\u4e0d\u4e3a null\uff0c\u5219\u521b\u5efa\u5305\u542b\u6620\u5c04\u8fd4\u56de\u503c\u7684 Optional \u4f5c\u4e3amap\u65b9\u6cd5\u8fd4\u56de\u503c\uff0c\u5426\u5219\u8fd4\u56de\u7a7aOptional<\/p>\n<p>\uff0810\uff09<code>static &lt;T&gt; Optional&lt;T&gt; of(T value)<\/code><\/p>\n<p>\u8fd4\u56de\u4e00\u4e2a\u6307\u5b9a\u975e null \u503c\u7684 Optional<\/p>\n<p>\uff0811\uff09<code>static &lt;T&gt; Optional&lt;T&gt; ofNullable(T value)<\/code><\/p>\n<p>\u5982\u679c\u4e3a\u975e\u7a7a\uff0c\u8fd4\u56de Optional \u63cf\u8ff0\u7684\u6307\u5b9a\u503c\uff0c\u5426\u5219\u8fd4\u56de\u7a7a\u7684 Optional<\/p>\n<p>\uff0812\uff09<code>T orElse(T other)<\/code><\/p>\n<p>\u5982\u679c\u5b58\u5728\u8be5\u503c\uff0c\u8fd4\u56de\u503c\uff0c\u5426\u5219\u8fd4\u56de other<\/p>\n<p>\uff0813\uff09<code>T orElseGet(Supplier&lt;? extends T&gt; other)<\/code><\/p>\n<p>\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<\/p>\n<p>\uff0814\uff09<code>&lt;X extends Throwable&gt; T orElseThrow(Supplier&lt;? extends X&gt; exceptionSupplier)<\/code><\/p>\n<p>\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<\/p>\n<p>\uff0815\uff09<code>String toString()<\/code><\/p>\n<p>\u8fd4\u56de\u4e00\u4e2a Optional \u7684\u975e\u7a7a\u5b57\u7b26\u4e32\uff0c\u7528\u6765\u8c03\u8bd5<\/p>\n<pre><code class=\"language-java\">public class OptionalTest {\n\n    public static void main(String[] args) {\n        Integer value1 = null;\n        Integer value2 = new Integer(10);\n\n        \/\/ Optional.ofNullable - \u5141\u8bb8\u4f20\u9012\u4e3a null \u53c2\u6570\n        Optional&lt;Integer&gt; a = Optional.ofNullable(value1);\n\n        \/\/ Optional.of - \u5982\u679c\u4f20\u9012\u7684\u53c2\u6570\u662f null\uff0c\u629b\u51fa\u5f02\u5e38 NullPointerException\n        Optional&lt;Integer&gt; b = Optional.of(value2);\n        System.out.println(sum(a, b));\n    }\n\n    public static Integer sum(Optional&lt;Integer&gt; a, Optional&lt;Integer&gt; b) {\n\n        \/\/ Optional.isPresent - \u5224\u65ad\u503c\u662f\u5426\u5b58\u5728\n        System.out.println(&quot;\u7b2c\u4e00\u4e2a\u53c2\u6570\u503c\u5b58\u5728: &quot; + a.isPresent());\n        System.out.println(&quot;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u503c\u5b58\u5728: &quot; + b.isPresent());\n\n        \/\/ Optional.orElse - \u5982\u679c\u503c\u5b58\u5728\uff0c\u8fd4\u56de\u5b83\uff0c\u5426\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c\n        Integer value1 = a.orElse(new Integer(0));\n\n        \/\/Optional.get - \u83b7\u53d6\u503c\uff0c\u503c\u9700\u8981\u5b58\u5728\n        Integer value2 = b.get();\n        return value1 + value2;\n    }\n}<\/code><\/pre>\n<pre><code>\u7b2c\u4e00\u4e2a\u53c2\u6570\u503c\u5b58\u5728: false\n\u7b2c\u4e8c\u4e2a\u53c2\u6570\u503c\u5b58\u5728: true\n10<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Optional \u7c7b\u662f\u4e00\u4e2a\u53ef\u4ee5\u4e3anull\u7684\u5bb9\u5668\u5bf9\u8c61\u3002\u5982\u679c\u503c\u5b58\u5728\u5219isPresent()\u65b9\u6cd5\u4f1a\u8fd4\u56detrue\uff0c\u8c03\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-335","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}