Rxjava2 Observable的數(shù)據(jù)過濾詳解及實(shí)例(二)

接續(xù)上篇: Rxjava2 Observable的數(shù)據(jù)過濾詳解及實(shí)例(一)

6. Filter

只發(fā)射通過了函數(shù)過濾的數(shù)據(jù)項(xiàng)。

img-filter

實(shí)例代碼:

    // filter(Predicate<? super Integer> predicate)
    // 驗(yàn)證數(shù)據(jù),決定是否發(fā)射數(shù)據(jù)
    Observable.range(1, 10)
            .filter(new Predicate<Integer>() {

                @Override
                public boolean test(Integer t) throws Exception {
                    // 進(jìn)行測(cè)試驗(yàn)證是否需要發(fā)射數(shù)據(jù)
                    return t > 5 ? true : false;
                }
            }).subscribe(new Consumer<Integer>() {

                @Override
                public void accept(Integer t) throws Exception {
                    System.out.println("--> accept filter: " + t);
                }
            });

輸出:

--> accept filter: 6
--> accept filter: 7
--> accept filter: 8
--> accept filter: 9
--> accept filter: 10

Javadoc: filter(predicate)

7. Frist

只發(fā)射第一項(xiàng)或者滿足某個(gè)條件的第一項(xiàng)數(shù)據(jù)。如果你只對(duì)Observable發(fā)射的第一項(xiàng)數(shù)據(jù),或者滿足某個(gè)條件的第一項(xiàng)數(shù)據(jù)感興趣,你可以使用 First 操作符。

Frist 操作符有以下幾種操作:

7.1 firstElement()

只發(fā)射第一個(gè)數(shù)據(jù),當(dāng)數(shù)據(jù)存在的情況。

img-firstElement()

實(shí)例代碼:

    // 1. firstElement()
    // 只發(fā)射第一個(gè)數(shù)據(jù)
    Observable.range(1, 10)
        .firstElement()
        .subscribe(new Consumer<Integer>() {

            @Override
            public void accept(Integer t) throws Exception {
                System.out.println("--> accept firstElement(1): "  + t);
            }
        });

輸出:

--> accept firstElement(1): 1

Javadoc: firstElement()

7.2 first(defaultItem)

first(defaultItem)firstElement() 類似,但是在Observagle沒有發(fā)射任何數(shù)據(jù)時(shí)發(fā)射一個(gè)你在參數(shù)中指定的 defaultItem 默認(rèn)值。

img-first(defaultItem)

實(shí)例代碼:

    // 2. first(Integer defaultItem)
    // 發(fā)射第一個(gè)數(shù)據(jù)項(xiàng),如果沒有數(shù)據(jù)項(xiàng),發(fā)送默認(rèn)的defaultItem
    Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
            emitter.onComplete();
        }
    }).first(999) // 沒有數(shù)據(jù)發(fā)送時(shí),發(fā)送默認(rèn)值999
      .subscribe(new Consumer<Integer>() {

          @Override
          public void accept(Integer t) throws Exception {
              System.out.println("--> accept first(2): " + t);
          }
      });

輸出:

--> accept first(2): 999

Javadoc: first(defaultItem)

7.3 firstOrError()

發(fā)射第一個(gè)數(shù)據(jù)項(xiàng),如果沒有數(shù)據(jù)項(xiàng),會(huì)發(fā)送 NoSuchElementException 通知。

img-firstOrError

實(shí)例代碼:

    // 3. first(Integer defaultItem)
    // 發(fā)射第一個(gè)數(shù)據(jù)項(xiàng),如果沒有數(shù)據(jù)項(xiàng),會(huì)有Error: NoSuchElementException
    Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
            emitter.onComplete();
        }
    }).firstOrError() // 沒有數(shù)據(jù)發(fā)送時(shí),將會(huì)發(fā)送NoSuchElementException通知
      .subscribe(new SingleObserver<Integer>() {

            @Override
            public void onSubscribe(Disposable d) {
                System.out.println("--> onSubscribe: ");
            }

            @Override
            public void onSuccess(Integer t) {
                System.out.println("--> accept onSuccess(3): " + t);
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("--> acctpt onError(3): " + e);
            }
      });

