{"id":202866,"date":"2025-05-22T10:45:10","date_gmt":"2025-05-22T02:45:10","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202866\/"},"modified":"2025-05-22T10:45:10","modified_gmt":"2025-05-22T02:45:10","slug":"lambda%e8%a1%a8%e8%be%be%e5%bc%8f%e8%83%bd%e5%90%a6%e7%94%a8%e4%ba%8e%e5%88%9b%e5%bb%ba%e5%8c%bf%e5%90%8d%e5%86%85%e9%83%a8%e7%b1%bb%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202866\/","title":{"rendered":"Lambda\u8868\u8fbe\u5f0f\u80fd\u5426\u7528\u4e8e\u521b\u5efa\u533f\u540d\u5185\u90e8\u7c7b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Lambda\u8868\u8fbe\u5f0f\u80fd\u5426\u7528\u4e8e\u521b\u5efa\u533f\u540d\u5185\u90e8\u7c7b\uff1f<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aLambda\u8868\u8fbe\u5f0f\u80fd\u5426\u7528\u4e8e\u521b\u5efa\u533f\u540d\u5185\u90e8\u7c7b\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\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><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729952853671cfc5540ea1.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Lambda \u8868\u8fbe\u5f0f\uff1a\u521b\u5efa\u533f\u540d\u5185\u90e8\u7c7b\u7684\u7b80\u4fbf\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u533f\u540d\u5185\u90e8\u7c7b\uff1f<\/strong><\/p>\n<p>\u533f\u540d\u5185\u90e8\u7c7b\u662f\u4e00\u79cd\u53ef\u4ee5\u5728\u521b\u5efa\u65f6\u5b9a\u4e49\u7684\u5185\u90e8\u7c7b\u3002\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u9700\u8981\u7279\u5b9a\u884c\u4e3a\u7684\u63a5\u53e3\u6216\u62bd\u8c61\u7c7b\u7684\u5355\u4e2a\u5b9e\u4f8b\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\u521b\u5efa\u533f\u540d\u5185\u90e8\u7c7b<\/strong><\/p>\n<p>Lambda \u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u521b\u5efa\u533f\u540d\u5185\u90e8\u7c7b\u7684\u7b80\u6d01\u66ff\u4ee3\u65b9\u6cd5\u3002\u5b83\u4eec\u4f7f\u7528\u7bad\u5934\uff08-&gt;\uff09\u8bed\u6cd5\u5b9a\u4e49\uff0c\u5176\u4e2d\u5de6\u4fa7\u662f\u53c2\u6570\u5217\u8868\uff0c\u53f3\u4fa7\u662f\u6267\u884c\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\u521b\u5efa\u5b9e\u73b0 <code>Runnable<\/code> \u63a5\u53e3\u7684\u533f\u540d\u5185\u90e8\u7c7b\uff1a<\/p>\n<pre>new Thread(() -&gt; {\n  \/\/ \u8fd9\u91cc\u6267\u884c\u533f\u540d\u5185\u90e8\u7c7b\u7684\u65b9\u6cd5\n}).start();<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u754c\u9762\u5b9e\u73b0\u573a\u666f\uff0c\u5176\u4e2d\u6211\u4eec\u9700\u8981\u5b9e\u73b0 <code>Comparator<\/code> \u63a5\u53e3\u6765\u5bf9\u4e00\u4e2a <code>List<\/code> \u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684 <code>Comparator<\/code> \u7c7b\u5e76\u8986\u76d6 <code>compare<\/code> \u65b9\u6cd5\u3002<\/p>\n<p>\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u7b80\u6d01\u3001\u66f4\u76f4\u63a5\u5730\u5b9e\u73b0\u6b64\u7c7b\uff1a<\/p>\n<pre>List&lt;String&gt; names = Arrays.asList(\"John\", \"Mary\", \"Bob\");\nCollections.sort(names, (s1, s2) -&gt; s1.compareTo(s2)); \/\/ Lambda \u8868\u8fbe\u5f0f\u6392\u5e8f<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cLambda \u8868\u8fbe\u5f0f\u4f5c\u4e3a\u533f\u540d\u5185\u90e8\u7c7b\uff0c\u5b9e\u73b0\u4e86 <code>compare<\/code> \u65b9\u6cd5\uff0c\u5c06 <code>s1<\/code> \u4e0e <code>s2<\/code> \u8fdb\u884c\u6bd4\u8f83\u5e76\u8fd4\u56de\u9002\u5f53\u7684\u6574\u6570\u3002<\/p>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<ul>\n<li><strong>\u7b80\u6d01\uff1a<\/strong>Lambda \u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u6bd4\u533f\u540d\u5185\u90e8\u7c7b\u66f4\u7b80\u6d01\u3001\u66f4\u65b9\u4fbf\u7684\u65b9\u5f0f\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\uff1a<\/strong>Lambda \u8868\u8fbe\u5f0f\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u6355\u83b7\u5916\u90e8\u4f5c\u7528\u57df\u7684\u53d8\u91cf\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027\uff1a<\/strong>\u5bf9\u4e8e\u7b80\u5355\u7684\u5b9e\u73b0\uff0cLambda \u8868\u8fbe\u5f0f\u5f80\u5f80\u6bd4\u533f\u540d\u5185\u90e8\u7c7b\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/li>\n<\/ul>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>Lambda \u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u521b\u5efa\u533f\u540d\u5185\u90e8\u7c7b\u7684\u5f3a\u5927\u800c\u65b9\u4fbf\u7684\u5de5\u5177\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u7b80\u6d01\u3001\u7075\u6d3b\u548c\u53ef\u8bfb\u7684\u8bed\u6cd5\uff0c\u4f7f\u60a8\u80fd\u591f\u5feb\u901f\u9ad8\u6548\u5730\u5b9e\u73b0\u6240\u9700\u7684\u7279\u5b9a\u884c\u4e3a\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lambda\u8868\u8fbe\u5f0f\u80fd\u5426\u7528\u4e8e\u521b\u5efa\u533f&#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-202866","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202866","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=202866"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202866\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}