RxJava操作符(三)

作者: tinuv 发布时间: 2019-09-09 浏览: 378 次 编辑

doOnNext操作符

这个操作符就是让观察者在接受事件之前干一点是,doOnNext()也接受一个consumer的接口

public class RxJavaDemo {
    public static void main(String[] args) {
        Observable<String> myObservableF = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> observableEmitter) throws Exception {
                observableEmitter.onNext("test1");
                observableEmitter.onNext("test2");
                observableEmitter.onNext("test3");
                observableEmitter.onComplete();
            }
        });
        myObservableF.doOnNext(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                System.out.println(s);
            }
        }).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                System.out.println(s);
            }
        });
    }
}

输出的结果是

test1

test1

test2

test2

test3

test3

skip操作符

接受一个long类型参数表示跳过的条数

public class RxJavaDemo {
    public static void main(String[] args) {
        Observable<String> myObservableF = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> observableEmitter) throws Exception {
                observableEmitter.onNext("test1");
                observableEmitter.onNext("test2");
                observableEmitter.onNext("test3");
                observableEmitter.onComplete();
            }
        });
        myObservableF.skip(1)
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        System.out.println(s);
                    }
                });
    }
}

小结

操作符是RxJava的一大特色,RxJava的操作远远不止这些,以后有机会还会慢慢总结 操作符这个部分参考自https://www.jianshu.com/p/0cd258eecf60


原文: https://tinuv.me/2018/07/20/RxJava-operater-three.html