輸出:

--> onSubscribe: 
--> acctpt onError(3): java.util.NoSuchElementException

Javadoc: firstOrError()

8. Single

singlefirst 類似,但是如果原始Observable在完成之前不是正好發(fā)射一次數(shù)據(jù),它會(huì)拋出一個(gè)NoSuchElementException 的通知。

Single 有以下幾種操作:

8.1 singleElement()

發(fā)射單例數(shù)據(jù),超過一個(gè)就會(huì)發(fā)送 NoSuchElementException 通知。

img-singleElement

實(shí)例代碼:

    // 1.singleElement()
    // 發(fā)射單例數(shù)據(jù),超過一個(gè)就會(huì)NoSuchElementException  
    Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
            emitter.onNext(1);
            emitter.onNext(2);
            emitter.onComplete();
        }
    }).singleElement() // 發(fā)送單個(gè)數(shù)據(jù),大于1項(xiàng)數(shù)據(jù)就會(huì)有Error通知
      .subscribe(new Consumer<Integer>() {

            @Override
            public void accept(Integer t) throws Exception {
                System.out.println("--> accept singleElement(1): " + t);
            }
      },new Consumer<Throwable>() {

        @Override
        public void accept(Throwable t) throws Exception {
            System.out.println("--> OnError(1): " + t);
        }
    });

輸出:

--> OnError(1): java.lang.IllegalArgumentException: Sequence contains more than one element!

Javadoc: singleElement()

8.2 single(defaultItem)

發(fā)射單例數(shù)據(jù),沒有接收到數(shù)據(jù)項(xiàng)則發(fā)送指定默認(rèn) defaultItem 數(shù)據(jù)。

img-single(defaultItem)

實(shí)例代碼:

    // 2. single(Integer defaultItem)
    // 發(fā)射單例數(shù)據(jù),沒有數(shù)據(jù)項(xiàng)發(fā)送指定默認(rèn)defaultItem
    Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
            emitter.onComplete();
        }
    }).single(999) // 沒有接受到數(shù)據(jù)則發(fā)送默認(rèn)數(shù)據(jù)999
      .subscribe(new Consumer<Integer>() {

            @Override
            public void accept(Integer t) throws Exception {
                System.out.println("--> accept single(2): " + t);
            }
      });

輸出:

--> accept single(2): 999

Javadoc: single(defaultItem)

8.3 singleOrError()

發(fā)射一個(gè)單例的數(shù)據(jù),如果數(shù)據(jù)源沒有數(shù)據(jù)項(xiàng),則發(fā)射一個(gè) NoSuchElementException 通知。

img-singleOrError

實(shí)例代碼:

    // 3.singleOrError()
    // 發(fā)射一個(gè)單例的數(shù)據(jù),如果數(shù)據(jù)源 沒有數(shù)據(jù)項(xiàng),則發(fā)送一個(gè)NoSuchElementException異常通知
    Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
            emitter.onComplete();
        }
    }).singleOrError() // 如果沒有數(shù)據(jù)項(xiàng)發(fā)送,則發(fā)送一個(gè)NoSuchElementException異常通知
      .subscribe(new SingleObserver<Integer>() {

            @Override
            public void onSubscribe(Disposable d) {
                System.out.println("--> onSubscribe(3): ");
            }

            @Override
            public void onSuccess(Integer t) {
                System.out.println("--> onSuccess(3): " + t);
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("--> onError(3): " + e);
            }
        });

輸出:

--> onSubscribe(3): 
--> onError(3): java.util.NoSuchElementException

Javadoc: singleOrError()

9. ElementAt

