{"id":294,"date":"2018-11-12T18:44:46","date_gmt":"2018-11-12T18:44:46","guid":{"rendered":"https:\/\/nurlandroid.com\/?p=294"},"modified":"2021-04-01T07:58:51","modified_gmt":"2021-04-01T07:58:51","slug":"%d0%b2%d0%bd%d0%b8%d0%ba%d0%b0%d0%b5%d0%bc-%d0%b2-map-flatmap-switchmap-concatmap","status":"publish","type":"post","link":"https:\/\/nurlandroid.com\/?p=294","title":{"rendered":"\u0412\u043d\u0438\u043a\u0430\u0435\u043c \u0432 Map, FlatMap, SwitchMap, ConcatMap"},"content":{"rendered":"\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0443\u0437\u043d\u0430\u0435\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e \u0442\u0430\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u0445 \u043a\u0430\u043a\u00a0Map, FlatMap, SwitchMap, ConcatMap.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>1. Map()<\/strong><\/p>\n\n\n\n<p>\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <strong>Map()<\/strong> \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0438\u0437\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u0439 <strong>Observable<\/strong>-\u043e\u043c, \u0438 \u044d\u043c\u0438\u0442\u0442\u0438\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442.\u00a0\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0438\u0437 \u0442\u0430\u043a\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 <strong>Map()<\/strong>, \u043a\u0430\u043a\u043e\u0435 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u043c \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438\u0434\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u043c \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u0443\u044e \u0438 \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0444\u043e\u0440\u043c\u0443 \u0434\u043b\u044f <strong>Observer<\/strong>-a.<\/p>\n\n\n\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Observable.just(\"Hello, world!\")\n    .map(s -> s + \" -Dan\")\n    .subscribe(s -> System.out.println(s));<\/code><\/pre>\n\n\n\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, world! -Dan<\/code><\/pre>\n\n\n\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 <strong>Map()<\/strong>, \u0433\u0434\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043e\u0444\u0444\u043b\u0430\u0439\u043d\u043e\u0432\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u044d\u043c\u0438\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0430\u0448\u0435\u043c\u0443 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044e. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <strong>Map()<\/strong> \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u044d\u043c\u0438\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n\n\n\n<p><strong>2. FlatMap()<\/strong><\/p>\n\n\n\n<p><strong>FlatMap()<\/strong> \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0434\u0430\u043d\u043d\u044b\u0435, \u0438\u0437\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u0435 \u043e\u0434\u043d\u0438\u043c <strong>Observable<\/strong> , \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u0438\u0437\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u0435 \u0434\u0440\u0443\u0433\u0438\u043c <strong>Observable<\/strong>, \u043f\u043e\u0434\u043c\u0435\u043d\u044f\u044f \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043e\u0434\u0438\u043d <strong>Observable<\/strong> \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u0439.<\/p>\n\n\n\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;String> urls = new ArrayList&lt;String>();\nurls.add(\"url1\");\nurls.add(\"url2\");\nurls.add(\"url3\");\n\nObservable.just(urls)\n  .flatMap(new Func1&lt;List&lt;String>, Observable&lt;String>>() {\n          @Override\n          public Observable&lt;String> call(List&lt;String> urls) {\n              return Observable.from(urls);\n          }\n      })\n      .subscribe(url -> System.out.println(url));<\/code><\/pre>\n\n\n\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0442\u043e \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <strong>Observable&lt;String><\/strong>, \u0445\u043e\u0442\u044f \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f <strong>Observable.from(urls)<\/strong>. \u0412\u044b\u0445\u043e\u0434\u0438\u0442, \u0447\u0442\u043e \u044d\u043c\u0438\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u0437 <strong>urls<\/strong>\u00a0\u043c\u044b \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c\u00a0 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 <strong>Observable <\/strong>\u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0437 \u043d\u0438\u0445.<\/p>\n\n\n\n<p><strong>FlatMap()<\/strong> \u043c\u043e\u0436\u0435\u0442 \u0447\u0435\u0440\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0438 \u044d\u043c\u0438\u0441\u0441\u0438\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f. \u041f\u043e\u043b\u0435\u0437\u0435\u043d \u0435\u0441\u043b\u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043d\u0435 \u0432\u0430\u0436\u0435\u043d, \u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0435\u0434\u0438\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>3. ConcatMap()<\/strong><\/p>\n\n\n\n<p>\u0422\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0432\u044b\u0434\u0430\u0435\u0442 \u043f\u043e\u0445\u043e\u0436\u0438\u0439 \u0441 <strong>FlatMap()<\/strong> \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u044d\u043c\u0438\u0442\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. <strong>ConcatMap()<\/strong> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u044d\u043c\u0438\u0441\u0441\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e <strong>Observable.<\/strong>\u00a0\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 <strong>ConcatMap()<\/strong> \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0447\u0435\u043c\u00a0<strong>FlatMap().<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>4.<\/strong>\u00a0<strong>SwithMap()<\/strong><\/p>\n\n\n\n<p><strong>SwithMap()<\/strong>\u00a0\u043a\u0430\u0440\u0434\u0438\u043d\u0430\u043b\u044c\u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 <strong>FlatMap()<\/strong> \u0438 <strong>ConcatMap()<\/strong>. \u041e\u043d\u00a0\u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442, \u0435\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u0440\u043e\u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439.\u00a0<strong>SwitchMap<\/strong> \u043e\u0442\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 <strong>Observable<\/strong> \u0432\u0441\u044f\u043a\u0438\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043d\u043e\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0438\u0437\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0432\u0441\u0435\u0433\u0434\u0430 \u044d\u043c\u0438\u0442\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e <strong>Observable<\/strong>.<\/p>\n\n\n\n<p>\u0427\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440:\u00a0\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0432\u044b \u043f\u0438\u0448\u0435\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0447\u0442\u043e-\u0442\u043e \u0432\u0432\u043e\u0434\u0438\u0442 \u0432 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u043d\u0430\u0431\u0438\u0440\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432. \u041f\u0440\u0438 \u043d\u0430\u0431\u043e\u0440\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439 \u043d\u0430 \u043e\u0434\u0438\u043d \u0441\u0438\u043c\u0432\u043e\u043b. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f <strong>SwitchMap()<\/strong> \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043d\u0430\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0443\u0437\u043d\u0430\u0435\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e \u0442\u0430\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u0445 \u043a\u0430\u043a\u00a0Map, FlatMap, SwitchMap, ConcatMap.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-294","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=294"}],"version-history":[{"count":3,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":300,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions\/300"}],"wp:attachment":[{"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nurlandroid.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}