RxJava操作符

create

public static  Observable create(ObservableOnSubscribe source)

变换操作符

map将发送的数据应用特定的方法再次发送出去。

public final  Observable map(Function mapper)

例如:

Observable.just("#0").map(new Function() {
            @Override
            public String apply(String s) throws Exception {
                return "hello world" + s;
            }
        }).subscribe(new Consumer() {
            @Override
            public void accept(String s) throws Exception {
                // System.out: accept: hello world#0
                System.out.println("accept: " + s);
            }
        });

flatMap将发送的数据应用特定的方法,转换为另一个数据源发送出去。

public final  Observable flatMap(Function> mapper)

例如:

Observable.just("#0", "#1", "#2", "#3").flatMap(new Function>() {
            @Override
            public ObservableSource apply(String s) throws Exception {
                long ts = 0;
                if (s.equals("#2")) {
                    ts = 1000;
                }
                return Observable.just("hello world" + s).delay(ts, TimeUnit.MILLISECONDS);
            }
        }, false, 3).subscribe(new Consumer() {
            @Override
            public void accept(String s) throws Exception {
                // System.out: accept: hello world#0
                // System.out: accept: hello world#1
                // System.out: accept: hello world#3
                // System.out: accept: hello world#2
                System.out.println("accept: " + s);
            }
        });

concatMap 作用类似于flatMap,只不过concatMap是有序的。

public final  Observable concatMap(Function> mapper)

例如:

Observable.just("#0", "#1", "#2", "#3").concatMap(new Function>() {
            @Override
            public ObservableSource apply(String s) throws Exception {
                long ts = 0;
                if (s.equals("#2")) {
                    ts = 1000;
                }
                return Observable.just("hello world" + s).delay(ts, TimeUnit.MILLISECONDS);
            }
        }).subscribe(new Consumer() {
            @Override
            public void accept(String s) throws Exception {
                // System.out: accept: hello world#0
                // System.out: accept: hello world#1
                // System.out: accept: hello world#2
                // System.out: accept: hello world#3
                System.out.println("accept: " + s);
            }
        });

zip 合并多个数据源

public static  Observable zip(Iterable> sources, Function zipper)
public static  Observable zip(ObservableSource> sources, final Function zipper)
public static  Observable zip(
            ObservableSource source1, ObservableSource source2,
            BiFunction zipper)
// 最多有9个参数的数据源

例如:

Observable.zip(Observable.just("#1"), Observable.just("#2"), new BiFunction() {
            @Override
            public String apply(String s, String s2) throws Exception {
                return s + "-" + s2;
            }
        }).subscribe(new Consumer() {
            @Override
            public void accept(String s) throws Exception {
                // System.out: accept: #1-#2
                System.out.println("accept: " + s);
            }
        });

amb/ambArray

amb 从一个ObservableSources迭代器中竞争出一个ObservableSources,哪个Observable首先发射了数据(包括onError和onComplete)就会继续发射这个Observable的数据,其他的Observable所发射的数据都会别丢弃。

public static  Observable amb(Iterable> sources) 

ambArray 从一个ObservableSources数组竞争出一个ObservableSources,哪个Observable首先发射了数据(包括onError和onComplete)就会继续发射这个Observable的数据,其他的Observable所发射的数据都会别丢弃。

public static  Observable ambArray(ObservableSource... sources)

例如:

        // 1
        Observable.ambArray(
                Observable.just("#1").delay(1000, TimeUnit.MILLISECONDS),
                Observable.just("#2"))
                .subscribe(new Consumer() {
                    @Override
                    public void accept(String s) throws Exception {
                        // System.out: accept: #2
                        System.out.println("accept: " + s);
                    }
                });

        // 2
        Observable.amb(new Iterable>() {
            @NonNull
            @Override
            public Iterator> iterator() {
                List> list = new ArrayList<>();
                for (int i = 0; i < 5; i++) {
                    final int finalI = i;
                    list.add(new Observable() {
                        @Override
                        protected void subscribeActual(Observer observer) {
                            observer.onNext("#" + finalI);
                        }
                    });
                }
                return list.iterator();
            }
        }).subscribe(new Consumer() {
            @Override
            public void accept(String s) throws Exception {
                // System.out: accept: #0
                System.out.println("accept: " + s);
            }
        });

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