Rx系列<第十二篇>:RxJava之过滤操作符

过滤操作符分为四类
1.指定条件过滤事件
2.指定事件数量过滤事件
3.指定时间过滤事件
4.指定事件位置过滤事件

指定条件过滤事件

(1)filter

过滤指定条件。

    Observable.just("1", "2", "3", "4", "5").filter(new Predicate() {
        @Override
        public boolean test(String s) throws Exception {
            // 根据test()的返回值 对被观察者发送的事件进行过滤 & 筛选
            // a. 返回true,则继续发送
            // b. 返回false,则不发送(即过滤)
            if(s.equals("1") || s.equals("2")){
                return true;
            }
            return false;
        }
    }).subscribeOn(Schedulers.newThread()).subscribe(new Consumer() {
        @Override
        public void accept(String s) throws Exception {
            System.out.println(s);
        }
    });

结果:返回1和2。

(2)ofType

过滤指定类型。

    Observable.just("A", "B", 1, 2).ofType(Integer.class)
            .subscribeOn(Schedulers.newThread()).subscribe(new Consumer() {
        @Override
        public void accept(Integer integer) throws Exception {
            System.out.println(String.valueOf(integer));
        }
    });

结果返回:1和2

(3)skip和skipLast

skip:从前往后跳过
skipLast:从后往前跳过

    Observable.just("A", "B", "1", "2").skip(3)
            .subscribeOn(Schedulers.newThread()).subscribe(new Consumer() {
        @Override
        public void accept(String s) throws Exception {
            System.out.println(String.valueOf(s));
        }
    });

返回结果:2

    Observable.just("A", "B", "1", "2").skipLast(3)
            .subscribeOn(Schedulers.newThread()).subscribe(new Consumer() {
        @Override
        public void accept(String s) throws Exception {
            System.out.println(String.valueOf(s));
        }
    });

返回结果:A

(4)distinct和distinctUntilChanged

distinct:过滤相同的数据
distinctUntilChanged:过滤连续相同的数据

    Observable.just(1, 2, 1, 2, 3, 3, 5)
            .distinct()
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

返回结果:1 2 3 5

    Observable.just(1, 2, 1, 2, 3, 3, 5)
            .distinctUntilChanged()
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

返回结果:1 2 1 2 3 5

指定数量过滤事件

(1)take和takeLast

take:观察者只能接收到前n个数据
takeLast:观察者只能接收到后n个数据

    Observable.just(1, 2, 3, 4, 5)
            .take(2)
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

返回结果:1 2

    Observable.just(1, 2, 3, 4, 5)
            .takeLast(2)
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

返回结果:4 5

指定时间过滤事件

(1)throttleFirst和throttleLast

throttleFirst:接收者只会接收到在指定时间内发送的第一条数据
throttleLast:接收者只会接收到在指定时间内发送的最后一条数据

    Observable.create(new ObservableOnSubscribe() {
        @Override
        public void subscribe(ObservableEmitter e) throws Exception {
            //每隔500毫秒发送一条数据
            e.onNext(1);
            Thread.sleep(500);
            e.onNext(2);
            Thread.sleep(500);
            e.onNext(3);
            Thread.sleep(500);
            e.onNext(4);
            Thread.sleep(500);
            e.onNext(5);
            Thread.sleep(500);
            e.onNext(6);
            Thread.sleep(500);
            e.onNext(7);
            Thread.sleep(500);
            e.onNext(8);
            Thread.sleep(500);
            e.onNext(9);
            Thread.sleep(500);
        }
    })
            .throttleFirst(1700, TimeUnit.MILLISECONDS)
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

返回结果:1 5 9

    Observable.create(new ObservableOnSubscribe() {
        @Override
        public void subscribe(ObservableEmitter e) throws Exception {
            //每隔500毫秒发送一条数据
            e.onNext(1);
            Thread.sleep(500);
            e.onNext(2);
            Thread.sleep(500);
            e.onNext(3);
            Thread.sleep(500);
            e.onNext(4);
            Thread.sleep(500);
            e.onNext(5);
            Thread.sleep(500);
            e.onNext(6);
            Thread.sleep(500);
            e.onNext(7);
            Thread.sleep(500);
            e.onNext(8);
            Thread.sleep(500);
            e.onNext(9);
            Thread.sleep(500);
        }
    })
            .throttleLast(1700, TimeUnit.MILLISECONDS)
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

返回结果:4 7 9

(2)sample

与throttleLast类似,sample接收者只会接收到在指定时间内发送的最后一条数据

    Observable.create(new ObservableOnSubscribe() {
        @Override
        public void subscribe(ObservableEmitter e) throws Exception {
            //每隔500毫秒发送一条数据
            e.onNext(1);
            Thread.sleep(500);
            e.onNext(2);
            Thread.sleep(500);
            e.onNext(3);
            Thread.sleep(500);
            e.onNext(4);
            Thread.sleep(500);
            e.onNext(5);
            Thread.sleep(500);
            e.onNext(6);
            Thread.sleep(500);
            e.onNext(7);
            Thread.sleep(500);
            e.onNext(8);
            Thread.sleep(500);
            e.onNext(9);
            Thread.sleep(500);
        }
    })
            .sample(1700, TimeUnit.MILLISECONDS)
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

返回结果:4 7 9

(3)throttleWithTimeout和debounce

throttleWithTimeout和debounce是一样的

假设指定时间为1秒, 那么被观察者发送第一条数据时,等待1秒之后仍然没有发送第二条数据,那么观察者将收到该数据,如果还没到1秒钟就重新发送了数据,那么第一条数据就被取消发送。

    Observable.create(new ObservableOnSubscribe() {
        @Override
        public void subscribe(ObservableEmitter e) throws Exception {
            //每隔500毫秒发送一条数据
            e.onNext(1);
            Thread.sleep(500);
            e.onNext(2);
            Thread.sleep(500);
            e.onNext(3);
            Thread.sleep(500);
            e.onNext(4);
            Thread.sleep(500);
            e.onNext(5);
            Thread.sleep(500);
            e.onNext(6);
            Thread.sleep(500);
            e.onNext(7);
            Thread.sleep(500);
            e.onNext(8);
            Thread.sleep(500);
            e.onNext(9);
            Thread.sleep(500);
        }
    })
            .throttleWithTimeout(600, TimeUnit.MILLISECONDS)
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

返回结果:9

指定事件位置过滤事件

(1)firstElement和lastElement

firstElement:只取第一个元素
lastElement:只取最后一个元素

    Observable.just(1, 2, 3, 4, 5)
            .firstElement()
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

返回结果:1

    Observable.just(1, 2, 3, 4, 5)
            .lastElement()
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

返回结果:5

(2)elementAt

elementAt:只接收指定位置的元素(角标从0开始)

    Observable.just(1, 2, 3, 4, 5)
            .elementAt(2)
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

返回结果:3

(3)elementAtOrError

elementAtOrError:elementAt即使角标越界也不会报错,但是elementAtOrError如果角标越界的话会报错。

    Observable.just(1, 2, 3, 4, 5)
            .elementAtOrError(5)
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            });

以上代码报错如下:

图片.png

为了防止崩溃,还是把代码补全的好,添加接收异常的回调

    Observable.just(1, 2, 3, 4, 5)
            .elementAtOrError(5)
            .subscribeOn(Schedulers.newThread())
            .subscribe(new Consumer() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(String.valueOf(integer));
                }
            }, new Consumer() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    System.out.println("角标越界了");
                }
            });

返回结果:角标越界了

你可能感兴趣的:(Rx系列<第十二篇>:RxJava之过滤操作符)