{"id":10006256,"date":"2022-08-21T00:48:35","date_gmt":"2022-08-20T16:48:35","guid":{"rendered":"https:\/\/ityet.com\/?p=10006256"},"modified":"2022-08-21T00:48:35","modified_gmt":"2022-08-20T16:48:35","slug":"reactor-%e4%b9%8b-flatmap-vs-map-%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006256","title":{"rendered":"Reactor \u4e4b flatMap vs map \u8be6\u89e3"},"content":{"rendered":"<h2>1 \u4f5c\u7528\u4e0d\u540c<\/h2>\n<h3>1.2 \u6620\u5c04\uff1f\u5c55\u5e73\uff1f<\/h3>\n<ul>\n<li>map \u53ea\u6267\u884c\u6620\u5c04<\/li>\n<li>flatMap \u65e2\u6267\u884c\u6620\u5c04\uff0c\u4e5f\u6267\u884c\u5c55\u5e73<\/li>\n<\/ul>\n<p>\u4ec0\u4e48\u53eb\u53ea\u80fd\u6267\u884c\u6620\u5c04\uff1f<\/p>\n<p>\u6211\u7406\u89e3\u662f\u628a\u4e00\u4e2a\u6570\u636e\u6267\u884c\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8f6c\u6362\u6210\u53e6\u5916\u4e00\u4e2a\u6570\u636e\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1amapper \u51fd\u6570\u628a\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u5927\u5199\u3002map()\u65b9\u6cd5\u6267\u884c\u8fd9\u4e2a mapper \u51fd\u6570\u3002<\/p>\n<pre><code>Function&lt;String, String &gt; mapper = String::toUpperCase;\n        Flux&lt;String&gt; inFlux = Flux.just(&quot;hello&quot;, &quot;.&quot;, &quot;com&quot;);\n        Flux&lt;String&gt; outFlux = inFlux.map(mapper);\n        \/\/ reactor \u6d4b\u8bd5\u5305\u63d0\u4f9b\u7684\u6d4b\u8bd5\u65b9\u6cd5\n        StepVerifier.create(outFlux)\n                .expectNext(&quot;HELLO&quot;, &quot;.&quot;, &quot;COM&quot;)\n                .expectComplete()\n                .verify();<\/code><\/pre>\n<p>\u4ec0\u4e48\u53eb\u5c55\u5e73\uff1f<\/p>\n<p>mapper \u51fd\u6570\u628a\u5b57\u7b26\u4e32\u8f6c\u6210\u5927\u5199\uff0c\u7136\u540e\u5206\u5272\u6210\u4e00\u4e2a\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<pre><code>Function&lt;String, Publisher&lt;String&gt;&gt; mapper = s -&gt; Flux.just(s.toUpperCase().split(&quot;&quot;));\n\n        Flux&lt;String&gt; inFlux = Flux.just(&quot;hello&quot;, &quot;.&quot;, &quot;com&quot;);\n        \/\/ \u8fd9\u91cc\u53ea\u80fd\u4f7f\u7528 flatMap\uff0c\u56e0\u4e3a\u53c2\u6570\u662f Function&lt;T, Publisher&lt;V&gt;&gt; \u5f62\u5f0f\n        Flux&lt;String&gt; outFlux = inFlux.flatMap(mapper);\n\n        List&lt;String&gt; output = new ArrayList&lt;&gt;();\n        outFlux.subscribe(output::add);\n        \/\/ \u8f93\u51fa [H, E, L, L, O, ., C, O, M]\n    System.out.println(output);    <\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u6765\u81ea\u4e0d\u540c\u6765\u6e90\u7684\u6570\u636e\u9879\u4ea4\u9519\uff0c\u5b83\u4eec\u5728\u8f93\u51fa\u4e2d\u7684\u987a\u5e8f\u53ef\u80fd\u4e0e\u6211\u4eec\u5728\u8f93\u5165\u4e2d\u770b\u5230\u7684\u4e0d\u540c\u3002<\/p>\n<h3>1.2 \u540c\u6b65\uff1f\u5f02\u6b65\uff1f<\/h3>\n<ul>\n<li>map \u662f\u540c\u6b65\u7684\uff0c\u975e\u963b\u585e\u7684\uff0c1-1\uff081\u4e2a\u8f93\u5165\u5bf9\u5e941\u4e2a\u8f93\u51fa\uff09 \u5bf9\u8c61\u8f6c\u6362\u7684\uff1b<\/li>\n<li>flatMap \u662f\u5f02\u6b65\u7684\uff0c\u975e\u963b\u585e\u7684\uff0c1-N\uff081\u4e2a\u8f93\u5165\u5bf9\u5e94\u4efb\u610f\u4e2a\u8f93\u51fa\uff09 \u5bf9\u8c61\u8f6c\u6362\u7684\uff1b<\/li>\n<\/ul>\n<p>\u5f53\u6d41\u88ab\u8ba2\u9605\uff08subscribe\uff09\u4e4b\u540e\uff0c\u6620\u5c04\u5668\u5bf9\u8f93\u5165\u6d41\u4e2d\u7684\u5143\u7d20\u6267\u884c\u5fc5\u8981\u7684\u8f6c\u6362\uff08\u6267\u884c\u4e0a\u8ff0 mapper \u64cd\u4f5c\uff09\u3002\u8fd9\u4e9b\u5143\u7d20\u4e2d\u7684\u6bcf\u4e00\u4e2a\u90fd\u53ef\u4ee5\u8f6c\u6362\u4e3a\u591a\u4e2a\u6570\u636e\u9879\uff0c\u7136\u540e\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u6d41\u3002<\/p>\n<p>\u4e00\u65e6\u4e00\u4e2a\u7531 Publisher \u5b9e\u4f8b\u8868\u793a\u7684\u65b0\u6d41\u51c6\u5907\u5c31\u7eea\uff0cflatMap \u5c31\u4f1a\u6025\u5207\u5730\u8ba2\u9605\u3002operator \u4e0d\u4f1a\u7b49\u5f85\u53d1\u5e03\u8005\u5b8c\u6210\uff0c\u4f1a\u7ee7\u7eed\u4e0b\u4e00\u4e2a\u6d41\u7684\u5904\u7406\uff0c\u8fd9\u610f\u5473\u7740\u8ba2\u9605\u662f\u975e\u963b\u585e\u7684\u3002\u540c\u65f6\u4e5f\u8bf4\u660e flatMap() \u662f\u5f02\u6b65\u7684\u3002<\/p>\n<p>\u7531\u4e8e\u7ba1\u9053<strong>\u540c\u65f6\u5904\u7406\u6240\u6709\u6d3e\u751f\u6d41<\/strong>\uff0c\u56e0\u6b64\u5b83\u4eec\u7684\u6570\u636e\u9879\u53ef\u80fd\u968f\u65f6\u8fdb\u5165\u3002\u7ed3\u679c\u5c31\u662f\u539f\u6709\u7684\u987a\u5e8f\u4e22\u5931\u3002<strong>\u5982\u679c\u9879\u76ee\u7684\u987a\u5e8f\u5f88\u91cd\u8981\uff0c\u8bf7\u8003\u8651\u6539\u7528 flatMapSequential \u8fd0\u7b97\u7b26\u3002<\/strong><\/p>\n<h2>2 \u65b9\u6cd5\u7b7e\u540d\u7684\u533a\u522b\u5f88\u660e\u663e<\/h2>\n<h3>2.1 \u65b9\u6cd5\u7b7e\u540d<\/h3>\n<ul>\n<li>map \u53c2\u6570\u662f <code>Function&lt;T, U&gt;<\/code> \uff0c\u8fd4\u56de\u662f <code>Flux&lt;U&gt;<\/code><\/li>\n<li>flatMap \u53c2\u6570\u662f <code>Function&lt;T, Publisher&lt;V&gt;&gt;<\/code> \u8fd4\u56de\u662f <code>Flux&lt;V&gt;<\/code><\/li>\n<\/ul>\n<p>\u4e3e\u4f8b\uff1a<\/p>\n<p>\u8fd9\u91cc\u53ea\u80fd\u4f7f\u7528 flatMap\uff0c\u56e0\u4e3a\u53c2\u6570\u662f <code>Function&lt;T, Publisher&lt;V&gt;&gt;<\/code> \u5f62\u5f0f<\/p>\n<pre><code>Function&lt;String, Publisher&lt;String&gt;&gt; mapper = s -&gt; Flux.just(s.toUpperCase().split(&quot;&quot;));\n\nFlux&lt;String&gt; inFlux = Flux.just(&quot;hello&quot;, &quot;.&quot;, &quot;com&quot;);\n\/\/ \u8fd9\u91cc\u53ea\u80fd\u4f7f\u7528 flatMap\uff0c\u56e0\u4e3a\u53c2\u6570\u662f Function&lt;T, Publisher&lt;V&gt;&gt; \u5f62\u5f0f\nFlux&lt;String&gt; outFlux = inFlux.flatMap(mapper);<\/code><\/pre>\n<p>\u8fd9\u91cc\u53ea\u80fd\u4f7f\u7528 map\uff0c\u56e0\u4e3a\u53c2\u6570\u662f <code>Function&lt;String, String &gt;<\/code><\/p>\n<pre><code>Function&lt;String, String &gt; mapper = String::toUpperCase;\nFlux&lt;String&gt; inFlux = Flux.just(&quot;hello&quot;, &quot;.&quot;, &quot;com&quot;);\n\/\/ \u8fd9\u91cc\u53ea\u80fd\u4f7f\u7528 map\uff0c\u56e0\u4e3a\u53c2\u6570\u662f Function&lt;String, String &gt;\nFlux&lt;String&gt; outFlux = inFlux.map(mapper);<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u770b\u65b9\u6cd5\u7b7e\u540d\uff0c\u53ef\u4ee5\u770b\u51fa\uff0c\u53ef\u4ee5\u7ed9 map() \u4f20\u53c2 <code>Function&lt;T, Publisher&lt;V&gt;&gt;<\/code>\uff0c\u6309\u7167\u65b9\u6cd5\u7b7e\u540d\uff0c\u5b83\u4f1a\u8fd4\u56de<code>Flux&lt;Publisher&lt;V&gt;&gt;<\/code>\uff0c\u4f46\u5b83\u4e0d\u77e5\u9053\u5982\u4f55\u5904\u7406 Publishers\u3002\u6bd4\u5982\u4e0b\u9762\u7684\u4ee3\u7801\uff1a\u7f16\u8bd1\u4e0d\u4f1a\u62a5\u9519\uff0c\u4f46\u662f\u4e0d\u77e5\u9053\u540e\u7eed\u600e\u4e48\u5904\u7406\u3002<\/p>\n<pre><code>Function&lt;String, Publisher&lt;String&gt;&gt; mapper = s -&gt; Flux.just(s.toUpperCase().split(&quot;&quot;));\nFlux&lt;String&gt; inFlux = Flux.just(&quot;hello&quot;, &quot;.&quot;, &quot;com&quot;);\nFlux&lt;Publisher&lt;String&gt;&gt; map = inFlux.map(mapper);<\/code><\/pre>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u6765\u6e90\u4e8e stackoverflow\uff1a<\/p>\n<p>\u4f7f\u7528 map \u65b9\u6cd5\u4f1a\u4ea7\u751f <code>Mono&lt;Mono&lt;T&gt;&gt;<\/code>\uff0c\u800c\u4f7f\u7528 flatMap \u4f1a\u4ea7\u751f <code>Mono&lt;T&gt;<\/code>\u3002\u4f7f\u7528 map() \u5c31\u662f\u7ed9 map \u4f20\u53c2\u4e86<code>Function&lt;T, Publisher&lt;V&gt;&gt;<\/code>\uff0c\u5b83\u8fd4\u56de\u7684\u4e5f\u662f <code>Mono&lt;Publisher&lt;V&gt;&gt;<\/code>\u3002<\/p>\n<pre><code>\/\/ Signature of the HttpClient.get method\nMono&lt;JsonObject&gt; get(String url);\n\n\/\/ The two urls to call\nString firstUserUrl = &quot;my-api\/first-user&quot;;\nString userDetailsUrl = &quot;my-api\/users\/details\/&quot;; \/\/ needs the id at the end\n\n\/\/ Example with map\nMono&lt;Mono&lt;JsonObject&gt;&gt; result = HttpClient.get(firstUserUrl).\n  map(user -&gt; HttpClient.get(userDetailsUrl + user.getId()));\n\/\/ This results with a Mono&lt;Mono&lt;...&gt;&gt; because HttpClient.get(...)\n\/\/ returns a Mono\n\n\/\/ Same example with flatMap\nMono&lt;JsonObject&gt; bestResult = HttpClient.get(firstUserUrl).\n  flatMap(user -&gt; HttpClient.get(userDetailsUrl + user.getId()));\n\/\/ Now the result has the type we expected<\/code><\/pre>\n<h3>2.3 \u8fd4\u56de<\/h3>\n<ul>\n<li>map() \u8fd4\u56de\u4e00\u4e2a\u503c\u7684\u6d41<\/li>\n<li>flatMap() \u8fd4\u56de\u4e00\u4e2a\u6d41\u503c\u7684\u6d41<\/li>\n<\/ul>\n<pre><code>Flux&lt;String&gt; stringFlux = Flux.just(&quot;hello word!&quot;);\nFunction&lt;String, Publisher&lt;String&gt;&gt; mapper = s -&gt; Flux.just(s.toUpperCase().split(&quot;&quot;));\n\/\/ \u4f7f\u7528 flatMap() \u8fd4\u56de\u7684\u662f FluxFlatMap.\nFlux&lt;String&gt; flatMapFlux = stringFlux.flatMap(mapper);\n\/\/ \u4f7f\u7528 map() \u8fd4\u56de\u7684\u662f FluxMapFuseable\nFlux&lt;String&gt; mapFlux = stringFlux.map(s -&gt; s);<\/code><\/pre>\n<p>flatMapFlux \u7c7b\u578b\u662f FluxFlatMap\uff1b\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f7f\u7528 flatMap() \u8fd4\u56de\u7684\u662f FluxFlatMap.<\/p>\n<p>mapFlux \u7c7b\u578b\u662f FluxMapFuseable\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f7f\u7528 map() \u8fd4\u56de\u7684\u662f FluxMapFuseable<\/p>\n<p>FluxMapFuseable \u662f\u4ec0\u4e48\uff1f<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042348212\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>FluxFlatMap \u662f\u4ec0\u4e48?<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042348213\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>FluxFlatMap \u548c FluxMapFuseable \u662f\u4ec0\u4e48\u533a\u522b\uff1f<\/p>\n<p>\u5404\u4f4d\u770b\u5b98\u53ef\u4ee5\u4e00\u8d77\u8ba8\u8bba\uff01<\/p>\n<p>\u53c2\u8003\u94fe\u63a5\uff1a<\/p>\n<p>baeldung: <a href=\"https:\/\/link.segmentfault.com\/?enc=GOOnRAnQtk4Zw7z04ebyZA%3D%3D.w3yPKoY9xaRVaLuM%2F7LtOlZU7REbnEYoGWhuer1GnKk7DDisRKY9I4myFe4%2BY%2Fl0750g2LBWgdhQzbGyesG10Q%3D%3D\" rel=\"nofollow\">Project Reactor: map() vs flatMap()<\/a><\/p>\n<p>csdn: <a href=\"https:\/\/link.segmentfault.com\/?enc=HShZLJ2CKSAlZOmtRz1W2w%3D%3D.NNfD8OK7R3YdmFJqS2gOCWRqSAEjdbiYhaI9kC9z0PBMkQEOz5NoOCRXXkfUoISPRkBH5nJQTSUXnJgqbPVI0w%3D%3D\" rel=\"nofollow\">map VS flatmap<\/a><\/p>\n<p>geeksforgeeks: <a href=\"https:\/\/link.segmentfault.com\/?enc=DB6%2BXqTLjsvLHmlzis2CRQ%3D%3D.cRG3yWZ615IJq1fObbMvDN2zllQ6BRNNJHlbnaStYRU%2Bzb5WJGAkRVCuWGw0yxHgcmsdgbP3deVgaNIKby1drPVOwUhSdUzEY5gKmrsLinvzdKQSvBCAQErzYH34pusB\" rel=\"nofollow\">Difference Between map() And flatMap() In Java Stream<\/a><\/p>\n<p>stackOverFlow: <a href=\"https:\/\/link.segmentfault.com\/?enc=lKkEzlXBiK58WKm8oUsjyQ%3D%3D.n0V%2FNxMc%2FABZQX%2B%2FVR4%2Blt7%2FBnv%2BuQ2libg2lBHFcUzqPWkcS6Zaj5kdGmY%2FEahoiiQ802i6tWxcVI2CG2HfCFq5Swad8xnY7y3EYiXSJMk%3D\" rel=\"nofollow\">map vs flatMap in reactor<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042348210\">https:\/\/segmentfault.com\/a\/1190000042348210<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 \u4f5c\u7528\u4e0d\u540c 1.2 \u6620\u5c04\uff1f\u5c55\u5e73\uff1f map \u53ea\u6267\u884c\u6620\u5c04 flatMap \u65e2\u6267\u884c\u6620\u5c04\uff0c\u4e5f\u6267\u884c\u5c55\u5e73 \u4ec0\u4e48\u53eb\u53ea\u80fd\u6267\u884c [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006256","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006256"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006256\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}