RxJava操作符三

doOnNext操作符

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

public class RxJavaDemo {

    public static void main(String[] args) {
        Observable myObservableF = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter observableEmitter) throws Exception {
                observableEmitter.onNext("test1");
                observableEmitter.onNext("test2");
                observableEmitter.onNext("test3");
                observableEmitter.onComplete();
            }
        });

        myObservableF.doOnNext(new Consumer() {
            @Override
            public void accept(String s) throws Exception {
                System.out.println(s);
            }
        }).subscribe(new Consumer() {
            @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 myObservableF = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter observableEmitter) throws Exception {
                observableEmitter.onNext("test1");
                observableEmitter.onNext("test2");
                observableEmitter.onNext("test3");
                observableEmitter.onComplete();
            }
        });

        myObservableF.skip(1)
                .subscribe(new Consumer() {
                    @Override
                    public void accept(String s) throws Exception {
                        System.out.println(s);
                    }
                });
    }
}

小结

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

你可能感兴趣的:(RxJava操作符三)