{"id":21988,"date":"2024-09-04T14:32:06","date_gmt":"2024-09-04T06:32:06","guid":{"rendered":"https:\/\/aif.amtbbs.org\/?p=21988"},"modified":"2024-09-04T14:32:06","modified_gmt":"2024-09-04T06:32:06","slug":"%e4%b8%80%e4%b8%aa%e8%83%bd%e8%ae%a9%e4%bd%a0%e5%b0%91%e5%86%99%e5%be%aa%e7%8e%af%e5%92%8c%e5%88%a4%e6%96%ad%e7%9a%84go%e5%bc%80%e6%ba%90%e5%8c%85%ef%bc%8c%e6%94%af%e6%8c%81%e8%8c%83%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/aif.amtbbs.org\/index.php\/2024\/09\/04\/21988\/","title":{"rendered":"\u4e00\u4e2a\u80fd\u8ba9\u4f60\u5c11\u5199\u5faa\u73af\u548c\u5224\u65ad\u7684Go\u5f00\u6e90\u5305\uff0c\u652f\u6301\u8303\u578b"},"content":{"rendered":"<div><img data-dominant-color=\"878991\" data-has-transparency=\"false\" style=\"--dominant-color: #878991;\" loading=\"lazy\" decoding=\"async\" class=\"not-transparent alignnone size-full wp-image-21990\" src=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/09\/a3e7f48506e7ea856d886530d32adb418440c2-300x167-1.jpg\" width=\"300\" height=\"167\" alt=\"\" srcset=\"https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/09\/a3e7f48506e7ea856d886530d32adb418440c2-300x167-1.jpg 300w, https:\/\/aiforumimage.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2024\/09\/a3e7f48506e7ea856d886530d32adb418440c2-300x167-1-150x84.jpg 150w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/div>\n<div class=\"article-desc\">\u4eca\u5929\u4ecb\u7ecd\u7684lo\u200b\u5e93\u5927\u5bb6\u53ef\u4ee5\u5c1d\u8bd5\u7528\u8d77\u6765\uff0c\u7b49\u7528\u4e60\u60ef\u4e86\u786e\u5b9e\u80fd\u8ba9\u81ea\u5df1\u7684\u4ee3\u7801\u5c11\u5199\u4e0d\u5c11\u5faa\u73af\u52a0\u5224\u65ad\u7684\u903b\u8f91\uff0c\u6574\u4f53\u98ce\u683c\u4f1a\u66f4\u6e05\u723d\uff0c\u81ea\u5df1\u4e5f\u80fd\u5c11\u5199\u4e00\u4e9b\u4ee3\u7801\u3002\u5982\u679c\u4f60\u4eec\u8fd8\u5728\u7528\u7684Go\u7248\u672c\u4e0d\u652f\u6301\u8303\u578b\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u7528\u4e00\u4e0b\u98ce\u683c\u7c7b\u4f3c\u7684\u5e93go-funk\u3002<\/div>\n<div id=\"postspictures\" class=\"article-content\">\n<div id=\"container\" class=\"container am-engine\" data-v-1d7a5742=\"\" data-element=\"root\">\n<p>\u5927\u5bb6\u5728\u5f00\u53d1\u9879\u76ee\u5199\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u6700\u5e38\u7528\u5230\u7684\u6570\u636e\u7c7b\u578b\u5e94\u8be5\u662f\u5217\u8868\uff0c\u6bd4\u5982\u4ece\u6570\u636e\u5e93\u67e5\u8be2\u4e00\u4e2a\u7528\u6237\u7684\u8ba2\u5355\uff0c\u67e5\u8be2\u7ed3\u679c\u4f1a\u4ee5\u4e00\u4e2a\u5bf9\u8c61\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u7ed9\u8c03\u7528\u7a0b\u5e8f\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_0\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\"><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">*<\/span><span class=\"token keyword\">Order<\/span> {\r\n    <span class=\"token operator\">&amp;<\/span>{\r\n        ID: <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>\r\n        OrderNo: <span class=\"token string\">\"20240903628359373756980001\"<\/span>\r\n        <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span>\r\n    }<span class=\"token punctuation\">,<\/span>\r\n    <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span>\r\n}<\/code><\/pre>\n<ul id=\"code_id_0\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u6709\u4e86\u7ed3\u679c\u96c6\u5217\u8868\u4e4b\u540e\uff0c\u5927\u90e8\u5206\u65f6\u5019\u4e3a\u4e86\u5b9e\u73b0\u4ea7\u54c1\u903b\u8f91\u6211\u4eec\u9700\u8981\u5728\u8fd9\u4e2a\u5217\u8868\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u5224\u65ad\u3001\u7b5b\u9009\u548c\u52a0\u5de5\u51fa\u81ea\u5df1\u60f3\u8981\u7684\u6570\u636e\u96c6\u3002<\/p>\n<p>\u56e0\u4e3a\u5217\u8868\u662f\u591a\u4e2a\u540c\u7c7b\u6570\u636e\u7684\u96c6\u5408\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u9700\u8981\u6211\u4eec\u5728\u904d\u5386\u5217\u8868\u7684\u57fa\u7840\u4e0a\u6765\u5b8c\u6210\uff0c\u6bd4\u5982\u5224\u65ad ID \u4e3a1 \u7684\u8ba2\u5355\u5728\u4e0d\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_1\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\"><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span>\r\nvar <span class=\"token keyword\">exists<\/span> <span class=\"token keyword\">bool<\/span>\r\n<span class=\"token keyword\">for<\/span> _<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">order<\/span> :<span class=\"token operator\">=<\/span> range orders {\r\n    <span class=\"token keyword\">if<\/span> <span class=\"token keyword\">order<\/span><span class=\"token punctuation\">.<\/span>ID <span class=\"token operator\">=<\/span><span class=\"token operator\">=<\/span> <span class=\"token number\">1<\/span> {\r\n        <span class=\"token keyword\">exists<\/span> <span class=\"token operator\">=<\/span> <span class=\"token boolean\">true<\/span>\r\n    }\r\n}\r\n<span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><\/code><\/pre>\n<ul id=\"code_id_1\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5f53\u7136\uff0c\u4e3a\u4e86\u51cf\u5c11\u904d\u5386\u6b21\u6570\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5148\u901a\u8fc7\u4e00\u6b21\u904d\u5386\u751f\u6210\u4e00\u4e2a\u4ee5\u6570\u636e\u4e3b\u952e\u4e3aKey\u7684\u54c8\u5e0cMap\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_2\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">map<span class=\"token punctuation\">[<\/span>int64<span class=\"token punctuation\">]<\/span><span class=\"token keyword\">Order<\/span> {\r\n    <span class=\"token string\">\"1\"<\/span>: {\r\n       ID: <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>\r\n       OrderNo: <span class=\"token string\">\"20240903628359373756980001\"<\/span>\r\n       <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span> \r\n    }\r\n}<\/code><\/pre>\n<ul id=\"code_id_2\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u5217\u8868\u548c\u54c8\u5e0cMap\u5728Go\u91cc\u7684\u7c7b\u578b\u662fSlice \u548c Map\uff0c\u4e0a\u9762\u8fd9\u4e9b\u64cd\u4f5c\u5e94\u8be5\u662f\u5927\u5bb6\u5199\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u5dee\u4e0d\u591a\u6bcf\u5929\u90fd\u4f1a\u9047\u5230\u7684\u60c5\u51b5\u3002\u6bd4\u5982\uff0c\u4eceSlice\u5207\u7247\u4e2d\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u3001\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u662f\u4e0d\u662f\u5b58\u5728\u3001\u67e5\u627e\u6240\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u53c8\u6bd4\u5982\u83b7\u53d6Map\u7684\u6240\u6709key\u3001\u6240\u6709\u7684value\u3001\u8fd8\u6709\u50cf\u4e0a\u9762\u8bf4\u7684\u628aSlice \u8f6c\u6362\u6210 Map\u3002<\/p>\n<p>\u8fd9\u4e9b\u64cd\u4f5c\u5728\u6240\u6709\u7f16\u7a0b\u8bed\u8a00\u91cc\u90fd\u5f88\u5e38\u89c1\uff0c\u6bd4\u5982Javascript\u91cc\u6570\u7ec4\u7684map\u3001reduce\u3001filter\u51fd\u6570\uff0cJava \u7684 Stream API\u5728\u7f16\u7a0b\u4e2d\u90fd\u975e\u5e38\u597d\u7528\uff0c\u4f46\u662f\u9057\u61be\u7684\u662fGo\u6807\u51c6\u5e93\u6ca1\u6709\u63d0\u4f9b\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4e3a\u4e86\u4e0d\u5728\u6bcf\u4e2a\u51fd\u6570\u91cc\u90fd\u5199\u4e00\u904d\uff0c\u5f88\u591a\u9879\u76ee\u91cc\u4f1a\u7f16\u5199\u5927\u91cf\u7684\u5de5\u5177\u51fd\u6570\u6765\u8fdb\u884cSlice\u548cMap\u6570\u636e\u7684\u5904\u7406\uff0c\u76f8\u4fe1\u4f60\u4e00\u5b9a\u5728\u81ea\u5df1\u5199\u8fc7\u7684\u9879\u76ee\u91cc\u89c1\u8fc7\u4e00\u4e2a\u53eb\u00a0util\u7684\u5305\uff0c\u91cc\u9762\u5199\u4e86\u5404\u79cd InSlice, InArray, XXXInSlice \u7b49\u7b49\u4e4b\u7c7b\u7684\u5de5\u5177\u51fd\u6570\u3002<\/p>\n<p>\u5f53\u7136\u8fd9\u4e9b\u4e5f\u4e0d\u7528\u6bcf\u6b21\u505a\u9879\u76ee\u90fd\u5199\u4e00\u904d\uff0c\u5927\u90e8\u5206\u901a\u7528\u7684\u53ef\u4ee5\u5148\u4ece\u8001\u9879\u76ee\u7c98\u5230\u65b0\u9879\u76ee\u91cc\u53bb\u3002\u3002\u3002\u4f46\u662fGo\u4ee5\u524d\u4e0d\u652f\u6301\u8303\u6027\uff0c\u8fd9\u79cd\u5de5\u5177\u51fd\u6570\u9488\u5bf9\u9879\u76ee\u7528\u5230\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u90fd\u5199\u4e00\u904d\u4e5f\u662f\u4e00\u4e2a\u95ee\u9898\uff0c\u65f6\u95f4\u957f\u4e86\u4e5f\u9700\u8981\u4eba\u6765\u7ef4\u62a4\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528\u793e\u533a\u91cc\u7ecf\u8fc7\u5145\u5206\u7684\u6d4b\u8bd5\u3001\u9a8c\u8bc1\uff0c\u5e76\u4e14\u7ecf\u5e38\u66f4\u65b0\u7684\u5f00\u6e90\u5e93\uff0c\u5728Go 1.18 \u7248\u672c\u4ee5\u524d\u6bd4\u8f83\u6709\u540d\u7684\u51fd\u6570\u5e93\u662fgo-funk\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f88\u591a\u597d\u7528\u7684\u51fd\u6570\uff1aContains\u3001Difference\u3001IndexOf\u3001Filter\u3001ToMap\u7b49\u7b49\uff0c\u66f4\u591a\u7684\u53ef\u4ee5\u53c2\u8003\u5b83\u7684\u7f51\u7ad9\uff1ahttps:\/\/github.com\/thoas\/go-funk<\/p>\n<p>\u56e0\u4e3a\u5b83\u662f\u5728Go1.18 \u4ee5\u524d\u51fa\u6765\u7684\uff0c\u6240\u4ee5\u4e0d\u53ef\u907f\u514d\u7684\u4f1a\u7528\u5230\u53cd\u5c04\u6765\u5904\u7406\u591a\u7c7b\u578b\u9002\u914d\u7684\u95ee\u9898\uff0c\u4e3e\u4e2aContains\uff0c\u5373\u5224\u65ad\u662f\u4e0d\u662f InSlice\u7684\u4f8b\u5b50\uff0c\u5047\u5982\u4e0d\u7528\u53cd\u5c04\uff0c\u8981\u591a\u7c7b\u578b\u4f7f\u7528\uff0c\u5c31\u5f97\u5b9a\u4e49\u5f88\u591a\u76f8\u4f3c\u540d\u79f0\u7684\u51fd\u6570\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_3\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">func ContainsInt<span class=\"token punctuation\">(<\/span>collection <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token keyword\">int<\/span><span class=\"token punctuation\">,<\/span> x <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">bool<\/span> {\r\n\r\n}\r\n\r\nfunc ContainsString<span class=\"token punctuation\">(<\/span>collection <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span>string<span class=\"token punctuation\">,<\/span> x string<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">bool<\/span> {\r\n\r\n}<\/code><\/pre>\n<ul id=\"code_id_3\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u8fd9\u8ddf\u54b1\u4eec\u81ea\u5df1\u5199\u5de5\u5177\u51fd\u6570\u5c31\u6ca1\u4ec0\u4e48\u533a\u522b\u4e86\uff0c\u5728Go\u8bed\u8a00\u7684\u6cdb\u578b\u652f\u6301\u4e4b\u524d\uff0c\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5c31\u80fd\u7528\u53cd\u5c04\u3002\u5047\u5982\u4f60\u4eec\u73b0\u5728\u7684\u9879\u76ee\u8fd8\u5728\u7528Go1.18 \u4ee5\u524d\u7684\u7248\u672c\uff0c\u53c8\u4e0d\u60f3\u5199\u81ea\u5df1\u624b\u5199\u90a3\u4e48\u591a\u5faa\u73af\u548c\u5224\u65ad\u4ee3\u7801\uff0c\u90a3\u8fd8\u662f\u5c31\u7528go-funk\u5427\u3002<\/p>\n<p>Go 1.18 \u652f\u6301\u4e86\u8303\u578b\u4ee5\u540e\uff0c\u5f88\u5feb\u5c31\u6709\u4eba\u7528\u8303\u578b\u5199\u51fa\u4e86\u4e0ego-funk\u529f\u80fd\u76f8\u540c\u7684\u51fd\u6570\u5305\uff0c\u8fd9\u4e2a\u5305\u5c31\u662f\u4eca\u5929\u8981\u4ecb\u7ecd\u7684\u4e3b\u89d2\uff0c\u5b83\u53eb\u00a0lo\uff0c\u540d\u5b57\u6709\u70b9\u602a\uff0c\u4f46\u662f\u7b80\u4ecb\u91cc\u5df2\u7ecf\u5199\u6e05\u695a\u4e86\u5b83\u7684\u7528\u9014: A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find&#8230;)<\/p>\n<p>\u4e00\u4e2a\u57fa\u4e8e Go 1.18+ \u7684\u8303\u578b\uff0c\u63d0\u4f9b map, filter, contains, find&#8230; \u7b49\u64cd\u4f5c\u7684\uff0c\u7c7b\u4f3c JS Lodash \u5de5\u5177\u5305\u98ce\u683c\u7684\u5de5\u5177\u5305\uff0c\u54c8\u54c8\u54c8\uff0c\u7ffb\u8bd1\u8fc7\u6765\u5b57\u513f\u6709\u70b9\u591a\u3002<\/p>\n<p>\u5b83\u662f\u57fa\u4e8e\u6cdb\u578b\u5b9e\u73b0\uff0c\u6ca1\u6709\u7528\u5230\u53cd\u5c04\uff0c\u6548\u7387\u66f4\u9ad8\uff0c\u4ee3\u7801\u4e5f\u66f4\u7b80\u6d01\u3002\u6bd4\u5982\u521a\u624d\u8bf4\u7684Contains\u51fd\u6570\uff0c\u662f\u8fd9\u4e48\u5b9e\u73b0\u7684\uff1a<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_4\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">func <span class=\"token keyword\">Contains<\/span><span class=\"token punctuation\">[<\/span>T comparable<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">(<\/span>collection <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span>T<span class=\"token punctuation\">,<\/span> element T<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">bool<\/span> {\r\n <span class=\"token keyword\">for<\/span> i :<span class=\"token operator\">=<\/span> range collection {\r\n  <span class=\"token keyword\">if<\/span> collection<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span><span class=\"token operator\">=<\/span> element {\r\n   <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">true<\/span>\r\n  }\r\n }\r\n\r\n <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span>\r\n}<\/code><\/pre>\n<ul id=\"code_id_4\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u53ea\u9700\u8981 T \u88ab\u7ea6\u675f\u4e3a comparable \u7684\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528==\u7b26\u53f7\u8fdb\u884c\u6bd4\u8f83\u4e86\uff0c\u6574\u4f53\u4ee3\u7801\u975e\u5e38\u7b80\u5355\uff0c\u5982\u679c\u4f60\u81ea\u5df1\u5199\u4ee3\u7801\u7684\u65f6\u5019\u9700\u8981\u7528\u5230\u8303\u578b\uff0c\u53ef\u4ee5\u5148\u5b66\u4e60\u5b66\u4e60\u5b83\u6e90\u7801\u4e2d\u5bf9Go\u8303\u578b\u7684\u5404\u79cd\u4f7f\u7528\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u7ed9\u5927\u5bb6\u6f14\u793a\u4e00\u4e9b\u6211\u4eec\u5e38\u7528\u5230\u7684\u64cd\u4f5c\u4f7f\u7528 lo \u5e93\u7684\u5de5\u5177\u51fd\u6570\u65f6\u5e94\u8be5\u600e\u4e48\u5199\u3002<\/p>\n<h2>\u5e38\u7528\u7684Slice \u548c Map \u64cd\u4f5c<\/h2>\n<p>\u9996\u5148 lo \u5e93\u91cc\u63d0\u4f9b\u4e86\u975e\u5e38\u591a\u7684\u5173\u4e8e Slice\u3001Map\u3001String\u3001Channel \u7684\u64cd\u4f5c\uff0c \u4e0d\u8fc7\u5b98\u65b9\u7ed9\u7684\u4f8b\u5b50\u6bd4\u8f83\u7b80\u5355\u90fd\u662f\u9488\u5bf9Int\u3001String \u5207\u7247\u8fd9\u6837\u57fa\u7840\u7c7b\u578b\u96c6\u5408\u7684\u64cd\u4f5c\uff0c\u6211\u8fd9\u91cc\u7ed9\u5927\u5bb6\u6f14\u793a\u4e00\u4e9b\u6211\u4eec\u5b9e\u9645\u5f00\u53d1\u65f6\u4f1a\u7528\u5230\u7684\u5173\u4e8e[]*Order\u00a0\u8fd9\u6837\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684Slice \u548c Map \u7684\u64cd\u4f5c\u3002<\/p>\n<h3>Filter \u7b5b\u9009\u7b26\u5408\u6761\u4ef6\u7684\u5b50\u5217\u8868<\/h3>\n<p>\u5047\u5982\u6211\u4eec\u6709\u4e00\u4e2a\u50cf\u4e0b\u9762\u8fd9\u6837\u7684\u8ba2\u5355\u5217\u8868<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_5\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\"><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">*<\/span><span class=\"token keyword\">Order<\/span> {\r\n    <span class=\"token operator\">&amp;<\/span>{\r\n        ID: <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>\r\n        OrderNo: <span class=\"token string\">\"20240903628359373756980001\"<\/span>\r\n        UserId: <span class=\"token number\">255<\/span>\r\n        <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span>\r\n    }<span class=\"token punctuation\">,<\/span>\r\n    <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span>\r\n}<\/code><\/pre>\n<ul id=\"code_id_5\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u6211\u4eec\u8981\u7b5b\u9009\u51fa\u8ba2\u5355\u5217\u8868\u4e2d UserId \u5b57\u6bb5\u503c\u7b49\u4e8e\u53c2\u6570\u00a0userId\u00a0\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_6\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">func FindUserOrders<span class=\"token punctuation\">(<\/span>orders <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">*<\/span><span class=\"token keyword\">Order<\/span><span class=\"token punctuation\">,<\/span> userId int64<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">*<\/span><span class=\"token keyword\">Order<\/span> { \r\n    \r\n    userOrders :<span class=\"token operator\">=<\/span> lo<span class=\"token punctuation\">.<\/span>Filter<span class=\"token punctuation\">(<\/span>orders<span class=\"token punctuation\">,<\/span> func<span class=\"token punctuation\">(<\/span>item <span class=\"token operator\">*<\/span><span class=\"token keyword\">Order<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">index<\/span> <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">bool<\/span> {\r\n      <span class=\"token keyword\">return<\/span> item<span class=\"token punctuation\">.<\/span>UserId <span class=\"token operator\">=<\/span><span class=\"token operator\">=<\/span> userId\r\n    }<span class=\"token punctuation\">)<\/span>\r\n\r\n    <span class=\"token keyword\">return<\/span> userOrders\r\n}<\/code><\/pre>\n<ul id=\"code_id_6\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3>\u4ece\u8ba2\u5355\u5217\u8868\u4e2d\u63d0\u53d6\u51fa\u6240\u6709\u8ba2\u5355ID<\/h3>\n<p>\u6709\u7684\u65f6\u5019\u6211\u4eec\u5e0c\u671b\u4ece\u5217\u8868\u4e2d\u63d0\u53d6\u51fa\u6240\u6709ID\uff0c\u518d\u53bb\u505a\u8fdb\u4e00\u6b65\u7684\u6570\u636e\u5e93\u7684 IN (idList) \u7684\u67e5\u8be2\uff0c\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Map \u51fd\u6570\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_7\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">orderIds :<span class=\"token operator\">=<\/span> lo<span class=\"token punctuation\">.<\/span>Map<span class=\"token punctuation\">(<\/span>orders<span class=\"token punctuation\">,<\/span> func<span class=\"token punctuation\">(<\/span>item <span class=\"token operator\">*<\/span><span class=\"token keyword\">Order<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">index<\/span> <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span> int64 {\r\n    <span class=\"token keyword\">return<\/span> item<span class=\"token punctuation\">.<\/span>ID\r\n}<span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<ul id=\"code_id_7\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3>\u628a\u5217\u8868\u8f6c\u6362\u6210Map<\/h3>\n<p>\u6587\u7ae0\u5f00\u5934\u63d0\u5230\u8fc7\uff0c\u5f88\u591a\u65f6\u5019\u4e3a\u4e86\u51cf\u5c11\u904d\u5386\u6b21\u6570\u4f1a\u6709\u628a\u5217\u8868\u8f6c\u6362\u6210\u4ee5ID \u4e3a Key Map\u7684\u9700\u6c42\uff0c\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 lo \u5e93\u7684 SliceToMap \u6765\u5b9e\u73b0<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_8\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">orderMap :<span class=\"token operator\">=<\/span> lo<span class=\"token punctuation\">.<\/span>SliceToMap<span class=\"token punctuation\">(<\/span>orders<span class=\"token punctuation\">,<\/span> func<span class=\"token punctuation\">(<\/span>item <span class=\"token operator\">*<\/span><span class=\"token keyword\">Order<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">(<\/span>int64<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">*<\/span><span class=\"token keyword\">Order<\/span><span class=\"token punctuation\">)<\/span> {\r\n    <span class=\"token keyword\">return<\/span> item<span class=\"token punctuation\">.<\/span>ID<span class=\"token punctuation\">,<\/span> item\r\n}<span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<ul id=\"code_id_8\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3>\u8ba9\u5217\u8868\u6309\u5b57\u6bb5\u8fdb\u884c\u5206\u7ec4<\/h3>\n<p>\u5982\u679c\u4f60\u60f3\u8ba9\u4e0a\u9762\u7684\u8ba2\u5355\u5217\u8868\u6309\u7167 UserId \u5206\u7ec4\u5f52\u7c7b\uff0c\u53d8\u6210\u4e00\u4e2a Key \u662f UserId \u503c\u662f\u7528\u6237\u6240\u6709\u8ba2\u5355\u7684\u5217\u8868\u7684 Map<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_9\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">map<span class=\"token punctuation\">[<\/span>int64<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">*<\/span><span class=\"token keyword\">Order<\/span>{\r\n    <span class=\"token number\">255<\/span>: <span class=\"token punctuation\">[<\/span>\r\n       <span class=\"token operator\">&amp;<\/span>{\r\n            ID: <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>\r\n           OrderNo: <span class=\"token string\">\"20240903628359373756980001\"<\/span>\r\n           UserId: <span class=\"token number\">255<\/span>\r\n           <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span>\r\n        }\r\n        <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span>\r\n    <span class=\"token punctuation\">]<\/span>\r\n    <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span> \r\n}<\/code><\/pre>\n<ul id=\"code_id_9\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<li>9.<\/li>\n<li>10.<\/li>\n<li>11.<\/li>\n<li>12.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 lo \u5e93\u7684GroupBy\u65b9\u6cd5\u6765\u5b9e\u73b0<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_10\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">userOrderMap <span class=\"token operator\">=<\/span> lo<span class=\"token punctuation\">.<\/span>GroupBy<span class=\"token punctuation\">(<\/span>orders<span class=\"token punctuation\">,<\/span> func<span class=\"token punctuation\">(<\/span>item <span class=\"token operator\">*<\/span><span class=\"token keyword\">Order<\/span><span class=\"token punctuation\">)<\/span> int64 {\r\n    <span class=\"token keyword\">return<\/span> item<span class=\"token punctuation\">.<\/span>UserId\r\n}<span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<ul id=\"code_id_10\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3>Reduce \u6c42\u52a0\u548c<\/h3>\n<p>\u6bd4\u5982\u6211\u4eec\u8981\u6c42\u6240\u6709\u8ba2\u5355\u91d1\u989d\u7684\u603b\u548c\uff0c\u53ef\u4ee5\u4f7f\u7528\u00a0lo.Reduce\u00a0\u51fd\u6570<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_11\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\"><span class=\"token comment\">\/\/ \u8ba1\u7b97\u603b\u4ef7<\/span>\r\ntotalPrice :<span class=\"token operator\">=<\/span> lo<span class=\"token punctuation\">.<\/span>Reduce<span class=\"token punctuation\">(<\/span>orders<span class=\"token punctuation\">,<\/span> func<span class=\"token punctuation\">(<\/span>agg <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">,<\/span> item <span class=\"token operator\">*<\/span><span class=\"token keyword\">Order<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">index<\/span> <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">int<\/span> {\r\n  <span class=\"token keyword\">return<\/span> agg <span class=\"token operator\">+<\/span> item<span class=\"token punctuation\">.<\/span>PayMoney\r\n}<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<ul id=\"code_id_11\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3>\u591a\u7ebf\u7a0bForeach<\/h3>\n<p>lo \u5305\u91cc\u9664\u4e86\u63d0\u4f9b\u4e86 Foreach \u529f\u80fd\u51fd\u6570\u6765\u904d\u5386\u96c6\u5408<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_12\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\">lo<span class=\"token punctuation\">.<\/span>ForEach<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span>string{<span class=\"token string\">\"hello\"<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">\"world\"<\/span>}<span class=\"token punctuation\">,<\/span> func<span class=\"token punctuation\">(<\/span>x string<span class=\"token punctuation\">,<\/span> _ <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span> {\r\n    println<span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">)<\/span>\r\n}<span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<ul id=\"code_id_12\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u9664\u6b64\u4e4b\u5916\u8fd8\u53ef\u4ee5\u7528\u591a\u4e2agoroutine \u6765\u8fdb\u884c\u904d\u5386\uff0c\u4e0d\u8fc7\u8981\u5b89\u88c5\u5b83\u7684\u4e00\u4e2a\u5b50\u5305\u3002<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_13\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\"><span class=\"token keyword\">import<\/span> lop <span class=\"token string\">\"github.com\/samber\/lo\/parallel\"<\/span>\r\n\r\nlop<span class=\"token punctuation\">.<\/span>ForEach<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span>string{<span class=\"token string\">\"hello\"<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">\"world\"<\/span>}<span class=\"token punctuation\">,<\/span> func<span class=\"token punctuation\">(<\/span>x string<span class=\"token punctuation\">,<\/span> _ <span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span> {\r\n    println<span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">)<\/span>\r\n}<span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<ul id=\"code_id_13\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u8fd9\u4e2a\u8bf4\u5b9e\u8bdd\u6211\u6ca1\u6709\u7528\u8fc7\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u8d85\u5927\u7684\u96c6\u5408\u8981\u904d\u5386\u53ef\u4ee5\u5c1d\u8bd5\u4e00\u4e0b\u3002<\/p>\n<h3>Map \u7684\u5e38\u7528\u64cd\u4f5c\u4e2d<\/h3>\n<p>lo \u5305\u4e2d\u4e5f\u6709\u5f88\u591a Map \u7c7b\u578b\u7684\u64cd\u4f5c\u529f\u80fd\uff0c\u50cf Keys\u3001UniqKeys\u3001Values\u3001UniqValues \u7b49\u7b49\uff0c\u5176\u5b9e\u5404\u79cd\u529f\u80fd\u7684\u540d\u5b57\u57fa\u672c\u4e0a\u8ddf\u5176\u4ed6\u8bed\u8a00\u63d0\u4f9b\u7684\u5e93\u51fd\u6570\u7684\u540d\u5b57\u7c7b\u4f3c\uff0c\u76f8\u4fe1\u901a\u8fc7\u6211\u4e0a\u9762\u7684\u6f14\u793a\u540e\u5927\u5bb6\u5b8c\u5168\u53ef\u4ee5\u81ea\u5df1\u63a2\u7d22\uff0c\u627e\u5230\u81ea\u5df1\u9700\u8981\u7684\u529f\u80fd\u4e86\u3002<\/p>\n<p>\u5173\u4e8e lo \u5e93\u66f4\u591a\u7684\u529f\u80fd\uff0c\u5927\u5bb6\u53c2\u8003\u5b83\u7684\u5b98\u65b9\u6587\u6863\u5427\uff1ahttps:\/\/github.com\/samber\/lo\u00a0\u63a5\u4e0b\u6765\u6211\u8bf4\u8bf4\u4f7f\u7528\u5b83\u7f16\u7801\u65f6\u7684\u4e00\u4e9b\u5efa\u8bae\u3002<\/p>\n<h2>\u4e1c\u897f\u867d\u597d\uff0c\u53ef\u522b\u8d2a\u676f<\/h2>\n<p>\u5173\u4e8e lo \u5e93\u7684\u4f7f\u7528\uff0c\u6211\u89c9\u5f97\u80fd\u7528\u4e00\u4e2a\u7b80\u5355\u5faa\u73af\u5b9e\u73b0\u7684\u903b\u8f91\u5c31\u4e0d\u7528\u5c0f\u9898\u5927\u505a\u5730\u6765\u4f7f\u7528 lo \u5e93\u91cc\u7684\u529f\u80fd\u4e86\uff0c\u5047\u5982\u50cf\u662f\u4e0a\u9762\u4e3e\u4f8b\u7684\u60c5\u51b5\u90a3\u6837\uff0c\u81ea\u5df1\u5199\u4ee3\u7801\u8981\u5faa\u73af\u52a0\u5224\u65ad\u518d\u52a0\u989d\u5916\u7684\u53d8\u91cf\u8d4b\u503c\u624d\u80fd\u641e\u5b9a\uff0c\u5efa\u8bae\u662f lo \u5e93\u91cc\u7684\u529f\u80fd\uff0c\u786e\u5b9e\u80fd\u8ba9\u5c11\u5199\u4ee3\u7801\uff0c\u800c\u4e14\u8ba9\u6574\u4e2a\u4ee3\u7801\u5757\u7684\u5d4c\u5957\u5c42\u6b21\u4e0d\u4f1a\u6df1\uff0c\u6574\u4f53\u770b\u4e0a\u53bb\u4f1a\u7b80\u6d01\u4e00\u4e9b\u3002<\/p>\n<p>\u53e6\u5916\u6211\u89c9\u5f97\u662f\uff0c\u8fd9\u4e9b\u529f\u80fd\u4e0d\u8981\u5d4c\u5957\u7740\u7528\uff0c\u672c\u6765\u5c31\u662f\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u98ce\u683c\uff0c\u518d\u5d4c\u5957\u7740\u7528\u5c31\u5f88\u96be\u770b\u61c2\u4e86\u3002<\/p>\n<p>\u4ee5\u524d\u6211\u5b66Java\u7684\u65f6\u5019\uff0c\u89c9\u5f97Java\u90a3\u4e2a Stream API\u771f\u7684\u5f88\u65b9\u4fbf\uff0c\u8fd8\u80fd\u94fe\u5f0f\u8c03\u7528\uff0c\u5199\u8d77\u6765\u5f88\u723d\u3002\u53ef\u662f\u8f6e\u5230\u81ea\u5df1\u7ef4\u62a4\u524d\u4eba\u5199\u7684\u9879\u76ee\u7684\u65f6\u5019\uff0c\u4e00\u6765\u5b9e\u9645\u7684\u4e1a\u52a1\u903b\u8f91\u672c\u6765\u5c31\u6bd4\u4e66\u4e0a\u7684\u4f8b\u5b50\u590d\u6742\uff0c\u8fd9\u4e9b\u4ee3\u7801\u903b\u8f91\u4e00\u987fStream API\u94fe\u5f0f\u8c03\u7528\uff0c\u518d\u52a0\u4e0a\u7528\u4e86 lambda\uff0c\u90a3\u4ee3\u7801\u770b\u8d77\u6765\u771f\u7684\u662f\u6bcf\u6b21\u90fd\u8981\u8bfb\u5f88\u4e45\u624d\u80fd\u660e\u767d\u662f\u5728\u5e72\u5565\u3002<\/p>\n<p>\u6bd4\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u4e0d\u770b\u6ce8\u91ca\u3001\u4e0d\u7ffb\u7ffbJava\u7684Stream \u548c Lambda \u8bed\u6cd5\u4f60\u80fd\u770b\u660e\u767d\u8fd9\u5757\u4ee3\u7801\u662f\u505a\u4ec0\u4e48\u7684\u5417\uff1f<\/p>\n<div>\n<div class=\"hljs-cto\">\n<div class=\"hljs-cto\"><button class=\"copy_btn disable\" data-clipboard-target=\"#code_id_14\">\u590d\u5236<\/button><\/p>\n<div class=\"code-toolbar\">\n<pre class=\"has-pre-numbering language-sql\" tabindex=\"0\"><code class=\"language-sql\"><span class=\"token comment\">\/\/ \u6c42\u4e24\u4e2aList, aList \u4e0e bList \u7684\u4ea4\u96c6 <\/span>\r\nList<span class=\"token operator\">&lt;<\/span>Person<span class=\"token operator\">&gt;<\/span> intersections <span class=\"token operator\">=<\/span> aList\r\n        <span class=\"token punctuation\">.<\/span>stream<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>filter<span class=\"token punctuation\">(<\/span>\r\n                a <span class=\"token operator\">-<\/span><span class=\"token operator\">&gt;<\/span>  bList<span class=\"token punctuation\">.<\/span>stream<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>map<span class=\"token punctuation\">(<\/span>Person::getId<span class=\"token punctuation\">)<\/span>\r\n                <span class=\"token punctuation\">.<\/span>anyMatch<span class=\"token punctuation\">(<\/span> \r\n                    id <span class=\"token operator\">-<\/span><span class=\"token operator\">&gt;<\/span> Objects<span class=\"token punctuation\">.<\/span>equals<span class=\"token punctuation\">(<\/span>a<span class=\"token punctuation\">.<\/span>getId<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> id<span class=\"token punctuation\">)<\/span>\r\n                <span class=\"token punctuation\">)<\/span>\r\n        <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>collect<span class=\"token punctuation\">(<\/span>Collectors<span class=\"token punctuation\">.<\/span>toList<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/code><\/pre>\n<ul id=\"code_id_14\" class=\"pre-numbering\">\n<li>1.<\/li>\n<li>2.<\/li>\n<li>3.<\/li>\n<li>4.<\/li>\n<li>5.<\/li>\n<li>6.<\/li>\n<li>7.<\/li>\n<li>8.<\/li>\n<\/ul>\n<div class=\"toolbar\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u4eca\u5929\u4ecb\u7ecd\u7684lo\u5e93\u5927\u5bb6\u53ef\u4ee5\u5c1d\u8bd5\u7528\u8d77\u6765\uff0c\u7b49\u7528\u4e60\u60ef\u4e86\u786e\u5b9e\u80fd\u8ba9\u81ea\u5df1\u7684\u4ee3\u7801\u5c11\u5199\u4e0d\u5c11\u5faa\u73af\u52a0\u5224\u65ad\u7684\u903b\u8f91\uff0c\u6574\u4f53\u98ce\u683c\u4f1a\u66f4\u6e05\u723d\uff0c\u81ea\u5df1\u4e5f\u80fd\u5c11\u5199\u4e00\u4e9b\u4ee3\u7801\u3002\u5982\u679c\u4f60\u4eec\u8fd8\u5728\u7528\u7684Go\u7248\u672c\u4e0d\u652f\u6301\u8303\u578b\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u7528\u4e00\u4e0b\u98ce\u683c\u7c7b\u4f3c\u7684\u5e93go-funk\u3002<\/p>\n<\/div>\n<\/div>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_21988\" class=\"pvc_stats total_only  \" data-element-id=\"21988\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u4ecb\u7ecd\u7684lo\u200b\u5e93\u5927\u5bb6\u53ef\u4ee5\u5c1d\u8bd5\u7528\u8d77\u6765\uff0c\u7b49\u7528\u4e60\u60ef\u4e86\u786e\u5b9e\u80fd\u8ba9\u81ea\u5df1\u7684\u4ee3\u7801\u5c11\u5199\u4e0d\u5c11\u5faa\u73af\u52a0\u5224\u65ad\u7684\u903b\u8f91\uff0c\u6574\u4f53\u98ce\u683c\u4f1a\u66f4\u6e05\u723d\uff0c [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_21988\" class=\"pvc_stats total_only  \" data-element-id=\"21988\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/aif.amtbbs.org\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":56,"featured_media":21990,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,80],"tags":[1287],"class_list":["post-21988","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-23","category-80","tag-lo--"],"_links":{"self":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/21988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/comments?post=21988"}],"version-history":[{"count":1,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/21988\/revisions"}],"predecessor-version":[{"id":21991,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/posts\/21988\/revisions\/21991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media\/21990"}],"wp:attachment":[{"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/media?parent=21988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/categories?post=21988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aif.amtbbs.org\/index.php\/wp-json\/wp\/v2\/tags?post=21988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}