{"id":393,"date":"2023-02-25T08:47:57","date_gmt":"2023-02-25T00:47:57","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=393"},"modified":"2023-04-30T14:37:37","modified_gmt":"2023-04-30T06:37:37","slug":"rxjava2-learning-5-map-operator","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/rxjava2-learning-5-map-operator\/","title":{"rendered":"RxJava2\u5b66\u4e60\u4e4b\u4e94\uff1aMap\u64cd\u4f5c\u7b26"},"content":{"rendered":"<p>Map \u64cd\u4f5c\u7b26\u662f\u5bf9\u5e8f\u5217\u7684\u6bcf\u4e00\u9879\u90fd\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u53d8\u6362Observable\u53d1\u5c04\u7684\u6570\u636e\u5e8f\u5217\u3002<\/p>\n<p>Map \u64cd\u4f5c\u7b26\u5bf9\u539f\u59cbObservable\u53d1\u5c04\u7684\u6bcf\u4e00\u9879\u6570\u636e\u5e94\u7528\u4e00\u4e2a\u4f60\u9009\u62e9\u7684\u51fd\u6570\uff0c\u6267\u884c\u53d8\u6362\u64cd\u4f5c\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u53d1\u5c04\u8fd9\u4e9b\u7ed3\u679c\u7684Observable\u3002<\/p>\n<p><!-- more --><\/p>\n<h3>Integer\u8f6c\u6362\u4e3aString<\/h3>\n<pre><code class=\"language-java\">\/\/Integer\u8f6c\u6362\u4e3aString\nObservable&lt;Integer&gt; observable = Observable.just(1);\n\nObservable&lt;String&gt; map = observable.map(new Function&lt;Integer, String&gt;() {\n    @Override\n    public String apply(Integer integer) throws Exception {\n        return integer + &quot;: appblog.cn&quot;;\n    }\n});\n\n\/\/\u8ba2\u9605\u53c2\u6570\uff1aInteger\nobservable.subscribe(new Consumer&lt;Integer&gt;() {\n    @Override\n    public void accept(Integer integer) throws Exception {\n        Log.i(TAG, &quot;accept: &quot; + integer);\n    }\n});\n\n\/\/\u8ba2\u9605\u53c2\u6570\uff1aString\nmap.subscribe(new Consumer&lt;String&gt;() {\n    @Override\n    public void accept(String s) throws Exception {\n        Log.i(TAG, &quot;accept: &quot; + s);\n    }\n});<\/code><\/pre>\n<h3>Integer\u8f6c\u6362\u4e3aUser\u5bf9\u8c61<\/h3>\n<pre><code class=\"language-java\">\/\/Integer\u8f6c\u6362\u4e3aUser\nObservable.just(1).map(new Function&lt;Integer, User&gt;() {\n    @Override\n    public User apply(Integer integer) throws Exception {\n        Retrofit retrofit = new Retrofit.Builder()\n                .baseUrl(SERVER_ADDRESS)\n                .addConverterFactory(GsonConverterFactory.create())\n                .build();\n\n        api = retrofit.create(Api.class);\n        User user = api.getUserInfoWithPath(1).execute().body();\n        return user;\n    }\n}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer&lt;User&gt;() {\n    @Override\n    public void accept(User user) throws Exception {\n        Toast.makeText(MainActivity.this, user.getUsername(), Toast.LENGTH_SHORT).show();\n    }\n});<\/code><\/pre>\n<h3>Integer\u8f6c\u6362\u4e3aObservable<\/h3>\n<pre><code class=\"language-java\">\/\/Integer\u8f6c\u6362\u4e3aObservable\nObservable.just(1).map(new Function&lt;Integer, Observable&lt;User&gt;&gt;() {\n    @Override\n    public Observable&lt;User&gt; apply(Integer integer) throws Exception {\n\n        return null;\n    }\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Map \u64cd\u4f5c\u7b26\u662f\u5bf9\u5e8f\u5217\u7684\u6bcf\u4e00\u9879\u90fd\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u53d8\u6362Observable\u53d1\u5c04\u7684\u6570\u636e\u5e8f\u5217\u3002 Map \u64cd\u4f5c\u7b26\u5bf9\u539f\u59cbO [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[131],"tags":[132],"class_list":["post-393","post","type-post","status-publish","format-standard","hentry","category-rxjava","tag-rxjava"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=393"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/393\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}