{"id":202861,"date":"2025-05-22T09:33:35","date_gmt":"2025-05-22T01:33:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202861\/"},"modified":"2025-05-22T09:33:35","modified_gmt":"2025-05-22T01:33:35","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e5%9c%a8%e5%87%bd%e6%95%b0%e7%bb%84%e5%90%88%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202861\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u51fd\u6570\u7ec4\u5408\u4e2d\u7684\u5e94\u7528"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u51fd\u6570\u7ec4\u5408\u4e2d\u7684\u5e94\u7528<\/h1>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u51fd\u6570\u7ec4\u5408\u4e2d\u7684\u5e94\u7528\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p>\u51fd\u6570\u7ec4\u5408\u662f Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u5c06\u51fd\u6570\u4e32\u8054\u5728\u4e00\u8d77\u521b\u5efa\u65b0\u51fd\u6570\u7684\u6280\u672f\uff0c\u53ef\u63d0\u9ad8\u53ef\u8bfb\u6027\u3001\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u65b9\u6cd5\uff1acompose()\uff1a\u5c06\u4e24\u4e2a\u51fd\u6570\u4ece\u53f3\u5230\u5de6\u7ec4\u5408\u3002andThen()\uff1a\u5c06\u4e24\u4e2a\u51fd\u6570\u4ece\u5de6\u5230\u53f3\u7ec4\u5408\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u5c06\u5b57\u7b26\u4e32\u6d41\u8f6c\u6362\u4e3a\u5927\u5199\u5e76\u6392\u5e8f\u3002\u4f7f\u7528 compose() \u65b9\u6cd5\u5c06\u8f6c\u6362\u548c\u6392\u5e8f\u51fd\u6570\u7ec4\u5408\u3002\u4f18\u52bf\uff1a\u589e\u5f3a\u53ef\u8bfb\u6027\u3002\u63d0\u9ad8\u91cd\u7528\u6027\u3002\u63d0\u5347\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729952263671cfa079b1a8.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u51fd\u6570\u7ec4\u5408<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u5f3a\u8c03\u4f7f\u7528\u4e0d\u53ef\u53d8\u6570\u636e\u548c\u7eaf\u51fd\u6570\uff0c\u5176\u4e2d\u51fd\u6570\u7ec4\u5408\u662f\u5173\u952e\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u65b0\u7684\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u7531\u8f83\u5c0f\u7684\u51fd\u6570\u7ec4\u5408\u800c\u6210\u3002Java 8 \u5f15\u5165\u4e86 <code>compose()<\/code> \u548c <code>andThen()<\/code> \u65b9\u6cd5\u6765\u65b9\u4fbf\u51fd\u6570\u7ec4\u5408\u3002<\/p>\n<p><strong>\u51fd\u6570\u7ec4\u5408<\/strong><\/p>\n<p>\u51fd\u6570\u7ec4\u5408\u662f\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u4e32\u8054\u5728\u4e00\u8d77\u7684\u8fc7\u7a0b\uff0c\u4ece\u800c\u521b\u5efa\u4e00\u4e2a\u6267\u884c\u590d\u5408\u64cd\u4f5c\u7684\u65b0\u51fd\u6570\u3002\u8003\u8651\u4ee5\u4e0b\u4e24\u4e2a\u51fd\u6570\uff1a<\/p>\n<pre>Function&lt;Integer, Integer&gt; square = x -&gt; x * x;\nFunction&lt;Integer, Integer&gt; add10 = x -&gt; x + 10;<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>compose()<\/code> \u65b9\u6cd5\u5c06 <code>add10<\/code> \u51fd\u6570\u4e0e <code>square<\/code> \u51fd\u6570\u7ec4\u5408\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5148\u6267\u884c\u5e73\u65b9\u64cd\u4f5c\uff0c\u7136\u540e\u6dfb\u52a0 10\uff1a<\/p>\n<pre>Function&lt;Integer, Integer&gt; squareAndAdd10 = square.compose(add10);<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u6d41\u8f6c\u6362\u4e3a\u5927\u5199\u5e76\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<pre>List&lt;String&gt; strings = Arrays.asList(\"apple\", \"banana\", \"cherry\", \"dog\");\n\n\/\/ \u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\u5b9a\u4e49\u8f6c\u6362\u51fd\u6570\nFunction&lt;String, String&gt; toUpperCase = String::toUpperCase;\nFunction&lt;String, String&gt; sort = strings -&gt; {\n    Collections.sort(strings);\n    return strings.stream().collect(Collectors.joining(\",\"));\n};\n\n\/\/ \u4f7f\u7528 `compose()` \u65b9\u6cd5\u7ec4\u5408\u51fd\u6570\nFunction&lt;List&lt;String&gt;, String&gt; convertAndSort = sort.compose(toUpperCase);\n\n\/\/ \u5c06\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u6253\u5370\u5230\u63a7\u5236\u53f0\nString result = convertAndSort.apply(strings);\nSystem.out.println(result); \/\/ BANANA,CHERRY,DOG,APPLE<\/pre>\n<p><strong>\u4f18\u52bf<\/strong><\/p>\n<p>\u51fd\u6570\u7ec4\u5408\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u4f18\u52bf\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u589e\u5f3a\u53ef\u8bfb\u6027\uff1a<\/strong>\u7ec4\u5408\u51fd\u6570\u53ef\u521b\u5efa\u66f4\u7b80\u6d01\u3001\u66f4\u6613\u4e8e\u7406\u89e3\u7684\u4ee3\u7801\u3002<\/li>\n<li><strong>\u91cd\u7528\u6027\uff1a<\/strong>\u80fd\u591f\u91cd\u7528\u5c0f\u7684\u3001\u72ec\u7acb\u7684\u51fd\u6570\u6765\u6784\u5efa\u66f4\u590d\u6742\u7684\u51fd\u6570\u3002<\/li>\n<li><strong>\u53ef\u7ef4\u62a4\u6027\uff1a<\/strong>\u901a\u8fc7\u5c06\u590d\u6742\u64cd\u4f5c\u5206\u89e3\u4e3a\u8f83\u5c0f\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u51fd\u6570\u7ec4\u5408\u4e2d\u7684\u5e94\u7528\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejava,\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u51fd\u6570\u7ec4\u5408\u4e2d\u7684&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202861","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202861"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202861\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}