{"id":202688,"date":"2025-05-22T09:59:19","date_gmt":"2025-05-22T01:59:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202688\/"},"modified":"2025-05-22T09:59:19","modified_gmt":"2025-05-22T01:59:19","slug":"%e5%9c%a8-java-%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e7%9a%84%e9%ab%98%e7%ba%a7%e6%8a%80%e5%b7%a7%e5%92%8c%e6%a8%a1%e5%bc%8f%e6%9c%89%e5%93%aa%e4%ba%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202688\/","title":{"rendered":"\u5728 Java \u4e2d\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u9ad8\u7ea7\u6280\u5de7\u548c\u6a21\u5f0f\u6709\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5728 Java \u4e2d\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u9ad8\u7ea7\u6280\u5de7\u548c\u6a21\u5f0f\u6709\u54ea\u4e9b\uff1f<\/h1>\n<p>\u76ee\u524d\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 Java \u4e2d\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u9ad8\u7ea7\u6280\u5de7\u548c\u6a21\u5f0f\u6709\u54ea\u4e9b\uff1f\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p>\u5728 Java \u4e2d\u51fd\u6570\u5f0f\u7f16\u7a0b\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6a21\u5f0f\u589e\u5f3a\uff1a\u5c3e\u9012\u5f52\u4f18\u5316\uff08\u9012\u5f52\u5c3e\u90e8\u8c03\u7528\u4e0d\u521b\u5efa\u65b0\u7684\u6808\u5e27\uff09\u3001\u60f0\u6027\u6c42\u503c\uff08\u4ec5\u5728\u9700\u8981\u65f6\u6267\u884c\u6d41\u64cd\u4f5c\uff09\u3001\u67ef\u91cc\u5316\uff08\u5c06\u591a\u53c2\u6570\u51fd\u6570\u8f6c\u6362\u4e3a\u5355\u53c2\u6570\u51fd\u6570\uff09\uff0c\u4ee5\u53ca\u4e0d\u53ef\u53d8\u6027\u3001\u7eaf\u51fd\u6570\u548c\u9ad8\u9636\u51fd\u6570\u3002\u8fd9\u4e9b\u6a21\u5f0f\u63d0\u4f9b\u4e86\u6027\u80fd\u3001\u5e76\u53d1\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u7684\u4f18\u52bf\uff0c\u53ef\u7528\u4e8e\u5e76\u53d1\u7f16\u7a0b\u3001\u6570\u636e\u5904\u7406\u548c\u4e0d\u53d8\u6027\u63a8\u7406\u7b49\u5b9e\u9645\u7528\u4f8b\u4e2d\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729932152671cab780d65f.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 Java \u4e2d\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u9ad8\u7ea7\u6280\u5de7\u548c\u6a21\u5f0f<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5b83\u5c06\u8ba1\u7b97\u89c6\u4e3a\u6570\u5b66\u51fd\u6570\u7684\u5e94\u7528\uff0c\u5f3a\u8c03\u4e0d\u53d8\u6027\u548c\u65e0\u526f\u4f5c\u7528\u3002\u5728 Java \u4e2d\uff0c\u51fd\u6570\u5f0f\u7f16\u7a0b\u53ef\u4ee5\u901a\u8fc7 Lambda \u8868\u8fbe\u5f0f\u548c\u6d41 API \u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u9ad8\u7ea7\u6280\u5de7<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u5c3e\u9012\u5f52\u4f18\u5316\uff1a<\/strong>\u9012\u5f52\u51fd\u6570\u5c3e\u90e8\u8c03\u7528\u81ea\u8eab\u65f6\uff0c\u4e0d\u4f1a\u521b\u5efa\u65b0\u7684\u6808\u5e27\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>public static int factorial(int n) {\n  return n == 0 ? 1 : n * factorial(n - 1);\n}<\/pre>\n<\/li>\n<li>\n<p><strong>\u60f0\u6027\u6c42\u503c\uff1a<\/strong>\u6d41\u64cd\u4f5c\u4ec5\u5728\u9700\u8981\u65f6\u624d\u6267\u884c\uff0c\u8fd9\u53ef\u4ee5\u8282\u7701\u8ba1\u7b97\u8d44\u6e90\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>Stream&lt;Integer&gt; numbers = Stream.iterate(0, n -&gt; n + 1);\n\/\/ \u4ec5\u5728\u4f7f\u7528\u65f6\u6267\u884c\u6c42\u548c\u64cd\u4f5c\nint sum = numbers.limit(100).sum();<\/pre>\n<\/li>\n<li>\n<p><strong>\u67ef\u91cc\u5316\uff1a<\/strong>\u5c06\u4e00\u4e2a\u591a\u53c2\u6570\u51fd\u6570\u8f6c\u6362\u4e3a\u4e00\u7cfb\u5217\u5355\u53c2\u6570\u51fd\u6570\uff0c\u4ee5\u4fbf\u90e8\u5206\u5e94\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>Function&lt;Integer, Function&lt;Integer, Integer&gt;&gt; add = a -&gt; b -&gt; a + b;\nint result = add.apply(1).apply(2); \/\/ \u8fd4\u56de 3<\/pre>\n<\/li>\n<\/ul>\n<p><strong>\u6a21\u5f0f<\/strong><\/p>\n<ul>\n<li><strong>\u4e0d\u53ef\u53d8\u6027\uff1a<\/strong>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u5bf9\u8c61\u88ab\u89c6\u4e3a\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u6709\u5229\u4e8e\u5e76\u53d1\u548c\u4e0d\u53d8\u6027\u63a8\u7406\u3002<\/li>\n<li><strong>\u7eaf\u51fd\u6570\uff1a<\/strong>\u7eaf\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u526f\u4f5c\u7528\uff0c\u5e76\u4e14\u603b\u662f\u8fd4\u56de\u76f8\u540c\u7684\u503c\uff0c\u7ed9\u5b9a\u76f8\u540c\u7684\u8f93\u5165\u3002\u8fd9\u4f7f\u5b83\u4eec\u6613\u4e8e\u6d4b\u8bd5\u548c\u63a8\u7406\u3002<\/li>\n<li>\n<p><strong>\u9ad8\u9636\u51fd\u6570\uff1a<\/strong>\u9ad8\u9636\u51fd\u6570\u5c06\u5176\u4ed6\u51fd\u6570\u4f5c\u4e3a\u8f93\u5165\u6216\u8f93\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>Function&lt;Function&lt;Integer, Integer&gt;, Integer&gt; applyTwice = f -&gt; f.apply(f.apply(x));<\/pre>\n<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u5e76\u53d1\u7f16\u7a0b\uff1a<\/strong>\u51fd\u6570\u5f0f\u7f16\u7a0b\u901a\u8fc7\u4e0d\u53ef\u53d8\u6027\u63d0\u4f9b\u4e00\u4e2a\u5929\u7136\u7684\u5e76\u53d1\u6a21\u578b\uff0c\u5141\u8bb8\u7ebf\u7a0b\u5b89\u5168\u5730\u5e76\u884c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<pre>List&lt;Integer&gt; numbers = List.of(1, 2, 3, 4, 5);\nint sum = numbers.parallelStream().sum(); \/\/ \u5e76\u884c\u8ba1\u7b97\u548c<\/pre>\n<p><strong>\u6570\u636e\u5904\u7406\uff1a<\/strong>\u6d41 API \u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5927\u6570\u636e\u96c6\u5408\u3002<\/p>\n<pre>Map&lt;String, Long&gt; wordCounts = words.stream()\n        .groupBy(String::toLowerCase)\n        .counting();<\/pre>\n<p>\u603b\u4e4b\uff0c\u5728 Java \u4e2d\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u3001\u53ef\u9760\u6027\u548c\u5e76\u53d1\u6027\u3002\u901a\u8fc7\u91c7\u7528\u9ad8\u7ea7\u6280\u5de7\u548c\u6a21\u5f0f\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u521b\u5efa\u66f4\u7b80\u6d01\u3001\u66f4\u5177\u53ef\u7ef4\u62a4\u6027\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5728 Java \u4e2d\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u9ad8\u7ea7\u6280\u5de7\u548c\u6a21\u5f0f\u6709\u54ea\u4e9b\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejava,\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u4e2d\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\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-202688","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202688","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=202688"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202688\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}