美文网首页
RxJava学习(7): 转换-Transformation o

RxJava学习(7): 转换-Transformation o

作者: crossroads | 来源:发表于2018-06-07 17:13 被阅读7次

前言

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

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

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

相关文章

网友评论

      本文标题:RxJava学习(7): 转换-Transformation o

      本文链接:https://www.haomeiwen.com/subject/wjmgsftx.html