ElementAt 操作符獲取原始Observable發(fā)射的數(shù)據(jù)序列指定索引位置的數(shù)據(jù)項(xiàng),然后當(dāng)做自己的唯一數(shù)據(jù)發(fā)射。

ElementAt 操作符有以下幾種操作:

9.1 elementAt(index)

發(fā)射索引位置第 index 項(xiàng)數(shù)據(jù)(從0開始計(jì)數(shù)),如果數(shù)據(jù)不存在,會(huì) IndexOutOfBoundsException 異常。

img-elementAt(index)

實(shí)例代碼:

    // 1. elementAt(long index)
    // 指定發(fā)射第N項(xiàng)數(shù)據(jù)(從0開始計(jì)數(shù)),如果數(shù)據(jù)不存在,會(huì)IndexOutOfBoundsException異常
    Observable.range(1, 10)
        .elementAt(5) // 發(fā)射數(shù)據(jù)序列中索引為5的數(shù)據(jù)項(xiàng),索引從0開始
        .subscribe(new Consumer<Integer>() {

            @Override
            public void accept(Integer t) throws Exception {
                System.out.println("--> accept ElementAt(1): " + t);
            }
        });

輸出:

--> accept ElementAt(1): 6

Javadoc: elementAt(index)

9.2 elementAt(index, defaultItem)

發(fā)射索引位置第 index 項(xiàng)數(shù)據(jù)(從0開始計(jì)數(shù)),如果數(shù)據(jù)不存在,發(fā)送默認(rèn) defaultItem 數(shù)據(jù)。

img-elementAt(index, defaultItem)

實(shí)例代碼:

    // 2. elementAt(long index, Integer defaultItem)
    // 指定發(fā)射第N項(xiàng)數(shù)據(jù)(從0開始計(jì)數(shù)),如果數(shù)據(jù)不存在,發(fā)送默認(rèn)defaultItem
    Observable.range(1, 10)
        .elementAt(20, 0) // 發(fā)射索引第20項(xiàng)數(shù)據(jù),不存在此項(xiàng)數(shù)據(jù)時(shí),發(fā)送默認(rèn)數(shù)據(jù)0
        .subscribe(new Consumer<Integer>() {

            @Override
            public void accept(Integer t) throws Exception {
                System.out.println("--> accept elementAt(2): " + t);
            }
        });

輸出:

--> accept elementAt(2): 0

Javadoc: elementAt(index, defaultItem)

9.3 elementAtOrError(index)

發(fā)射索引位置第 index 項(xiàng)數(shù)據(jù)(從0開始計(jì)數(shù)),如果指定發(fā)射的數(shù)據(jù)不存在,會(huì)發(fā)射NoSuchElementException 異常通知。

img-elementAtOrError(index)

實(shí)例代碼:

    // 3. elementAtOrError(long index)
    // 如果指定發(fā)射的數(shù)據(jù)不存在,會(huì)拋出NoSuchElementException
    Observable.range(1, 10)
        .elementAtOrError(50) // 發(fā)射索引為50的數(shù)據(jù),不存在則發(fā)送NoSuchElementException異常通知
        .subscribe(new SingleObserver<Integer>() {

            @Override
            public void onSubscribe(Disposable d) {
                System.out.println("--> onSubscribe(3): ");
            }

            @Override
            public void onSuccess(Integer t) {
                System.out.println("--> onSuccess(3): " + t);
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("--> onError(3): " + e);
            }
        });

輸出:

--> onSubscribe(3): 
--> onError(3): java.util.NoSuchElementException

Javadoc: elementAtOrError(index)

10. ignoreElements

不發(fā)射任何數(shù)據(jù),只發(fā)射Observable的終止通知。

IgnoreElements 操作符抑制原始Observable發(fā)射的所有數(shù)據(jù),只允許它的終止通知 (onError 或 onCompleted )通過。

img-ignoreElements

