{"id":35973,"date":"2024-11-26T09:23:40","date_gmt":"2024-11-26T01:23:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35973\/"},"modified":"2024-11-26T09:23:40","modified_gmt":"2024-11-26T01:23:40","slug":"%e4%b8%a4%e7%a7%8d-collect-%e6%93%8d%e4%bd%9c%e8%8e%b7%e5%8f%96%e6%9c%80%e5%a4%a7-id-%e8%8f%9c%e5%8d%95%e9%a1%b9%e7%9a%84%e5%8c%ba%e5%88%ab%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35973\/","title":{"rendered":"\u4e24\u79cd Collect \u64cd\u4f5c\u83b7\u53d6\u6700\u5927 id \u83dc\u5355\u9879\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173085847914840.jpg\" class=\"aligncenter\" title=\"\u4e24\u79cd Collect \u64cd\u4f5c\u83b7\u53d6\u6700\u5927 id \u83dc\u5355\u9879\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" alt=\"\u4e24\u79cd Collect \u64cd\u4f5c\u83b7\u53d6\u6700\u5927 id \u83dc\u5355\u9879\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4e24\u79cd collect \u64cd\u4f5c\u5199\u6cd5\u7684<\/strong><\/p>\n<p>\u5bf9\u4e8e\u7ed9\u5b9a\u7684 menulist\uff0c\u6211\u4eec\u60f3\u8981\u83b7\u53d6\u5176\u4e2d id\u6700\u5927\u7684\u83dc\u5355\u9879\u3002\u4e0b\u9762\u63d0\u4f9b\u4e86\u4e24\u79cd\u4f7f\u7528 collectors \u8fdb\u884c\u6b64\u64cd\u4f5c\u7684\u5199\u6cd5\uff1a<\/p>\n<p><strong>\u7b2c\u4e00\u79cd\u5199\u6cd5\uff1a<\/strong><\/p>\n<pre>menulist.stream().collect(collectors.maxby(comparator.comparing(menu::getid))).get();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7b2c\u4e8c\u79cd\u5199\u6cd5\uff1a<\/strong><\/p>\n<pre>menulist.stream().collect(collectors.collectingandthen(collectors.maxby(comparator.comparing(menu::getid)), optional::get));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u533a\u522b\uff1a<\/strong><\/p>\n<p>\u8fd9\u4e24\u79cd\u5199\u6cd5\u5728\u529f\u80fd\u4e0a\u7b49\u540c\uff0c\u90fd\u80fd\u83b7\u5f97 id\u6700\u5927\u7684\u83dc\u5355\u9879\u3002\u4f46\u662f\uff0c\u5b83\u4eec\u5728\u5b9e\u73b0\u7ec6\u8282\u4e0a\u6709\u4ee5\u4e0b\u533a\u522b\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u6d41\u5904\u7406\u6d41\u7a0b\uff1a<\/strong><\/p>\n<ul>\n<li>\u7b2c\u4e00\u5199\u6cd5\u4f7f\u7528 collectors.maxby \u5bf9\u6d41\u8fdb\u884c\u6392\u5e8f\u5e76\u83b7\u53d6\u6700\u5927\u503c\uff0c\u7136\u540e\u4f7f\u7528 .get() \u65b9\u6cd5\u89e3\u5305 optional\u3002<\/li>\n<li>\u7b2c\u4e8c\u5199\u6cd5\u5148\u4f7f\u7528 collectors.maxby \u627e\u5230\u6700\u5927\u503c\uff0c\u7136\u540e\u4f7f\u7528 collectingandthen \u5bf9\u7ed3\u679c\u5e94\u7528 optional::get \u65b9\u6cd5\u89e3\u5305\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8bbe\u8ba1\u521d\u8877\uff1a<\/strong><\/p>\n<ul>\n<li>collectingandthen \u901a\u5e38\u7528\u4e8e\u5bf9\u6d41\u5904\u7406\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 collectingandthen(tolist(), list -&gt; {&#8230;}) \u5c06\u6d41\u6536\u96c6\u4e3a\u5217\u8868\u5e76\u6267\u884c\u989d\u5916\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u7b2c\u4e00\u5199\u6cd5\u4f7f\u7528 collect \u8fdb\u884c\u5f52\u7ea6\u64cd\u4f5c\uff0c\u8fd9\u8131\u79bb\u4e86 collect \u7684\u8bbe\u8ba1\u521d\u8877\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff0c<strong>\u5efa\u8bae\u60a8\u4f7f\u7528\u4ee5\u4e0b\u66f4\u7b80\u6d01\u9ad8\u6548\u7684\u5199\u6cd5\uff1a<\/strong><\/p>\n<pre>menuList.stream().max(Comparator.comparing(Menu::getId)).get();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e24\u79cd Collect \u64cd\u4f5c\u83b7\u53d6\u6700\u5927 id \u83dc\u5355\u9879\u7684\u662f\u4ec0\u4e48\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e24\u79cd collect \u64cd\u4f5c\u5199\u6cd5\u7684 \u5bf9\u4e8e\u7ed9\u5b9a\u7684 menulist\uff0c\u6211\u4eec\u60f3\u8981\u83b7\u53d6\u5176\u4e2d id\u6700\u5927\u7684\u83dc\u5355\u9879\u3002\u4e0b\u9762\u63d0\u4f9b\u4e86\u4e24\u79cd\u4f7f\u7528 collectors \u8fdb\u884c\u6b64\u64cd\u4f5c\u7684\u5199\u6cd5\uff1a \u7b2c\u4e00\u79cd\u5199\u6cd5\uff1a menulist.stream().collect(collectors.maxby(comparator.comparing(menu::getid))).get(); \u767b\u5f55\u540e\u590d\u5236 \u7b2c\u4e8c\u79cd\u5199\u6cd5\uff1a menulist.stream().collect(collectors.collectingandthen(collectors.maxby(comparator.comparing(menu::getid)), optional::get)); \u767b\u5f55\u540e\u590d\u5236 \u533a\u522b\uff1a \u8fd9\u4e24\u79cd\u5199\u6cd5\u5728\u529f\u80fd\u4e0a\u7b49\u540c\uff0c\u90fd\u80fd\u83b7\u5f97 id\u6700\u5927\u7684\u83dc\u5355\u9879\u3002\u4f46\u662f\uff0c\u5b83\u4eec\u5728\u5b9e\u73b0\u7ec6\u8282\u4e0a\u6709\u4ee5\u4e0b\u533a\u522b\uff1a \u6d41\u5904\u7406\u6d41\u7a0b\uff1a \u7b2c\u4e00\u5199\u6cd5\u4f7f\u7528 collectors.maxby \u5bf9\u6d41\u8fdb\u884c\u6392\u5e8f\u5e76\u83b7\u53d6\u6700\u5927\u503c\uff0c\u7136\u540e\u4f7f\u7528 .get() \u65b9\u6cd5\u89e3\u5305 optional\u3002 \u7b2c\u4e8c\u5199\u6cd5\u5148\u4f7f\u7528 collectors.maxby \u627e\u5230\u6700\u5927\u503c\uff0c\u7136\u540e\u4f7f\u7528 collectingandthen \u5bf9\u7ed3\u679c\u5e94\u7528 optional::get \u65b9\u6cd5\u89e3\u5305\u3002 \u8bbe\u8ba1\u521d\u8877\uff1a collectingandthen \u901a\u5e38\u7528\u4e8e\u5bf9\u6d41\u5904\u7406\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 collectingandthen(tolist(), list -&gt; {&#8230;}) \u5c06\u6d41\u6536\u96c6\u4e3a\u5217\u8868\u5e76\u6267\u884c\u989d\u5916\u7684\u64cd\u4f5c\u3002 \u7b2c\u4e00\u5199\u6cd5\u4f7f\u7528 collect \u8fdb\u884c\u5f52\u7ea6\u64cd\u4f5c\uff0c\u8fd9\u8131\u79bb\u4e86 collect \u7684\u8bbe\u8ba1\u521d\u8877\u3002 \u56e0\u6b64\uff0c\u5efa\u8bae\u60a8\u4f7f\u7528\u4ee5\u4e0b\u66f4\u7b80\u6d01\u9ad8\u6548\u7684\u5199\u6cd5\uff1a menuList.stream().max(Comparator.comparing(Menu::getId)).get(); \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662f\u4e24\u79cd Collect \u64cd\u4f5c\u83b7\u53d6\u6700\u5927 id \u83dc\u5355\u9879\u7684\u662f\u4ec0\u4e48\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-35973","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=35973"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35973\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}