{"id":35985,"date":"2024-11-26T10:30:40","date_gmt":"2024-11-26T02:30:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35985\/"},"modified":"2024-11-26T10:30:40","modified_gmt":"2024-11-26T02:30:40","slug":"%e4%b8%a4%e7%a7%8d%e5%86%99%e6%b3%95%e8%8e%b7%e5%8f%96%e6%9c%80%e5%a4%a7-id-%e8%8f%9c%e5%8d%95%ef%bc%8c%e5%93%aa%e7%a7%8d%e6%9b%b4%e5%90%88%e7%90%86%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35985\/","title":{"rendered":"\u4e24\u79cd\u5199\u6cd5\u83b7\u53d6\u6700\u5927 id \u83dc\u5355\uff0c\u54ea\u79cd\u66f4\u5408\u7406\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173086293088587.jpg\" class=\"aligncenter\" title=\"\u4e24\u79cd\u5199\u6cd5\u83b7\u53d6\u6700\u5927 id \u83dc\u5355\uff0c\u54ea\u79cd\u66f4\u5408\u7406\uff1f\u63d2\u56fe\" alt=\"\u4e24\u79cd\u5199\u6cd5\u83b7\u53d6\u6700\u5927 id \u83dc\u5355\uff0c\u54ea\u79cd\u66f4\u5408\u7406\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4e24\u79cd\u5199\u6cd5\u7684\u5dee\u5f02<\/strong><\/p>\n<p>\u5982\u9898\u6240\u793a\uff0c\u201c\u4e0b\u9762\u4e24\u79cd\u5199\u6cd5\u6709\u5565\u201d\uff0c\u6587\u4e2d\u63d0\u5230\u4e86\u4ee5\u4e0b\u4e24\u79cd\u5199\u6cd5\uff1a<\/p>\n<pre>menulist.stream().collect(collectors.maxby(comparator.comparing(menu::getid))).get();\n\nmenulist.stream().collect(collectors.collectingandthen(collectors.maxby(comparator.comparing(menu::getid)), optional::get));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8be6\u89e3<\/strong><\/p>\n<p><strong>\u529f\u80fd\u7b49\u540c<\/strong><\/p>\n<p>\u8fd9\u4e24\u79cd\u5199\u6cd5\u672c\u8d28\u4e0a\u662f\u7b49\u540c\u7684\uff0c\u90fd\u4f1a\u8fd4\u56de\u6307\u5b9a\u5217\u8868\u4e2d\u5177\u6709\u6700\u5927 id \u503c\u7684\u83dc\u5355\u3002<\/p>\n<p><strong>\u8bbe\u8ba1\u521d\u8877<\/strong><\/p>\n<p>\u4f46\u662f\uff0c\u5b83\u4eec\u5728\u8bbe\u8ba1\u521d\u8877\u4e0a\u5b58\u5728\u5dee\u5f02\uff1a<\/p>\n<ul>\n<li> <strong>\u7b2c\u4e00\u79cd\u5199\u6cd5<\/strong>\u76f4\u63a5\u4f7f\u7528 collect \u64cd\u4f5c\u6536\u96c6\u6d41\uff0c\u5e76\u5c06 optional \u503c\u89e3\u5305\u3002<\/li>\n<li> <strong>\u7b2c\u4e8c\u79cd\u5199\u6cd5<\/strong>\u4f7f\u7528 collectingandthen \u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u5728\u6d41\u6536\u96c6\u5b8c\u6210\u540e\u5bf9\u7ed3\u679c\u6267\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u3002\u7136\u800c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0ccollectingandthen \u4e2d\u7684\u64cd\u4f5c\u4ec5\u4ec5\u662f\u89e3\u5305 optional \u503c\uff0c\u8fd9\u4e0e\u7b2c\u4e00\u79cd\u5199\u6cd5\u7684\u76ee\u7684\u76f8\u540c\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff0c\u7b2c\u4e8c\u79cd\u5199\u6cd5\u8131\u79bb\u4e86 collectingandthen \u7684\u8bbe\u8ba1\u521d\u8877\uff0c\u5b83\u672c\u5e94\u7528\u4e8e\u5728\u6d41\u6536\u96c6\u540e\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u5982\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u5c06\u5176\u5206\u7ec4\u3002<\/p>\n<p><strong>\u63a8\u8350\u5199\u6cd5<\/strong><\/p>\n<p>\u8003\u8651\u5230\u8bbe\u8ba1\u89c4\u7ea6\u548c\u56e2\u961f\u534f\u4f5c\u7684\u7b80\u6d01\u6027\uff0c\u5efa\u8bae\u4f7f\u7528\u4e0b\u5217\u5199\u6cd5\uff1a<\/p>\n<pre>stream.sorted().findFirst()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u5199\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u76f4\u63a5\u4f7f\u7528 sorted \u65b9\u6cd5\u5bf9\u6d41\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528 findfirst \u65b9\u6cd5\u83b7\u53d6\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u4ece\u800c\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u6536\u96c6\u548c\u89e3\u5305\u64cd\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e24\u79cd\u5199\u6cd5\u83b7\u53d6\u6700\u5927 id \u83dc\u5355\uff0c\u54ea\u79cd\u66f4\u5408\u7406\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\u5199\u6cd5\u7684\u5dee\u5f02 \u5982\u9898\u6240\u793a\uff0c\u201c\u4e0b\u9762\u4e24\u79cd\u5199\u6cd5\u6709\u5565\u201d\uff0c\u6587\u4e2d\u63d0\u5230\u4e86\u4ee5\u4e0b\u4e24\u79cd\u5199\u6cd5\uff1a menulist.stream().collect(collectors.maxby(comparator.comparing(menu::getid))).get(); menulist.stream().collect(collectors.collectingandthen(collectors.maxby(comparator.comparing(menu::getid)), optional::get)); \u767b\u5f55\u540e\u590d\u5236 \u8be6\u89e3 \u529f\u80fd\u7b49\u540c \u8fd9\u4e24\u79cd\u5199\u6cd5\u672c\u8d28\u4e0a\u662f\u7b49\u540c\u7684\uff0c\u90fd\u4f1a\u8fd4\u56de\u6307\u5b9a\u5217\u8868\u4e2d\u5177\u6709\u6700\u5927 id \u503c\u7684\u83dc\u5355\u3002 \u8bbe\u8ba1\u521d\u8877 \u4f46\u662f\uff0c\u5b83\u4eec\u5728\u8bbe\u8ba1\u521d\u8877\u4e0a\u5b58\u5728\u5dee\u5f02\uff1a \u7b2c\u4e00\u79cd\u5199\u6cd5\u76f4\u63a5\u4f7f\u7528 collect \u64cd\u4f5c\u6536\u96c6\u6d41\uff0c\u5e76\u5c06 optional \u503c\u89e3\u5305\u3002 \u7b2c\u4e8c\u79cd\u5199\u6cd5\u4f7f\u7528 collectingandthen \u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u5728\u6d41\u6536\u96c6\u5b8c\u6210\u540e\u5bf9\u7ed3\u679c\u6267\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u3002\u7136\u800c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0ccollectingandthen \u4e2d\u7684\u64cd\u4f5c\u4ec5\u4ec5\u662f\u89e3\u5305 optional \u503c\uff0c\u8fd9\u4e0e\u7b2c\u4e00\u79cd\u5199\u6cd5\u7684\u76ee\u7684\u76f8\u540c\u3002 \u56e0\u6b64\uff0c\u7b2c\u4e8c\u79cd\u5199\u6cd5\u8131\u79bb\u4e86 collectingandthen \u7684\u8bbe\u8ba1\u521d\u8877\uff0c\u5b83\u672c\u5e94\u7528\u4e8e\u5728\u6d41\u6536\u96c6\u540e\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u5982\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u5c06\u5176\u5206\u7ec4\u3002 \u63a8\u8350\u5199\u6cd5 \u8003\u8651\u5230\u8bbe\u8ba1\u89c4\u7ea6\u548c\u56e2\u961f\u534f\u4f5c\u7684\u7b80\u6d01\u6027\uff0c\u5efa\u8bae\u4f7f\u7528\u4e0b\u5217\u5199\u6cd5\uff1a stream.sorted().findFirst() \u767b\u5f55\u540e\u590d\u5236 \u6b64\u5199\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u76f4\u63a5\u4f7f\u7528 sorted \u65b9\u6cd5\u5bf9\u6d41\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528 findfirst \u65b9\u6cd5\u83b7\u53d6\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u4ece\u800c\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u6536\u96c6\u548c\u89e3\u5305\u64cd\u4f5c\u3002 \u4ee5\u4e0a\u5c31\u662f\u4e24\u79cd\u5199\u6cd5\u83b7\u53d6\u6700\u5927 id \u83dc\u5355\uff0c\u54ea\u79cd\u66f4\u5408\u7406\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-35985","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35985","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=35985"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35985\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}