解析: 如果你不關(guān)心一個(gè)Observable發(fā)射的數(shù)據(jù),但是希望在它完成時(shí)或遇到錯(cuò)誤終止時(shí)收到通知,你可以對(duì)Observable使用 ignoreElements 操作符,它會(huì)確保永遠(yuǎn)不會(huì)調(diào)用觀察者的 onNext() 方法。

實(shí)例代碼:

    // ignoreElements()
    // 只接受onError或onCompleted通知,攔截onNext事件(不關(guān)心發(fā)射的數(shù)據(jù),只希望在成功或者失敗的時(shí)候收到通知)
    Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
            emitter.onNext(1);
            //  int i = 1/0;
            emitter.onComplete();
        }
    }).ignoreElements()
      .subscribe(new CompletableObserver() {

            @Override
            public void onSubscribe(Disposable d) {
                System.out.println("--> onSubscribe");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("--> onError: " + e);
            }

            @Override
            public void onComplete() {
                System.out.println("--> onComplete");
            }
      });

輸出:

--> onSubscribe
--> onComplete

Javadoc: ignoreElements()

11. Last

只發(fā)射最后一項(xiàng)(或者滿足某個(gè)條件的最后一項(xiàng))數(shù)據(jù)。

如果你只對(duì)Observable發(fā)射的最后一項(xiàng)數(shù)據(jù),或者滿足某個(gè)條件的最后一項(xiàng)數(shù)據(jù)感興趣,你可以使用 Last 操作符。

Last 有以下幾種操作:

11.1 lastElement()

只發(fā)射最后一項(xiàng)數(shù)據(jù),使用沒有參數(shù)的 last 操作符,如果Observable中沒有數(shù)據(jù)發(fā)送,則同樣沒有數(shù)據(jù)發(fā)送。

img-lastElement

實(shí)例代碼:

    // 1. lastElement()
    // 接受最后一項(xiàng)數(shù)據(jù)
    Observable.create(new ObservableOnSubscribe<Integer>() {

        @Override
        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
            emitter.onNext(1);
            emitter.onNext(2);
            emitter.onNext(3);
            emitter.onComplete();
        }
    }).lastElement() // 存在數(shù)據(jù)發(fā)送的話,即發(fā)射最后一項(xiàng)數(shù)據(jù),否則沒有數(shù)據(jù)發(fā)射
      .subscribe(new Consumer<Integer>() {

            @Override
            public void accept(Integer t) throws Exception {
                System.out.println("--> accept lastElement(1): " + t);
            }
        });

輸出:

--> accept lastElement(1): 3

Javadoc: lastElement()

11.2 last(defaultItem)

只發(fā)射最后一項(xiàng)數(shù)據(jù),如果Observable中沒有數(shù)據(jù)發(fā)送,則發(fā)送指定的默認(rèn)值 defaultItem。

img-last(defaultItem)

實(shí)例代碼:

    // 2. last(Integer defaultItem)
    // 接受最后一項(xiàng)數(shù)據(jù),如果沒有數(shù)據(jù)發(fā)送,發(fā)送默認(rèn)數(shù)據(jù):defaultItem
    Observable.range(0, 0)
        .last(999) // 接受最后一項(xiàng)數(shù)據(jù),沒有數(shù)據(jù)則發(fā)送默認(rèn)數(shù)據(jù)999
        .subscribe(new Consumer<Integer>() {

            @Override
            public void accept(Integer t) throws Exception {
                System.out.println("--> accept: last(2): " + t);
            }
        });

輸出:

--> accept: last(2): 999

Javadoc: last(defaultItem)

11.3 lastOrError()

接受最后一項(xiàng)數(shù)據(jù),如果沒有數(shù)據(jù)發(fā)送,拋出 NoSuchElementException 異常通知。

img-lastOrError

