{"id":203388,"date":"2025-05-22T08:20:41","date_gmt":"2025-05-22T00:20:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203388\/"},"modified":"2025-05-22T08:20:41","modified_gmt":"2025-05-22T00:20:41","slug":"stream-%e6%94%b6%e9%9b%86%e6%93%8d%e4%bd%9c%ef%bc%9a%e4%b8%a4%e7%a7%8d%e5%86%99%e6%b3%95%e5%af%b9%e6%af%94%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:\/\/server.hk\/cnblog\/203388\/","title":{"rendered":"Stream \u6536\u96c6\u64cd\u4f5c\uff1a\u4e24\u79cd\u5199\u6cd5\u5bf9\u6bd4\uff0c\u54ea\u79cd\u66f4\u5408\u7406\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Stream \u6536\u96c6\u64cd\u4f5c\uff1a\u4e24\u79cd\u5199\u6cd5\u5bf9\u6bd4\uff0c\u54ea\u79cd\u66f4\u5408\u7406\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aStream \u6536\u96c6\u64cd\u4f5c\uff1a\u4e24\u79cd\u5199\u6cd5\u5bf9\u6bd4\uff0c\u54ea\u79cd\u66f4\u5408\u7406\uff1f\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241107\/1730948279672c2cb757d95.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4e24\u79cd\u6536\u96c6\u64cd\u4f5c\u5199\u6cd5\u5bf9\u6bd4<\/strong><\/p>\n<p>original post: <\/p>\n<p>\u5728 stream \u64cd\u4f5c\u4e2d\uff0c\u6709\u4e24\u79cd\u6bd4\u8f83\u5e38\u89c1\u7684\u6536\u96c6\u64cd\u4f5c\u5199\u6cd5\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u5199\u6cd5\u4e00<\/strong>\uff1a<\/p>\n<pre>menulist.stream().collect(collectors.maxby(comparator.comparing(menu::getid))).get();<\/pre>\n<\/li>\n<li>\n<p><strong>\u5199\u6cd5\u4e8c<\/strong>\uff1a<\/p>\n<pre>menuList.stream().collect(Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparing(Menu::getId)), Optional::get));<\/pre>\n<\/li>\n<\/ul>\n<p>\u8fd9\u4e24\u79cd\u5199\u6cd5\u6709\u4f55\u533a\u522b\u548c\u4f18\u52a3\u5462\uff1f<\/p>\n<p><strong>\u5206\u6790<\/strong><\/p>\n<p><strong>1\u3001\u529f\u80fd\u7b49\u540c<\/strong><\/p>\n<p>\u4e24\u79cd\u5199\u6cd5\u5728\u529f\u80fd\u4e0a\u662f\u7b49\u540c\u7684\uff0c\u90fd\u7528\u4e8e\u4ece menulist \u4e2d\u6536\u96c6\u5177\u6709\u6700\u5927 id \u7684 menu \u5bf9\u8c61\u3002<\/p>\n<p><strong>2\u3001\u8bbe\u8ba1\u521d\u8877<\/strong><\/p>\n<ul>\n<li><strong>\u5199\u6cd5\u4e00<\/strong>\uff1acollect \u64cd\u4f5c\u539f\u672c\u7684\u8bbe\u8ba1\u76ee\u7684\u662f\u5bf9\u6d41\u8fdb\u884c\u6536\u96c6\u64cd\u4f5c\uff0c\u5982\u7edf\u8ba1\u3001\u5206\u7ec4\u7b49\u3002\u5c06 reduce \u64cd\u4f5c\u653e\u5728 collect \u4e2d\u662f\u4e0d\u5408\u7406\u7684\u3002<\/li>\n<li><strong>\u5199\u6cd5\u4e8c<\/strong>\uff1acollectingandthen \u64cd\u4f5c\u539f\u672c\u7684\u8bbe\u8ba1\u76ee\u7684\u662f\u5bf9\u6d41\u7684\u5904\u7406\u7ed3\u679c\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\uff0c\u5982\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a list\u3001map \u7b49\u3002<\/li>\n<\/ul>\n<p><strong>3\u3001\u63a8\u8350\u5199\u6cd5<\/strong><\/p>\n<p>\u4ece\u8bbe\u8ba1\u521d\u8877\u548c\u4ee3\u7801\u7b80\u6d01\u6027\u7684\u89d2\u5ea6\u8003\u8651\uff0c\u66f4\u63a8\u8350\u4f7f\u7528 <strong>stream().sorted().findfirst()<\/strong> \u7684\u5199\u6cd5\u3002\u8fd9\u4e2a\u5199\u6cd5\u66f4\u52a0\u76f4\u89c2\u9ad8\u6548\uff0c\u7b26\u5408 stream \u64cd\u4f5c\u7684\u8bed\u4e49\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u867d\u7136\u4e24\u79cd\u5199\u6cd5\u5728\u529f\u80fd\u4e0a\u7b49\u540c\uff0c\u4f46\u4ece\u8bbe\u8ba1\u89c4\u7ea6\u548c\u56e2\u961f\u534f\u4f5c\u7684\u89d2\u5ea6\u8003\u8651\uff0c\u5efa\u8bae\u4e0d\u4f7f\u7528\u4e0a\u8ff0\u4e24\u79cd\u5199\u6cd5\uff0c\u4f18\u5148\u9009\u62e9\u66f4\u5408\u7406\u3001\u7b80\u6d01\u7684\u5199\u6cd5\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aStream \u6536\u96c6\u64cd\u4f5c\uff1a\u4e24\u79cd\u5199\u6cd5\u5bf9\u6bd4\uff0c\u54ea\u79cd\u66f4\u5408\u7406\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stream \u6536\u96c6\u64cd\u4f5c\uff1a\u4e24\u79cd\u5199\u6cd5&#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-203388","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203388","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=203388"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203388\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}