{"id":202852,"date":"2025-05-22T10:33:08","date_gmt":"2025-05-22T02:33:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202852\/"},"modified":"2025-05-22T10:33:08","modified_gmt":"2025-05-22T02:33:08","slug":"%e4%bd%bf%e7%94%a8lambda%e8%a1%a8%e8%be%be%e5%bc%8f%e5%88%9b%e5%bb%ba%e5%92%8c%e4%bd%bf%e7%94%a8java%e5%87%bd%e6%95%b0%e7%9a%84%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202852\/","title":{"rendered":"\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u521b\u5efa\u548c\u4f7f\u7528Java\u51fd\u6570\u7684\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u521b\u5efa\u548c\u4f7f\u7528Java\u51fd\u6570\u7684\u6700\u4f73\u5b9e\u8df5<\/h1>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u521b\u5efa\u548c\u4f7f\u7528Java\u51fd\u6570\u7684\u6700\u4f73\u5b9e\u8df5\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>Java lambda \u8868\u8fbe\u5f0f\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\uff1a\u6e05\u6670\u547d\u540d\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002\u8003\u8651\u7c7b\u578b\u6307\u5b9a\u4ee5\u6355\u83b7\u7f16\u8bd1\u65f6\u9519\u8bef\u3002\u5c06\u5916\u90e8\u53d8\u91cf\u58f0\u660e\u4e3a final \u4ee5\u9632\u6b62\u610f\u5916\u4fee\u6539\u3002\u8c28\u614e\u4f7f\u7528\u95ed\u5305\uff0c\u907f\u514d\u590d\u6742\u7684\u9519\u8bef\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729951175671cf5c72a7d0.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u521b\u5efa\u548c\u4f7f\u7528 Java \u51fd\u6570\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>Lambda \u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u533f\u540d\u51fd\u6570\u3002\u5b83\u4eec\u5e7f\u6cdb\u7528\u4e8e Java \u4e2d\uff0c\u4ee5\u7b80\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u521b\u5efa\u548c\u4f7f\u7528 Java \u51fd\u6570\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p><strong>1. \u6e05\u6670\u547d\u540d<\/strong><\/p>\n<p>\u660e\u786e\u547d\u540d lambda \u8868\u8fbe\u5f0f\u6709\u52a9\u4e8e\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002\u4f7f\u7528\u53cd\u6620\u51fd\u6570\u76ee\u7684\u7684\u7b80\u6d01\u540d\u79f0\u3002<\/p>\n<p><strong>2. \u8003\u8651\u7c7b\u578b<\/strong><\/p>\n<p>\u6307\u5b9a lambda \u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u6355\u83b7\u9519\u8bef\u3002\u4f7f\u7528\u7c7b\u578b\u6ce8\u91ca\u6216\u663e\u5f0f\u7c7b\u578b\u63a8\u65ad\u3002<\/p>\n<p><strong>3. \u4f7f\u7528 final \u53d8\u91cf<\/strong><\/p>\n<p>\u5982\u679c lambda \u8868\u8fbe\u5f0f\u5f15\u7528\u5916\u90e8\u53d8\u91cf\uff0c\u5e94\u5c06\u8fd9\u4e9b\u53d8\u91cf\u58f0\u660e\u4e3a final\u3002\u8fd9\u53ef\u9632\u6b62\u5bf9\u53d8\u91cf\u7684\u610f\u5916\u4fee\u6539\u3002<\/p>\n<p><strong>4. \u4f7f\u7528\u95ed\u5305\u8c28\u614e<\/strong><\/p>\n<p>\u95ed\u5305\u5141\u8bb8 lambda \u8868\u8fbe\u5f0f\u4fee\u6539\u5916\u90e8\u53d8\u91cf\u3002\u8c28\u614e\u4f7f\u7528\u95ed\u5305\uff0c\u4ee5\u514d\u51fa\u73b0\u590d\u6742\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<pre>\/\/ \u8ba1\u7b97\u5217\u8868\u4e2d\u5143\u7d20\u4e4b\u548c\nList&lt;Integer&gt; numbers = List.of(1, 2, 3, 4, 5);\nint sum = numbers.stream()\n        .map(n -&gt; n) \/\/ \u4fdd\u7559\u539f\u59cb\u503c\n        .reduce(0, (a, b) -&gt; a + b);\nSystem.out.println(sum); \/\/ \u8f93\u51fa\uff1a15\n\n\/\/ \u6839\u636e\u6bd4\u8f83\u5668\u5bf9\u5217\u8868\u6392\u5e8f\nComparator&lt;String&gt; comparator = (s1, s2) -&gt; s1.compareTo(s2);\nList&lt;String&gt; strings = List.of(\"Alice\", \"Bob\", \"Carol\");\nstrings.sort(comparator);\nSystem.out.println(strings); \/\/ \u8f93\u51fa\uff1a[Alice, Bob, Carol]<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\u6709\u52a9\u4e8e\u521b\u5efa\u548c\u4f7f\u7528\u7b80\u6d01\u4e14\u6709\u6548\u7684 Java lambda \u8868\u8fbe\u5f0f\u3002\u6e05\u6670\u547d\u540d\u3001\u9002\u5f53\u7c7b\u578b\u5316\u548c\u8c28\u614e\u4f7f\u7528\u95ed\u5305\u53ef\u4ee5\u63d0\u5347\u4ee3\u7801\u8d28\u91cf\u5e76\u7b80\u5316\u7ef4\u62a4\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u521b\u5efa\u548c\u4f7f\u7528Java\u51fd\u6570\u7684\u6700\u4f73\u5b9e\u8df5\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u521b\u5efa\u548c\u4f7f\u7528&#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-202852","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202852","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=202852"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202852\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}