1. 程式人生 > >RxJava(二) map操作符用法詳解

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

RxJava系列文章目錄導讀:

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'