{"id":148,"date":"2025-07-19T18:13:12","date_gmt":"2025-07-19T10:13:12","guid":{"rendered":"http:\/\/192.140.166.102\/?p=148"},"modified":"2025-07-19T18:13:12","modified_gmt":"2025-07-19T10:13:12","slug":"lambda%e8%a1%a8%e8%be%be%e5%bc%8f%ef%bc%8c%e8%ae%a9%e4%bb%a3%e7%a0%81%e7%bc%96%e5%86%99%e6%9b%b4%e5%8a%a0%e7%ae%80%e6%98%93","status":"publish","type":"post","link":"https:\/\/lsuccess.xyz\/index.php\/2025\/07\/19\/lambda%e8%a1%a8%e8%be%be%e5%bc%8f%ef%bc%8c%e8%ae%a9%e4%bb%a3%e7%a0%81%e7%bc%96%e5%86%99%e6%9b%b4%e5%8a%a0%e7%ae%80%e6%98%93\/","title":{"rendered":"Lambda\u8868\u8fbe\u5f0f\uff0c\u8ba9\u4ee3\u7801\u7f16\u5199\u66f4\u52a0\u7b80\u6613"},"content":{"rendered":"\n<ul class=\"wp-block-list\">\n<li>\u521d\u8bc6Lambda\u8868\u8fbe\u5f0f<\/li>\n\n\n\n<li>Lambda\u8bed\u6cd5<\/li>\n\n\n\n<li>Lambda\u548cthis\u5173\u952e\u5b57<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u521d\u8bc6Lambda\u8868\u8fbe\u5f0f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>() -> System.out.println(\"hello\uff0cthere\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c31\u662f\u4e00\u4e2a\u5f88\u7b80\u5355\u7684Lambda\u8868\u8fbe\u5f0f\uff0c\u5728\u8fd9\u91cc\u6211\u5c06\u548c\u6211\u7684\u533f\u540d\u7c7b\u8fdb\u884c\u5bf9\u6bd4\uff0c\u539f\u6765\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u5e76\u542f\u52a8\u5b83\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class LamadaTest {\n    public static void main(String&#91;] args) {\n        new Thread(new Runnable() {\n            @Override\n            public void run() {\n                System.out.println(\"hello\uff0cthere\");\n            }\n        }).start();\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528Lambda\u8868\u8fbe\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class LamadaTest {\n    public static void main(String&#91;] args) {\n        new Thread(() -> System.out.println(\"\u6c89\u9ed8\u738b\u4e8c\")).start();\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6574\u4f53\u4e0a\u5c31\u7b80\u5355\u4e86\u8bb8\u591a\u4e0d\u662f\u5417<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lambda\u8bed\u6cd5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bcf\u4e2a Lambda \u8868\u8fbe\u5f0f\u90fd\u9075\u5faa\u4ee5\u4e0b\u6cd5\u5219\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>( parameter-list ) -&gt; { expression-or-statements }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>()<\/code>&nbsp;\u4e2d\u7684&nbsp;<code>parameter-list<\/code>&nbsp;\u662f\u4ee5\u9017\u53f7\u5206\u9694\u7684\u53c2\u6570\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u6307\u5b9a\uff08\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u4e0a\u4e0b\u6587\u8fdb\u884c\u63a8\u65ad\uff09\u3002Java 11 \u540e\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528&nbsp;<code>var<\/code>&nbsp;\u5173\u952e\u5b57\u4f5c\u4e3a\u53c2\u6570\u7c7b\u578b\uff0c\u6709\u70b9 JavaScript \u7684\u5473\u9053\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>-><\/code>\u00a0\u76f8\u5f53\u4e8e Lambda \u7684\u6807\u8bc6\u7b26<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>{}<\/code>&nbsp;\u4e2d\u7684&nbsp;<code>expression-or-statements<\/code>&nbsp;\u4e3a Lambda \u7684\u4e3b\u4f53\uff0c\u53ef\u4ee5\u662f\u4e00\u884c\u8bed\u53e5\uff0c\u4e5f\u53ef\u4ee5\u591a\u884c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u901a\u8fc7 Lambda \u8868\u8fbe\u5f0f\u5e72\u5f88\u591a\u4e8b\u60c5\uff0c\u6bd4\u5982\u8bf4<br>1\uff09\u4e3a\u53d8\u91cf\u8d4b\u503c\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Runnable r = () -> { System.out.println(\"hello\uff0cthere\"); };\nr.run();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2\uff09\u4f5c\u4e3a return \u7ed3\u679c\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static FileFilter getFilter(String ext)\n{\n    return (pathname) -&gt; pathname.toString().endsWith(ext);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3\uff09\u4f5c\u4e3a\u6570\u7ec4\u5143\u7d20\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>final PathMatcher matchers&#91;] =\n{\n        (path) -&gt; path.toString().endsWith(\"txt\"),\n        (path) -&gt; path.toString().endsWith(\"java\")\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4\uff09\u4f5c\u4e3a\u666e\u901a\u65b9\u6cd5\u6216\u8005\u6784\u9020\u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>new Thread(() -&gt; System.out.println(\"\u6c89\u9ed8\u738b\u4e8c\")).start();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9700\u8981\u6ce8\u610f Lambda \u8868\u8fbe\u5f0f\u7684\u4f5c\u7528\u57df\u8303\u56f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n    int limit = 10;\n    Runnable r = () -&gt; {\n        int limit = 5;\n        for (int i = 0; i &lt; limit; i++)\n            System.out.println(i);\n    };\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u5728\u7f16\u8bd1\u7684\u65f6\u5019\u4f1a\u63d0\u793a\u9519\u8bef\uff1a\u53d8\u91cf limit \u5df2\u7ecf\u5b9a\u4e49\u8fc7\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u548c\u533f\u540d\u5185\u90e8\u7c7b\u4e00\u6837\uff0c\u4e0d\u8981\u5728 Lambda \u8868\u8fbe\u5f0f\u4e3b\u4f53\u5185\u5bf9\u65b9\u6cd5\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u8fdb\u884c\u4fee\u6539\uff0c\u5426\u5219\u7f16\u8bd1\u4e5f\u4e0d\u4f1a\u901a\u8fc7\uff1aLambda \u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u5fc5\u987b\u662f final \u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lambda\u548cthis\u5173\u952e\u5b57<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Lambda \u8868\u8fbe\u5f0f\u5e76\u4e0d\u4f1a\u5f15\u5165\u65b0\u7684\u4f5c\u7528\u57df\uff0c\u8fd9\u4e00\u70b9\u548c\u533f\u540d\u5185\u90e8\u7c7b\u662f\u4e0d\u540c\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cLambda \u8868\u8fbe\u5f0f\u4e3b\u4f53\u5185\u4f7f\u7528\u7684 this \u5173\u952e\u5b57\u548c\u5176\u6240\u5728\u7684\u7c7b\u5b9e\u4f8b\u76f8\u540c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u521d\u8bc6Lambda\u8868\u8fbe\u5f0f \u8fd9\u5c31\u662f\u4e00\u4e2a\u5f88\u7b80\u5355\u7684Lambda\u8868\u8fbe\u5f0f\uff0c\u5728\u8fd9\u91cc\u6211\u5c06\u548c\u6211\u7684\u533f\u540d\u7c7b\u8fdb\u884c\u5bf9\u6bd4\uff0c\u539f\u6765\u6211\u4eec\u521b\u5efa\u4e00\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":149,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learn"],"_links":{"self":[{"href":"https:\/\/lsuccess.xyz\/index.php\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lsuccess.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lsuccess.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lsuccess.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lsuccess.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":1,"href":"https:\/\/lsuccess.xyz\/index.php\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":150,"href":"https:\/\/lsuccess.xyz\/index.php\/wp-json\/wp\/v2\/posts\/148\/revisions\/150"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lsuccess.xyz\/index.php\/wp-json\/wp\/v2\/media\/149"}],"wp:attachment":[{"href":"https:\/\/lsuccess.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lsuccess.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lsuccess.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}