RxJava学习(7): 转换-Transformation of sequences

前言

根据Froussios英文版的学习笔记。

一、map 、flatmap (用的最多的就是这俩了)与concatmap

  1. map :对Observable发射的每一项数据应用一个函数,执行变换操作
  2. flatmap:将一个发射数据的Observable变换为多个Observables,然后将它们发射的数据合并后放进一个单独的Observable
  3. concatMap : 与 flatmap 类似,但是不会交错顺序
   Observable.just(1, 2, 2, 3)
            .map(new Function() {
                @Override
                public String apply(Integer integer) throws Exception {
                    return integer+"转1";
                }
            })
            .flatMap(new Function>() {
                @Override
                public ObservableSource apply(String s) throws Exception {
                    return Observable.just(s+"转2");
                }
            })
            .subscribe(new Consumer() {
                @Override
                public void accept(String s) throws Exception {
                    Log.d("pngpng", s);
                }
            }, throwable -> Log.e("pngpng", "just error", throwable));

二、timeStamp与timeInterval

  1. timeStamp: 给Observable发射的数据项附加一个时间戳
   Observable.interval(1000,TimeUnit.MILLISECONDS)
            .take(3)
            .timestamp()
            .subscribe(new Consumer>() {
                @Override
                public void accept(Timed longTimed) throws Exception {
                    Log.d("pngpng",longTimed+"");
                }
            }, throwable -> Log.e("pngpng", "just error", throwable));

结果为:

D/pngpng: Timed[time=1528358894819, unit=MILLISECONDS, value=0]
D/pngpng: Timed[time=1528358895818, unit=MILLISECONDS, value=1]
D/pngpng: Timed[time=1528358896818, unit=MILLISECONDS, value=2]
  1. timeInterval: 将一个发射数据的Observable转换为发射那些数据发射时间间隔的Observable
   Observable.interval(1000,TimeUnit.MILLISECONDS)
            .take(3)
            .timeInterval()
            .subscribe(new Consumer>() {
                @Override
                public void accept(Timed longTimed) throws Exception {
                    Log.d("pngpng",longTimed+"");
                }
            }, throwable -> Log.e("pngpng", "just error", throwable));

结果为:

D/pngpng: Timed[time=1002, unit=MILLISECONDS, value=0]
D/pngpng: Timed[time=999, unit=MILLISECONDS, value=1]
D/pngpng: Timed[time=1000, unit=MILLISECONDS, value=2]

三、flatMapIterable

    Observable.just(1,2,3)
            .flatMapIterable(new Function>() {
                @Override
                public Iterable apply(Integer integer) throws Exception {
                    ArrayList strings = new ArrayList<>();
                    strings.add("a");
                    strings.add("b");
                    return strings;
                }
            })
            .subscribe(new Consumer() {
                @Override
                public void accept(String s) throws Exception {
                    Log.d("pngpng",s);
                }
            }, throwable -> Log.e("pngpng", "just error", throwable));

结果为:

D/pngpng: a
D/pngpng: b
D/pngpng: a
D/pngpng: b
D/pngpng: a
D/pngpng: b

后记

一定要收藏好https://rxmarbles.com/ 这个网址,记不得的时候去看看就好啦,图片很详细的。

你可能感兴趣的:(RxJava学习(7): 转换-Transformation of sequences)