{"id":1321,"date":"2025-03-25T18:53:37","date_gmt":"2025-03-25T10:53:37","guid":{"rendered":"https:\/\/blog.5danyuan.com\/?p=1321"},"modified":"2025-03-25T18:53:37","modified_gmt":"2025-03-25T10:53:37","slug":"java-%e9%87%8c%e7%9a%84%e5%85%83%e7%bb%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/blog.5danyuan.com\/?p=1321","title":{"rendered":"Java \u91cc\u7684\u5143\u7ec4\u4f7f\u7528"},"content":{"rendered":"\n<p>\u5728\u6700\u8fd1\u7684 Sping boot \u9879\u76ee\u91cc\uff0c\u60f3\u8981\u4e00\u6b21\u6027\u8fd4\u56de\u591a\u4e2a\u5bf9\u8c61\uff0c\u6309\u7167\u8fc7\u53bb .Net\u7684\u7ecf\u9a8c\uff0c\u5f88\u5bb9\u6613\u60f3\u5230\u7684\u5c31\u662f\u4f7f\u7528\u5143\u7ec4 Tuple\uff0c\u4f46\u662f\u7b2c\u4e00\u4e0b\u5c45\u7136\u6ca1\u6709\u627e\u5230\uff0c\u4e0d\u8fc7\u6709 HashMap\uff0c\u770b\u8d77\u6765\u4f3c\u4e4e\u4e5f\u662f\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u4f46\u662f\u5b58\u5728\u9700\u8981\u5f3a\u5236\u8f6c\u6362\u7684\u60c5\u51b5\uff0c\u67d0\u4e9b\u573a\u666f\u4e0b\u53ef\u80fd\u4f1a\u51fa\u9519\u3002<\/p>\n\n\n\n<p>\u4e0d\u662f\u5f88\u6b7b\u5fc3\uff0c\u4e8e\u662f\u5c31\u641c\u7d22\u4e86\u4e00\u4e0b\uff0c\u627e\u5230\u4e86 vavr \u8fd9\u4e2a\u5e93\uff0c\u6bd4\u8f83\u7b80\u5355\uff0c\u5927\u5bb6\u53ef\u4ee5\u51d1\u6d3b\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u662f\u5728 pom.xml \u91cc\u6dfb\u52a0\u4f9d\u8d56\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- https:\/\/mvnrepository.com\/artifact\/io.vavr\/vavr -->\n&lt;dependency>\n    &lt;groupId>io.vavr&lt;\/groupId>\n    &lt;artifactId>vavr&lt;\/artifactId>\n    &lt;version>0.10.6&lt;\/version>\n&lt;\/dependency><\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528 Tuple.of \u6765\u521b\u5efa\u5bf9\u8c61\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Tuple2&lt;String, LocalDateTime> element = Tuple.of(\"Name\", LocalDateTime.now());<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa\u4e24\u4e2a\u5143\u7d20\u7684\u662f Tuple2\uff0c\u4e09\u4e2a\u5143\u7d20\u5c31\u662f Tuple3\uff0c\u8fd9\u4e2a\u547d\u540d\u65b9\u5f0f\u6709\u70b9\u7279\u522b\uff0c\u54c8\u54c8\u3002<\/p>\n\n\n\n<p>\u66f4\u65b0\u5143\u7ec4\u91cc\u7684\u5185\u5bb9\uff1aelement.update1(&#8220;\u540d\u79f0&#8221;); \u4e5f\u5c31\u662f update[1-n]\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>\u8bfb\u53d6\u5143\u7ec4\u5143\u7d20\u7684\u5185\u5bb9\uff0celement._1() \u662f\u83b7\u53d6\u7b2c\u4e00\u4e2a\u503c\uff0celement._2()\u5c31\u662f\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u503c\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u5e93\u662f Apache \u534f\u8bae\uff0c\u53ef\u4ee5\u5546\u7528\uff0c\u4e0d\u9519\u3002<\/p>\n\n\n\n<p>\u6211\u53c8\u7ee7\u7eed\u641c\u7d22\u4e86\u4e0b\uff0c\u5143\u7ec4\u53ea\u662f\u5176\u652f\u6301\u7684\u4e00\u4e2a\u529f\u80fd\uff0c\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e5f\u652f\u6301\uff0candThen\u3001compose \u770b\u8d77\u6765\u597d\u9ad8\u7ea7\u54c7\u3002<\/p>\n\n\n\n<p>\u5f02\u5e38\u5904\u7406\u7684\u5199\u6cd5\u4e5f\u4e0d\u9519\uff0c\u6211\u7b80\u5355\u5730\u590d\u5236\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Try result = Try.of(() -> 0)\n                .map((a) -> 10 \/ a) \/\/\u5373\u4f7f\u6b64\u5904\u629b\u51fa\u5f02\u5e38\uff0c\u4e0d\u4f1a\u5bfc\u81f4\u5f53\u524d\u7ebf\u7a0b\u7ed3\u675f\u3002\u8fd9\u91cc\u65e0\u9700\u4f7f\u7528 try{}catch()\u5bf9\u4ee3\u7801\u8fdb\u884c\u6355\u83b7\n                .andThen(() -> System.out.printf(\"--\u629b\u51fa\u5f02\u5e38\u6b64\u5904\u4e0d\u4f1a\u6267\u884c--\")) \/\/\u6267\u884c\u4e00\u4e2a\u52a8\u4f5c\uff0c\u4e0d\u4fee\u6539\u7ed3\u679c\n                .map(i -> {\n                    System.out.println(\"\u5f53\u524d\u503c\uff1a\" + i);\n                    return i + 10;\n                })\n                .onFailure(e -> e.printStackTrace())\/\/\u5931\u8d25\u65f6\u4f1a\u89e6\u53d1onFailure\n                .recover(ArithmeticException.class, 1000) \/\/\u5982\u679c\u9047\u5230 Exception\u7c7b\u578b\u7684\u5f02\u5e38,\u5219\u8fd4\u56de1000\n                .map((a) -> a + 1);\n\n        System.out.println(\"\u662f\u5426\u629b\u51fa\u5f02\u5e38\uff1a\" + result.isFailure());\n        System.out.println(\"\u6267\u884c\u7ed3\u679c:\" + result.getOrElse(100)); \/\/\u5982\u679c\u6709\u5f02\u5e38\u53d1\u751f\uff0c\u5219\u8fd4\u56de100<\/code><\/pre>\n\n\n\n<p>\u5ef6\u8fdf\u8ba1\u7b97\uff0c\u8fd9\u4e2a\u4e5f\u9ad8\u7ea7\uff0c\u4f46\u662f\u6f5c\u610f\u8bc6\u611f\u89c9\uff0c\u5982\u679c\u7528\u7684\u4e0d\u597d\uff0c\u5f88\u5bb9\u6613\u7ed9\u81ea\u5df1\u6316\u5751\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Lazy&lt;Double> lazy = Lazy.of(Math::random)\n                .map(i -> {\n                    System.out.println(\"-----\u6b63\u5728\u8fdb\u884c\u8ba1\u7b97\uff0c\u6b64\u5904\u53ea\u4f1a\u6267\u884c\u4e00\u6b21------\");\n                    return i * 100;\n                });\n        System.out.println(lazy.isEvaluated());\n        System.out.println(lazy.get()); \/\/\u89e6\u53d1\u8ba1\u7b97\n        System.out.println(lazy.isEvaluated());\n        System.out.println(lazy.get());\/\/\u4e0d\u4f1a\u91cd\u65b0\u8ba1\u7b97\uff0c\u8fd4\u56de\u4e0a\u6b21\u7ed3\u679c<\/code><\/pre>\n\n\n\n<p>\u8fd8\u6709\u53e6\u5916\u4e00\u79cd\u7ebf\u7a0b\u64cd\u4f5c\u7684\u5199\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.println(\"\u5f53\u524d\u7ebf\u7a0b\u540d\u79f0\uff1a\" + Thread.currentThread().getName());\n        Integer result = Future.of(() -> {\n            System.out.println(\"future\u7ebf\u7a0b\u540d\u79f0\uff1a\" + Thread.currentThread().getName());\n            Thread.sleep(2000);\n            return 100;\n        })\n                .map(i -> i * 10)\n                .await(3000, TimeUnit.MILLISECONDS) \/\/\u7b49\u5f85\u7ebf\u7a0b\u6267\u884c3000\u6beb\u79d2\n                .onFailure(e -> e.printStackTrace())\n                .getValue() \/\/\u8fd4\u56deOptional&lt;Try&lt;Integer>>\u7c7b\u578b\u7ed3\u679c\n                .getOrElse(Try.of(() -> 100)) \/\/\u5982\u679cOption \u4e3a empty\u65f6\uff0c\u5219\u8fd4\u56deTry(100)\n                .get();\n        System.out.println(result); \/\/ 1000<\/code><\/pre>\n\n\n\n<p>\u5927\u5bb6\u653e\u5fc3\u7528\u5427\uff0c\u4e00\u7528\u4e00\u4e2a\u4e0d\u5431\u58f0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6700\u8fd1\u7684 Sping boot \u9879\u76ee\u91cc\uff0c\u60f3\u8981\u4e00\u6b21\u6027\u8fd4\u56de\u591a\u4e2a\u5bf9\u8c61\uff0c\u6309\u7167\u8fc7\u53bb .Net\u7684\u7ecf\u9a8c\uff0c\u5f88\u5bb9\u6613\u60f3\u5230\u7684\u5c31\u662f\u4f7f\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":[354],"tags":[355],"class_list":["post-1321","post","type-post","status-publish","format-standard","hentry","category-java","tag-355"],"_links":{"self":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts\/1321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1321"}],"version-history":[{"count":0,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts\/1321\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}