RxJava 操作符(过滤、组合)

2018-05-30  本文已影响78人  IT一书生

过滤操作符

过滤操作符是过滤和选择Observable发射的数据序列,让Observable只返回满足条件的数据。

       Observable.just(1, 2, 3, 5)
                .filter(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        // 限制条件
                        return integer > 2;
                    }
                })
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.e("zpan", " filter =" + integer);
                    }
                });
E/zpan:  filter =3
E/zpan:  filter =5
      Observable.just(1, 2, 3, 4)
                .elementAt(3)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.e("zpan", " element =" + integer);
                    }
                });
E/zpan:  element =4
        Observable.just(1, 4, 8, 5, 5, 1, 5, 6, 8)
                .distinct()
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.e("zpan", " distinct=" + integer);
                    }
                });
E/zpan:  distinct=1
     distinct=4
     distinct=8
     distinct=5
     distinct=6
        Observable.just(1, 2, 3, 4, 5)
                .skip(2)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.e("zpan", " skip = " + integer);
                    }
                });
E/zpan:  skip = 3
     skip = 4
     skip = 5
        Observable.just(1, 2, 3, 4, 5)
                .take(2)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.e("zpan", " take = " + integer);
                    }
                });
E/zpan:  take = 1
     take = 2
        Observable.just(1, 2, 3, 4)
                .ignoreElements()
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onCompleted() {
                        Log.e("zpan", "==onCompleted=====");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("zpan", "==onError=====");
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.e("zpan", "==onNext=====");
                    }
                });
E/zpan: ==onCompleted=====
        Observable.just(1, 2, "3", "4")
                .ofType(String.class)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        Log.e("zpan", "ofType = " + s);
                    }
                });
E/zpan: ofType = 3
    ofType = 4
        Observable.just(1, 2, 3, 4)
                .first(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        return integer > 2;
                    }
                })
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.e("zpan"," first =" + integer);
                    }
                });
E/zpan:  first =3
       Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                for (int i = 0; i < 10; i++) {
                    subscriber.onNext(i);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    subscriber.onCompleted();
                }
            }
        })
                // 每隔100ms发射一个数据,throttleFirst设定的时间是200ms,所以它会发射每个200ms内的第一个数据
                .throttleFirst(200, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.e("zpan", " throttleFirst=" + integer);
                    }
                });

组合操作符

组合操作符可以同时处理多个Observable来创建我们所需要的Observable。

        Observable.just(4,5,6)
                .startWith(1,2)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.e("zpan"," startWith = " + integer);
                    }
                });
E/zpan:  startWith = 1
     startWith = 2
     startWith = 4
     startWith = 5
     startWith = 6
Observable<Integer> o1 = Observable.just(1, 2, 3);
        Observable<String> o2 = Observable.just("7", "8", "9");
        Observable.merge(o1,o2)
                .subscribe(new Action1<Serializable>() {
                    @Override
                    public void call(Serializable serializable) {
                        Log.e("zpan","merge = " + serializable.toString());     
                    }
                });
E/zpan: merge = 1
    merge = 2
    merge = 3
    merge = 7
    merge = 8
    merge = 9
        Observable<Integer> o1 = Observable.just(1, 2);
        Observable<Integer> o2 = Observable.just(6, 7);
        Observable.concat(o1, o2)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.e("zpan", " concat = " + integer);
                    }
                });
E/zpan:  concat = 1
     concat = 2
     concat = 6
     concat = 7
        Observable<Integer> o1 = Observable.just(1, 2);
        Observable<String> o2 = Observable.just("- A", "- B");
        Observable.zip(o1, o2, new Func2<Integer, String, Object>() {
            @Override
            public Object call(Integer integer, String s) {
                return integer + s;
            }
        })
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        Log.e("zpan", " zip = " + o);
                    }
                });
E/zpan:  zip = 1- A
E/zpan:  zip = 2- B
        Observable<Integer> o1 = Observable.just(1, 2);
        Observable<String> o2 = Observable.just("- A", "- B");
        Observable.combineLatest(o1, o2, new Func2<Integer, String, Object>() {
            @Override
            public Object call(Integer integer, String s) {
                return integer + s;
            }
        })
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        Log.e("zpan", " combineLatest= " + o);
                    }
                });
E/zpan:  combineLatest = 2- A
E/zpan:  combineLatest = 2- B
        Observable<Integer> o1 = Observable.just(1, 2);
        Observable<String> o2 = Observable.just("- c", "- d");
        o1.join(o2, new Func1<Integer, Observable<Long>>() {
            @Override
            public Observable<Long> call(Integer integer) {
                return Observable.timer(1, TimeUnit.SECONDS);
            }
        }, new Func1<String, Observable<Long>>() {
            @Override
            public Observable<Long> call(String s) {
                return Observable.timer(0, TimeUnit.SECONDS);
            }
        }, new Func2<Integer, String, Object>() {
            @Override
            public Object call(Integer integer, String s) {
                return integer + s;
            }
        })
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        Log.e("zpan", " join= " + o);
                    }
                });
E/zpan:  join= 2- c
     join= 1- c
     join= 2- d
     join= 1- d
上一篇下一篇

猜你喜欢

热点阅读