實(shí)例代碼:

    // 3. lastOrError()
    // 接受最后一項(xiàng)數(shù)據(jù),如果沒有數(shù)據(jù)發(fā)送,拋出onError: NoSuchElementException
    Observable.range(0, 0)
        .lastOrError() // 接受最后一項(xiàng)數(shù)據(jù),如果沒有數(shù)據(jù),則反射NoSuchElementException異常通知
        .subscribe(new SingleObserver<Integer>() {

            @Override
            public void onSubscribe(Disposable d) {
                System.out.println("--> onSubscribe: ");
            }

            @Override
            public void onSuccess(Integer t) {
                System.out.println("--> onSuccess(3)");
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("--> onError(3): " + e);
            }
        });

輸出:

--> onSubscribe: 
--> onError(3): java.util.NoSuchElementException

Javadoc: lastOrError()

12. Take

使用Take操作符讓你可以修改Observable的行為,只返回前面的N項(xiàng)數(shù)據(jù),然后發(fā)射完成通知,忽略剩余的數(shù)據(jù)。

Take 操作符有以下幾種操作:

12.1 take(count)

如果你對(duì)一個(gè)Observable使用 take(n) 操作符,而那個(gè)Observable發(fā)射的數(shù)據(jù)少于N項(xiàng),那么 take 操作生成的Observable不會(huì)拋異?;虬l(fā)射 onError 通知,在完成前它只會(huì)發(fā)射相同的少量數(shù)據(jù)。

img-take(count)

實(shí)例代碼:

    // 1. take(long count)
    // 返回前count項(xiàng)數(shù)據(jù)
    Observable.range(1, 100)
        .take(5) // 返回前5項(xiàng)數(shù)據(jù)
        .subscribe(new Consumer<Integer>() {

            @Override
            public void accept(Integer t) throws Exception {
                System.out.println("--> accept take(1): " + t);
            }
        });

輸出:

--> accept take(1): 1
--> accept take(1): 2
--> accept take(1): 3
--> accept take(1): 4
--> accept take(1): 5

Javadoc: take(count)

12.2 take(timeout, TimeUnit)

取一定時(shí)間間隔內(nèi)的數(shù)據(jù),有可選參數(shù) scheduler 指定線程調(diào)度器。

img-Take(timeout, TimeUnit)

實(shí)例代碼:

    // 2. take(long time, TimeUnit unit,[Scheduler] scheduler)
    //  取一定時(shí)間間隔內(nèi)的數(shù)據(jù),可選參數(shù)scheduler指定線程調(diào)度器
    Observable.intervalRange(1, 10, 1, 1, TimeUnit.SECONDS)
        .take(5, TimeUnit.SECONDS) // 返回前5秒的數(shù)據(jù)項(xiàng)
        .subscribe(new Consumer<Long>() {

            @Override
            public void accept(Long t) throws Exception {
                System.out.println("--> accept take(2): " + t);
            }
        });

輸出:

--> accept take(2): 1
--> accept take(2): 2
--> accept take(2): 3
--> accept take(2): 4
--> accept take(2): 5

Javadoc: take(timeout, TimeUnit)
Javadoc: take(timeout, TimeUnit, Scheduler)

13. TakeLast

使用 TakeLast 操作符修改原始Observable,你可以只發(fā)射Observable發(fā)射的后N項(xiàng)數(shù)據(jù),忽略前面的數(shù)據(jù)。

takeLast 的這個(gè)變體默認(rèn)在 computation 調(diào)度器上執(zhí)行,但是你可以使用第三個(gè)參數(shù)指定其它的調(diào)度器。

TakeLast 一般有下面幾種操作:

13.1 takeLast(count)

使用 takeLast(count) 操作符,你可以只發(fā)射原始Observable發(fā)射的后 count 項(xiàng)數(shù)據(jù)(或者原始Observable發(fā)射onCompleted() 前的 count 項(xiàng)數(shù)據(jù)),忽略之前的數(shù)據(jù)。 注意:這會(huì)延遲原始Observable發(fā)射的任何數(shù)據(jù)項(xiàng),直到它全部完成。

