操做符之變換操做符

     一、做用

  • 對事件序列中的事件 / 整個事件序列 進行加工處理(即變換),使得其轉變成不一樣的事件 / 整個事件序列

     二、類型

     三、詳解

          3.1   map()----- 見rxdocs.pdf第80頁緩存

       做用:對 被觀察者發送的每1個事件都經過 指定的函數 處理,從而變換成另一種事件app

 

    public static void map() {
        Observable.just("I", "am", "RxJava")
                .map(new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) throws Exception {
                        return s.length();
                    }
                }).subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "onSubscribe");
            }

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "onNext: value = " + value);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError: " + e.toString());
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete");
            }
        });
    }

          輸出:ide

08-06 14:55:52.715 18643 18643 D Operation: onSubscribe
08-06 14:55:52.716 18643 18643 D Operation: onNext: value = 1
08-06 14:55:52.716 18643 18643 D Operation: onNext: value = 2
08-06 14:55:52.716 18643 18643 D Operation: onNext: value = 6
08-06 14:55:52.716 18643 18643 D Operation: onComplete

 

          3.2   flatMap()----- 見rxdocs.pdf第72頁函數

       做用:將被觀察者發送的每一個事件序列進行 拆分 & 單獨轉換,再合併成一個新的事件序列,最後再進行發送spa

       原理:code

  • 爲事件序列中每一個事件都建立一個 Observable 對象;
  • 將對每一個 原始事件 轉換後的 新事件 都放入到對應 Observable對象;
  • 將新建的每一個Observable 都合併到一個 新建的、總的Observable 對象;
  • 新建的、總的Observable 對象 將 新合併的事件序列 發送給觀察者(Observer
  • 新合併生成的事件序列順序是無序的,即 與舊序列發送事件的順序無關

 

 

    public static void flatMap() {
        Observable.just("I", "am", "RxJava")
                .flatMap(new Function<String, ObservableSource<Integer>>() {
                    @Override
                    public ObservableSource<Integer> apply(String s) throws Exception {
                        int length = s.length();
                        ArrayList<Integer> num = new ArrayList<>();
                        for (int i = 0; i < length; i++) {
                            num.add(i);
                        }
                        return Observable.fromIterable(num);
                    }
                }).subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "onSubscribe");
            }

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "onNext: value = " + value);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError: " + e.toString());
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete");
            }
        });
    }

          輸出:server

08-06 15:19:50.714 19829 19829 D Operation: onSubscribe
08-06 15:19:50.714 19829 19829 D Operation: onNext: value = 0
08-06 15:19:50.714 19829 19829 D Operation: onNext: value = 0
08-06 15:19:50.714 19829 19829 D Operation: onNext: value = 1
08-06 15:19:50.714 19829 19829 D Operation: onNext: value = 0
08-06 15:19:50.714 19829 19829 D Operation: onNext: value = 1
08-06 15:19:50.714 19829 19829 D Operation: onNext: value = 2
08-06 15:19:50.714 19829 19829 D Operation: onNext: value = 3
08-06 15:19:50.715 19829 19829 D Operation: onNext: value = 4
08-06 15:19:50.715 19829 19829 D Operation: onNext: value = 5
08-06 15:19:50.715 19829 19829 D Operation: onComplete

 

          3.3   concatMap()----- 見rxdocs.pdf第75頁對象

       做用:相似flatMap()操做符,與flatMap()的 區別在於:新合併生成的事件序列順序是有序的,即 嚴格按照舊序列發送事件的順序blog

 

          3.4   buffer()----- 見rxdocs.pdf第63頁事件

       做用:按期從 被觀察者(Obervable)須要發送的事件中 獲取必定數量的事件 & 放到緩存區中,最終發送

 

    public static void buffer() {
        Observable.just(1, 3, 4, 6, 8)
                .buffer(3, 2)
                .subscribe(new Observer<List<Integer>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "onSubscribe");
                    }

                    @Override
                    public void onNext(List<Integer> values) {
                        Log.d(TAG, "onNext start");
                        for (Integer value : values) {
                            Log.d(TAG, "onNext: value = " + value);
                        }
                        Log.d(TAG, "onNext end");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "onError: " + e.toString());
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete");
                    }
                });
    }

          輸出:

08-06 15:49:52.400 21011 21011 D Operation: onSubscribe
08-06 15:49:52.400 21011 21011 D Operation: onNext start
08-06 15:49:52.400 21011 21011 D Operation: onNext: value = 1
08-06 15:49:52.400 21011 21011 D Operation: onNext: value = 3
08-06 15:49:52.400 21011 21011 D Operation: onNext: value = 4
08-06 15:49:52.400 21011 21011 D Operation: onNext end
08-06 15:49:52.400 21011 21011 D Operation: onNext start
08-06 15:49:52.400 21011 21011 D Operation: onNext: value = 4
08-06 15:49:52.400 21011 21011 D Operation: onNext: value = 6
08-06 15:49:52.401 21011 21011 D Operation: onNext: value = 8
08-06 15:49:52.401 21011 21011 D Operation: onNext end
08-06 15:49:52.401 21011 21011 D Operation: onNext start
08-06 15:49:52.401 21011 21011 D Operation: onNext: value = 8
08-06 15:49:52.401 21011 21011 D Operation: onNext end
08-06 15:49:52.401 21011 21011 D Operation: onComplete

 

     四、總結

相關文章
相關標籤/搜索