RxJava(二) map操作符用法详解

时间:2023-03-09 05:26:03
RxJava(二) map操作符用法详解

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

http://blog.****.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操作符的流程图:

RxJava(二) 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源码下载