img-takeLast(count)

實(shí)例代碼:

    // 1. takeLast(int count)
    // 接受Observable數(shù)據(jù)發(fā)射完成前的Count項(xiàng)數(shù)據(jù), 忽略前面的數(shù)據(jù)
    Observable.range(1, 10)
            .doOnNext(new Consumer<Integer>() {
                @Override
                public void accept(Integer t) throws Exception {
                    System.out.println("--> accept(1): " + t);
                }
            })
            .doOnComplete(new Action() {
                @Override
                public void run() throws Exception {
                    System.out.println("--> onCompleted(1): ");
                }
            })
            .takeLast(5) // 發(fā)送數(shù)據(jù)發(fā)射完成前的5項(xiàng)數(shù)據(jù)
            .subscribe(new Consumer<Integer>() {

                @Override
                public void accept(Integer t) throws Exception {
                    System.out.println("--> accept takeLast(1): " + t);
                }
            });

輸出:

--> accept(1): 1
--> accept(1): 2
--> accept(1): 3
--> accept(1): 4
--> accept(1): 5
--> accept(1): 6
--> accept(1): 7
--> accept(1): 8
--> accept(1): 9
--> accept(1): 10
--> onCompleted(1): 
--> accept takeLast(1): 6
--> accept takeLast(1): 7
--> accept takeLast(1): 8
--> accept takeLast(1): 9
--> accept takeLast(1): 10

Javadoc: takeLast(count)

13.2 takeLast(time, TimeUnit)

還有一個(gè) takeLast 變體接受一個(gè)時(shí)長(zhǎng)而不是數(shù)量參數(shù)。它會(huì)發(fā)射在原始Observable的生命周期內(nèi)最后一段時(shí)間內(nèi)發(fā)射的數(shù)據(jù)。時(shí)長(zhǎng)和時(shí)間單位通過參數(shù)指定。

注意: 這會(huì)延遲原始Observable發(fā)射的任何數(shù)據(jù)項(xiàng),直到它全部完成。

img-takeLast(time, TimeUnit)

實(shí)例代碼:

    // 2. takeLast(long time, TimeUnit unit, Scheduler scheduler, boolean delayError, int bufferSize)
    // 可選參數(shù) scheduler:指定工作調(diào)度器  delayError:延遲Error通知  bufferSize:指定緩存大小
    // 接受Observable數(shù)據(jù)發(fā)射完成前指定時(shí)間間隔發(fā)射的數(shù)據(jù)項(xiàng)
    Observable.intervalRange(1, 5, 1, 1, TimeUnit.SECONDS)
            .doOnNext(new Consumer<Long>() {
                @Override
                public void accept(Long t) throws Exception {
                    System.out.println("--> accept(2): " + t);
                }
            })
            .doOnComplete(new Action() {
                @Override
                public void run() throws Exception {
                    System.out.println("--> onCompleted(2): ");
                }
            })
            .takeLast(3, TimeUnit.SECONDS) // 發(fā)送數(shù)據(jù)發(fā)射完成前3秒時(shí)間段內(nèi)的數(shù)據(jù)
            .subscribe(new Consumer<Long>() {

                @Override
                public void accept(Long t) throws Exception {
                    System.out.println("--> accept takeLast(2): " + t);
                }
            });                                                     

輸出:

--> accept(2): 1
--> accept(2): 2
--> accept(2): 3
--> accept(2): 4
--> accept(2): 5
--> onCompleted(2): 
--> accept takeLast(2): 3
--> accept takeLast(2): 4
--> accept takeLast(2): 5

Javadoc: takeLast(long time, TimeUnit unit)
Javadoc: takeLast(long time, TimeUnit unit, boolean delayError)
Javadoc: takeLast(long time, TimeUnit unit, Scheduler scheduler)
Javadoc: takeLast(long time, TimeUnit unit, Scheduler scheduler, boolean delayError)
Javadoc: takeLast(long time, TimeUnit unit, Scheduler scheduler, boolean delayError, int bufferSize)

