{"id":200513,"date":"2020-11-15T21:42:48","date_gmt":"2020-11-15T13:42:48","guid":{"rendered":"http:\/\/4563.org\/?p=200513"},"modified":"2020-11-15T21:42:48","modified_gmt":"2020-11-15T13:42:48","slug":"java8-stream-%e6%b5%81%e4%b8%80%e8%be%b9-filter-%e8%bf%87%e6%bb%a4%e4%b8%80%e8%be%b9-map-%e6%98%a0%e5%b0%84%ef%bc%9f%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=200513","title":{"rendered":"Java8 Stream \u6d41\u4e00\u8fb9 filter \u8fc7\u6ee4\u4e00\u8fb9 map \u6620\u5c04\uff1f\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Java8 Stream \u6d41\u4e00\u8fb9 filter \u8fc7\u6ee4\u4e00\u8fb9 map \u6620\u5c04\uff1f\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : easterfan <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e00\u65f6 stream \u4e00\u65f6\u723d\uff0c\u7528\u4e60\u60ef\u4e86\u4ee5\u540e\uff0c\u7a81\u7136\u60f3\u5230\u5bf9\u4e8e\u6d41\uff0c\u80fd\u5426\u5b9e\u73b0\u8fc7\u6ee4\u548c map \u540c\u65f6\u8fdb\u884c\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u4e00\u4e2a\u573a\u666f\uff1a<\/p>\n<pre><code>\/\/ \u5982\u679c\u5143\u7d20\u662f\u5947\u6570\uff0c\u5c31 + 1\uff0c\u5982\u679c\u5143\u7d20\u662f\u5076\u6570\uff0c\u5c31 +2\uff0c\u671f\u671b\u4e00\u987f\u6d41\u5f0f\u64cd\u4f5c\u540e\uff0c\u8f93\u51fa\u4e00\u4e2a\u65b0\u7684 List\uff1a{2,4,4,6,6}  List&lt;Integer&gt; input = Arrays.asList(1, 2, 3, 4, 5);  inputList.stream()          .filter(p -&gt; xxx)          .map(p -&gt; xxx)          .collect(Collectors.toList()); <\/code><\/pre>\n<p>\u73b0\u5728\u6700\u9ad8\u9891\u7528\u5230\u7684 filter \u901a\u5e38\u53ea\u6709\u4e00\u4e2a\u6761\u4ef6\uff0c\u662f\u4ece\u539f\u6765\u7684 List \u4e2d\u8fc7\u6ee4\u51fa\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u7136\u540e\u5bf9\u8fd9\u4e9b\u5143\u7d20\u8fdb\u884c map \u64cd\u4f5c\uff0c<\/p>\n<p>\u5982\u679c\u5bf9\u4e00\u4e2a\u96c6\u5408\uff0c\u6709\u4e24\u79cd\u8fc7\u6ee4\u6761\u4ef6 A \u548c \u8fc7\u6ee4\u6761\u4ef6 B\uff0c\u6ee1\u8db3\u8fc7\u6ee4\u6761\u4ef6 A \u5c31\u5bf9\u5143\u7d20\u8fdb\u884c mapA \u64cd\u4f5c\uff0c\u6ee1\u8db3\u8fc7\u6ee4\u6761\u4ef6 B\uff0c\u5c31\u5bf9\u5143\u7d20\u8fdb\u884c mapB \u64cd\u4f5c\uff0c\u6700\u540e\u5143\u7d20\u90fd\u6309\u7167\u4e0d\u540c\u7684 map \u65b9\u5f0f\u6620\u5c04\u5b8c\u4e86\uff0c\u518d\u653e\u5230\u540c\u4e00\u4e2a\u96c6\u5408\u91cc~~<\/p>\n<p>\u521d\u7528 stream\uff0c\u6709\u5927\u4f6c\u8fd9\u6837\u64cd\u4f5c\u8fc7\u561b~~<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4282479\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chanchan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             peek \u4e86\u89e3\u4e00\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282480\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wowo243 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             partitioningBy()\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282481\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ye2moe <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u76f4\u63a5\u7684\u8bdd\u662f\u505a\u4e0d\u5230\u7684\uff0c\u53ef\u4ee5\u641e\u4e24\u4e2a\u6d41\u64cd\u4f5c\u518d\u5408\u8d77\u6765\u5c31\u597d\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282482\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : easterfan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wowo243 <br \/>@ye2moe <br \/>&#8220;`java<br \/> Map&lt;Boolean, List&lt;Integer&gt;&gt; partitionMap = Stream.of(1, 2, 3, 4, 5).collect(Collectors.partitioningBy(p -&gt; p \/ 2 == 0));<\/p>\n<p> Stream&lt;Integer&gt; evenStream = partitionMap.get(true).stream().map(p -&gt; p + 2);<br \/> Stream&lt;Integer&gt; oddStream = partitionMap.get(false).stream().map(p -&gt; p + 1);<br \/> Stream.concat(evenStream, oddStream);<br \/>&#8220;`<br \/>\u5206\u7ec4\u6210\u4e24\u4e2a\u6d41\uff0c\u7136\u540e\u518d\u5408\u5e76\uff0c\u5408\u5e76\u540e\u7684\u6d41\u548c\u539f\u6765\u7684\u6d41\u5143\u7d20\u987a\u5e8f\u4e0d\u4e00\u6837\u4e86~~                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282483\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : anzu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3a\u4ec0\u4e48\u8981 filter\uff0c\u5947\u5076\u4e0d\u662f\u4e8c\u9009\u4e00\u5417\u3002\u5982\u679c\u5224\u65ad\u6761\u4ef6\u590d\u6742\uff0c\u8981\u4e48 map \u91cc\u9762\u591a\u5199\u70b9\u4ee3\u7801\uff0c\u8981\u4e48\u7528 for<br \/>input.stream().map(p -&gt; p % 2 == 0 ? p + 2 : p + 1).collect(Collectors.toList())                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282484\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : easterfan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @anzu <br \/>\u611f\u8c22\u5927\u4f6c\uff01\u89e3\u51b3\u4e86\u6211\u7684\u95ee\u9898\uff0c\u7528 map \u524d\u603b\u662f\u5f88\u4e60\u60ef\u7684 filter \u4e00\u4e0b\uff0c\u6709\u4e86\u601d\u7ef4\u5b9a\u5f0f\uff0c\u7528\u4f60\u7684\u601d\u8def\uff0c\u628a\u5224\u65ad\u903b\u8f91\u5199\u5728 map \u91cc\uff0c\u5bf9\u4e00\u4e2a\u5143\u7d20 map \u524d\u5148\u505a\u4e00\u6b21\u5224\u65ad\uff0c\u7136\u540e\u6267\u884c\u5bf9\u5e94\u7684 map\uff0c\u5df2\u7ecf\u8fbe\u5230\u4e86\u6211\u5728\u627e\u7684\u4e00\u8fb9 filter\uff0c\u4e00\u8fb9 map \u7684\u60f3\u6cd5\uff0c\u611f\u8c22\uff01<\/p>\n<p>&#8220;`java<br \/>Stream.of(1,2,3,4,5)<br \/> .map(p -&gt; {if(isFilterA) mapA(); if (isFilterB) mapB(); if (isFilterC) mapC();})<br \/> .collect(Collectors.toList());<br \/>&#8220;`<br \/>\uff08\u539f\u6765\u7684\u56fa\u5b9a\u601d\u7ef4\u7684\u60f3\u6cd5~\uff1a\uff09<br \/>https:\/\/imgur.com\/7sn8euR                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282485\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jimmyismagic <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @easterfan filter \u6982\u5ff5\u8981\u641e\u6e05\u695a\u554a\uff0c\u6ca1\u4e8b\u5e72\u561b\u641e filter                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282486\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : optional <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             rx \u6b22\u8fce\u4f60                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282487\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : easterfan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jimmyismagic \u6bcd\u9e21\u554a~\u5199\u987a\u624b\u4e86\uff0cmap \u524d\u5fc5 filter~~Q                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282488\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : easterfan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @optional <br \/>\u6562\u95ee rx \u662f\uff1f\u8fd9 id \u5f88 6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282489\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jimmyismagic <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @easterfan rxJava,\u53e6\u5916\u8fd8\u6709 project reactor\uff0c\u90fd\u662f\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff0c\u4f46\u5176\u4e2d\u4e5f\u4f7f\u7528\u4e86 streams \u91cc\u7684\u4e00\u4e9b\u6982\u5ff5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282490\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : oneisall8955 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5728 map \u91cc\u9762\u76f4\u63a5\u5224\u65ad A \u6216 B \u6761\u4ef6\u5206\u5f00\u8f6c\u6362\u5c31\u884c\u4e86\u5427<\/p>\n<p>\u501f\uff0c\u9898\u5916\u8bdd\uff0c\u6700\u8fd1\u5bc6\u7684 stream \u7684\u53e6\u4e00\u4e2a API\uff0cCollectors.mapping\uff0c\u771f\u662f\u8282\u7701\u4e86\u4e0d\u5c11\u4ee3\u7801\u903b\u8f91\uff0c\u4e3b\u8981\u7528\u4e8e group \u5206\u7ec4\u540e\u7684 value \u96c6\u5408\u518d map<\/p>\n<p>https:\/\/stackoverflow.com\/questions\/52281724\/java-streams-group-list-entries-based-on-a-property-but-collect-a-property-of-ob<\/p>\n<p>\u77e5\u9053\u8fd9\u4e2a API \u7684\u5927\u4f6c\u8bf7\u5ffd\u7565                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4282491\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zch693922 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6309\u4e00\u7684 peek \u4e00\u4e0b\u5c31\u53ef\u4ee5\u4e86<br \/>list.stream().peek(item -&gt; {<br \/> if (xxxxx) { do something}<br \/> if (xxxx) {item.setXXX}<br \/>})                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Java8 Stream \u6d41\u4e00\u8fb9 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/200513"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=200513"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/200513\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200513"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}