欢迎转载,转载请标明出处:

http://blog.csdn.net/johnny901114/article/details/51531348

本文出自:【余志强的博客】

1 map操作符的作用

Returns an Observable that applies a specified function to each item emitted by the source Observable and emits the results of these function applications.

下面是官方对于map操作符的流程图:

对Observable发射的数据都应用一个函数,然后再发射最后的结果集。最后map()方法返回一个新的Observable。

2 用法示例

1 先来个简单的例子

假设我们从服务器获取了一个字符串集合,我们想里面的数据项都转成大写,然后把集合的顺序反转。如把[“this”,”is”,”rxJava”]转成[“RXJAVA”,”IS”,“THIS”]。

Observable.from(new String[]{"This", "is", "RxJava"})
                .map(new Func1<String, String>() {
                    @Override
                    public String call(String s) {
                        printLog(tvLogs, "Transform Data toUpperCase: ", s);
                        return s.toUpperCase();
                    }
                })
                //转成List
                .toList()
                .map(new Func1<List<String>, List<String>>() {
                    @Override
                    public List<String> call(List<String> strings) {
                        printLog(tvLogs, "Transform Data Reverse List: ", strings.toString());
                        Collections.reverse(strings);
                        return strings;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Action1<List<String>>() {
                    @Override
                    public void call(List<String> s) {
                        printLog(tvLogs, "Consume Data ", s.toString());
                    }
                });

输出结果:

Transform Data toUpperCase: 'This' , Main Thread:false, Thread Name:RxCachedThreadScheduler-1
Transform Data toUpperCase: 'is' , Main Thread:false, Thread Name:RxCachedThreadScheduler-1
Transform Data toUpperCase: 'RxJava' , Main Thread:false, Thread Name:RxCachedThreadScheduler-1
Transform Data Reverse List: '[THIS, IS, RXJAVA]' , Main Thread:false, Thread Name:RxCachedThreadScheduler-1
Consume Data '[RXJAVA, IS, THIS]' , Main Thread:true, Thread Name:main

来个复杂点的示例

假设我们有一个主机列表,想根据这个主机列表好获取它们的IP地址。

首先得有一个方法,根据主机获取IP:

    private String getIPByUrl(String str) throws MalformedURLException, UnknownHostException {
        URL urls = new URL(str);
        String host = urls.getHost();
        String address = InetAddress.getByName(host).toString();
        int b = address.indexOf("/");
        return address.substring(b + 1);

    }

然后通过map操作符来实现:

private Observable<String> processUrlsIpByMap() {
        return Observable.just(
                "http://www.baidu.com/",
                "http://www.google.com/",
                "https://www.bing.com/")
                .map(new Func1<String, String>() {
                    @Override
                    public String call(String s) {
                        try {
                            return s + " : " + getIPByUrl(s);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (UnknownHostException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        printLog(tvLogs, "Consume Data: ", s);
                    }
                });
    }

输出结果:

Consume Data: 'http://www.baidu.com/ : 115.239.210.27'
Consume Data: 'http://www.google.com/ : 216.58.203.36'
Consume Data: 'https://www.bing.com/ : 202.89.233.104'

github源码下载

RxJava(二) map操作符用法详解的更多相关文章

  1. js数组中foEach和map的用法详解 jq中的$.each和$.map

    数组中foEach和map的用法详解 相同点: 1.都是循环遍历数组(仅仅是数组)中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项value, ...

  2. STL map 常见用法详解

    <算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 / ...

  3. RxJava(三) flatMap操作符用法详解

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51532776 本文出自:[余志强的博客] flatMap操作符的作用 ...

  4. RxJava(四) concatMap操作符用法详解

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51533282 本文出自:[余志强的博客] concatMap操作符的 ...

  5. C语言 · C++中map的用法详解

    转载自:http://blog.csdn.net/sunquana/article/details/12576729 一.定义   (1) map<string,   int>   Map ...

  6. [转]Java中Map的用法详解

    转载地址:http://www.zhixing123.cn/jsp/30113.html Map简介 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.此接口取代 Dictio ...

  7. Java中Map的用法详解

    Map简介 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.此接口取代 Dictionary 类,后者完全是一个抽象类,而不是一个接口. Map 接口提供三种collecti ...

  8. Dll学习(二)__declspec用法详解

    __declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和 C++语言的ANSI规范,而__declspec是一种 ...

  9. map函数用法详解

    map函数是Python内置的高阶函数,它是一个典型的函数式编程例子.它的参数为: 一个函数function.一个或多个sequence.通过把函数function依次作用在sequence的每个元素 ...

随机推荐

  1. [LeetCode] Optimal Division 最优分隔

    Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...

  2. PKUWC2018划水记

    PKUWC2018划水记 Day -1 ​ 从福州出发去长沙,原本是预定Day0当天的航班,后来怕来不及提前到了今天. ​ 由于最近长沙下雪,所以听说飞机取消了很多班次,所以早上起来的时候还特地看了一 ...

  3. AtCoder Grand Contest 021 D - Reversed LCS

    Description Takahashi has decided to give a string to his mother. The value of a string T is the len ...

  4. ●HDU 3507 Print Article

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=3507 题解: 斜率优化DP 一个入门题,就不给题解了,网上的好讲解很多的.   这里就只提一个小问题吧( ...

  5. linux内核中的链表

    1.内核中的链表 linux内核链表与众不同,他不是把将数据结构塞入链表,而是将链表节点塞入数据,在2.1内核中引入了官方链表,从此内核中所有的链表使用都采用此链表,千万不要在重复造车轮子了!链表实现 ...

  6. poj2331 (IDA*)

    题意:给你k种管道,然后是每种的长度,每种的数量,求(x1,y1)到(x2,y2)所用管道的最少数量 思路: 最开始考虑的是直接bfs,但是没有成功. 然后发现可以先找x轴x1 到 x2 ,再找y轴y ...

  7. poj 2065 高斯消元(取模的方程组)

    SETI Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1735   Accepted: 1085 Description ...

  8. [Spoj]Counting Divisors (cube)

    来自FallDream的博客,未经允许,请勿转载,谢谢. 设d(x)表示x的约数个数,求$\sum_{i=1}^{n}d(i^{3})$ There are 5 Input files. - Inpu ...

  9. Simpson积分(BZOJ2178)

    lrj的代码常数太大T了QAQ,改了一下. #include <cstdio> #include <cmath> #include <algorithm> usin ...

  10. C++是跨平台的语言

    最开始学习Java时,老师就说Java是跨平台的,而c++不是,这里要纠正一下观点,c++也是跨平台的,只不过是实现跨平台的方式不同而已. 1.平台 一般我们把CPU处理器与操作系统的整体叫平台.不同 ...