13.3 takeLast(count, time, TimeUnit)

接受 Observable 發(fā)射完成前 time 時(shí)間段內(nèi)收集 count 項(xiàng)數(shù)據(jù)并發(fā)射。

img-takeLast(count, time, TimeUnit)

示例代碼:

    // 3. takeLast(long count, long time, TimeUnit unit, Scheduler scheduler, boolean delayError, int bufferSize)
    // 可選參數(shù) scheduler:指定工作調(diào)度器  delayError:延遲Error通知  bufferSize:指定緩存大小
    // 接受Observable數(shù)據(jù)發(fā)射完成前time時(shí)間段內(nèi)收集count項(xiàng)數(shù)據(jù)并發(fā)射
    Observable.intervalRange(1, 10, 1, 100, TimeUnit.MILLISECONDS)
            .doOnNext(new Consumer<Long>() {
                @Override
                public void accept(Long t) throws Exception {
                    System.out.println("--> accept(3): " + t);
                }
            })
            .doOnComplete(new Action() {
                @Override
                public void run() throws Exception {
                    System.out.println("--> onCompleted(3): ");
                }
            })
            .takeLast(2, 500, TimeUnit.MILLISECONDS) // 在原數(shù)據(jù)發(fā)射完成前500毫秒內(nèi)接受2項(xiàng)數(shù)據(jù)
            .subscribe(new Consumer<Long>() {

                @Override
                public void accept(Long t) throws Exception {
                    System.out.println("--> accept takeLast(3): " + t);
                }
            });

輸出:

--> accept(3): 1
--> accept(3): 2
--> accept(3): 3
--> accept(3): 4
--> accept(3): 5
--> accept(3): 6
--> accept(3): 7
--> accept(3): 8
--> accept(3): 9
--> accept(3): 10
--> onCompleted(3): 
--> accept takeLast(3): 9
--> accept takeLast(3): 10

Javadoc: takeLast(long count, long time, TimeUnit unit)
Javadoc: takeLast(long count, long time, TimeUnit unit, Scheduler scheduler)
Javadoc: takeLast(long count, long time, TimeUnit unit, Scheduler scheduler, boolean delayError, int bufferSize)

14. OfType

ofType 是 filter 操作符的一個(gè)特殊形式。它過濾一個(gè)Observable只返回指定類型的數(shù)據(jù)。

img-OfType

示例代碼:

    Object[] dataObjects = {1, "Hello", 2.1f, 8.88, "1", new Integer(5)};
    // ofType(Class clazz)
    // 過濾數(shù)據(jù),只返回特定類型的數(shù)據(jù)
    Observable.fromArray(dataObjects)
            .ofType(Integer.class) // 過濾Integer類型的數(shù)據(jù)
            .subscribe(new Consumer<Integer>() {

                @Override
                public void accept(Integer t) throws Exception {
                    System.out.println("--> accept ofType: " + t);
                }
            });

輸出:

--> accept ofType: 1
--> accept ofType: 5

Javadoc: ofType(Class clazz)

小結(jié):

數(shù)據(jù)過濾的操作符主要是過濾被觀察者(Observable)發(fā)射的數(shù)據(jù)序列,按照指定的規(guī)則過濾數(shù)據(jù)項(xiàng),忽略并丟棄其他的數(shù)據(jù)。實(shí)際開發(fā)場(chǎng)景如網(wǎng)絡(luò)數(shù)據(jù)的過濾,數(shù)據(jù)庫數(shù)據(jù)的過濾等,是開發(fā)中重要且常見的操作之一。

Rx介紹與講解及完整目錄參考:Rxjava2 介紹與詳解實(shí)例

實(shí)例代碼:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容