{"id":202807,"date":"2025-05-22T08:42:46","date_gmt":"2025-05-22T00:42:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202807\/"},"modified":"2025-05-22T08:42:46","modified_gmt":"2025-05-22T00:42:46","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8java%e4%b8%ad%e4%bd%bf%e7%94%a8lambda%e8%a1%a8%e8%be%be%e5%bc%8f%e8%bf%9b%e8%a1%8c%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202807\/","title":{"rendered":"\u5982\u4f55\u5728Java\u4e2d\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u8fdb\u884c\u51fd\u6570\u5f0f\u7f16\u7a0b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728Java\u4e2d\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u8fdb\u884c\u51fd\u6570\u5f0f\u7f16\u7a0b\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728Java\u4e2d\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u8fdb\u884c\u51fd\u6570\u5f0f\u7f16\u7a0b\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729944326671cdb068f093.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\u8fdb\u884c\u51fd\u6570\u5f0f\u7f16\u7a0b<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>Lambda \u8868\u8fbe\u5f0f\u662f Java 8 \u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u7b80\u5316\u533f\u540d\u51fd\u6570\u521b\u5efa\u7684\u8bed\u6cd5\u7279\u5f81\u3002\u5b83\u4eec\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u4e8e\u9605\u8bfb\uff0c\u4ece\u800c\u63d0\u5347\u4e86\u51fd\u6570\u5f0f\u7f16\u7a0b\u80fd\u529b\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\uff0c\u5e76\u63d0\u4f9b\u5b9e\u9645\u6848\u4f8b\u4ee5\u5c55\u793a\u5176\u7528\u6cd5\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>Lambda \u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>(parameters) -&gt; body<\/pre>\n<p>\u5176\u4e2d <code>parameters<\/code> \u662f\u51fd\u6570\u53c2\u6570\u5217\u8868\uff0c<code>body<\/code> \u662f\u51fd\u6570\u4f53\u3002<\/p>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<p>\u521b\u5efa\u7b80\u5355\u7684 Lambda \u8868\u8fbe\u5f0f\uff0c\u53ea\u9700\u5c06\u53c2\u6570\u5217\u8868\u548c\u51fd\u6570\u4f53\u653e\u5728\u7bad\u5934\u7b26\u53f7 <code>-&gt;<\/code> \u5185\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>(x) -&gt; x * 2<\/pre>\n<p>\u8fd9\u4e2a Lambda \u8868\u8fbe\u5f0f\u5c06\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 <code>x<\/code> \u5e76\u8fd4\u56de\u5176\u4e24\u500d\u3002<\/p>\n<p><strong>\u7c7b\u578b\u58f0\u660e<\/strong><\/p>\n<p>Lambda \u8868\u8fbe\u5f0f\u53ef\u4ee5\u6709\u660e\u786e\u7684\u7c7b\u578b\u58f0\u660e\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>(parameters) -&gt; {...} : return-type<\/pre>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>(String s) -&gt; s.toUpperCase() : String<\/pre>\n<p>\u8fd9\u4e2a Lambda \u8868\u8fbe\u5f0f\u5c06\u63a5\u53d7\u4e00\u4e2a <code>String<\/code> \u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5927\u5199\u5f62\u5f0f\u7684 <code>String<\/code>\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u8fc7\u6ee4\u96c6\u5408<\/strong><\/p>\n<pre>List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5, 6);\n\nList&lt;Integer&gt; evenNumbers = numbers\n    .stream() \/\/ \u521b\u5efa\u6d41\n    .filter(x -&gt; x % 2 == 0) \/\/ \u8fc7\u6ee4\u5076\u6570\n    .collect(Collectors.toList()); \/\/ \u6536\u96c6\u7ed3\u679c\u5230 List \u4e2d\n\nevenNumbers.forEach(System.out::println); \/\/ \u6253\u5370\u5076\u6570<\/pre>\n<p><strong>\u6620\u5c04\u96c6\u5408<\/strong><\/p>\n<pre>List&lt;String&gt; names = Arrays.asList(\"John\", \"Mary\", \"Bob\");\n\nList&lt;String&gt; upperNames = names\n    .stream()\n    .map(s -&gt; s.toUpperCase()) \/\/ \u5c06\u6bcf\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199\n    .collect(Collectors.toList());\n\nupperNames.forEach(System.out::println); \/\/ \u6253\u5370\u5927\u5199\u5b57\u7b26\u4e32<\/pre>\n<p><strong>\u901a\u8fc7\u95ed\u5305\u83b7\u53d6\u5916\u56f4\u53d8\u91cf<\/strong><\/p>\n<pre>int x = 10;\n\nRunnable runnable = () -&gt; System.out.println(x);\n\nrunnable.run(); \/\/ \u6253\u5370 10<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u5728 Java \u4e2d\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\u7b80\u5316\u4e86\u521b\u5efa\u533f\u540d\u51fd\u6570\u7684\u8fc7\u7a0b\uff0c\u4fc3\u8fdb\u4e86\u51fd\u6570\u5f0f\u7f16\u7a0b\u6a21\u5f0f\u7684\u91c7\u7528\u3002\u901a\u8fc7\u7406\u89e3\u5b83\u4eec\u7684\u8bed\u6cd5\u548c\u7528\u6cd5\uff0c\u4f60\u73b0\u5728\u53ef\u4ee5\u901a\u8fc7 Lambda \u8868\u8fbe\u5f0f\u589e\u5f3a\u4ee3\u7801\u7684\u6e05\u6670\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u5728Java\u4e2d\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u8fdb\u884c\u51fd\u6570\u5f0f\u7f16\u7a0b\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\u4e8e\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728Java\u4e2d\u4f7f\u7528lambda&#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-202807","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202807","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=202807"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202807\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}