乐虎游戏|乐虎国际登录|欢迎你

Android拾萃 - RxJava2之变换操作符及其demo

日期:2020-03-01编辑作者:计算机资讯

清汤寡水在Lacrossexjava中本人会用interval当放大计时器与flatmap结合发起互联网央浼.在类型中的实际须求是每四个钟头乞请三遍,那条拾壹分就跳过,继续号令下一条,可是发现叁个难点不怕央浼或者会超时非常.为了譬如方便,今后本人将代码改为贴近的恐怕回到十分的简约替代

本文独家宣布到公众号:Android技能杂货铺

  • Android拾萃 - 大切诺基xJava2操作符汇总
  • Android拾萃 - PRADOxJava2之创设操作符及其demo
 public static void main(String[] args) { Observable.interval(0, 1, TimeUnit.SECONDS, Schedulers.trampoline .flatMap(new Function<Long, ObservableSource<Long>>() { @Override public ObservableSource<Long> apply(Long aLong) throws Exception { return Observable.just(aLong == 3 ? aLong / 0 : aLong * 2); } }) .subscribe(new Consumer<Long>() { @Override public void accept(Long integer) throws Exception { System.out.println(integer + ""); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { System.out.println(throwable.toString; }

图片 1

一、调换操作符列表

操作符 解析
buffer() 缓存,可以简单的理解为缓存,它定期从Observable收集数据到一个集合,然后把这些数据集合打包发射,而不是一次发射一个
map() 对序列的每一项都应用一个函数来变换Observable发射的数据序列
flatMap() , concatMap() , flatMapIterable() 将Observable发射的数据集合变换为Observables集合,然后将这些Observable发射的数据平坦化的放进一个单独的Observable
switchMap() 将Observable发射的数据集合变换为Observables集合,然后只发射这些Observables最近发射的数据
scan() 对Observable发射的每一项数据应用一个函数,然后按顺序依次发射每一个值
groupBy() 将Observable分拆为Observable集合,将原始Observable发射的数据按Key分组,每一个Observable发射一组不同的数据
buffer() 它定期从Observable收集数据到一个集合,然后把这些数据集合打包发射,而不是一次发射一个
window() 定期将来自Observable的数据分拆成一些Observable窗口,然后发射这些窗口,而不是每次发射一项
cast() 在发射之前强制将Observable发射的所有数据转换为指定类型

大家的想望是失误调用错误管理后interval继续发射数量,继续呼吁下二个,大家看一打字与印刷结果:

封面图-pixabay

二、转变操作符

map 事件指标的直接转换 map(卡塔尔国是一定的中转

图片 2

map.png

//事件的参数类型也由int转为Bitmap
        Observable.just(R.drawable.ic_launcher_round).map(new Function<Integer, Bitmap>() {
            @Override
            public Bitmap apply(@NonNull Integer id) throws Exception {
                return BitmapFactory.decodeResource(getResources(), id);
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bitmap>() {
                    @Override
                    public void accept(Bitmap bitmap) throws Exception {
                        showBitmap(bitmap);
                    }
                });


private void showBitmap(Bitmap bitmap) {
        imageView.setImageBitmap(bitmap);
    }

位置的代码直接将财富展现到了Imagview上边

flatMap( )
我们知晓from/just会对数码举行依次分发,可是flatmap会退换那些作为,被订阅时将兼具数据传递完成汇总到叁个Observable然后挨门挨户实行onNext方法(奉行各种不做决定).

图片 3

flatMap.png

实际是flatMap会创立超多的Observa(中游水管State of Qatar,然后一并往下游的一个Observable灌数据这时候,哪个人的多寡先踏向上游水管是迫于调整的.
以下的图来源互连网,侵删~~

图片 4

flatmap_水管.png

早先得到ip一向接纳InetAddress.getByName(domain卡塔尔国.getHostAddress(卡塔尔(قطر‎的静态方法获取的,不精晓这一次会怎么样间接不能够分析域名?有通晓的分神告诉一下,多谢.前面使用上面包车型客车代码获取到具备的InetAddress

private Collection<InetAddress> getServerIP(String domain) {
        Collection<InetAddress> myServer = new ArrayList<InetAddress>();
        List array =  null;
        try{
            array = Arrays.asList(InetAddress.getAllByName(domain));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        if (array == null || array.size() <= 0 || array.isEmpty()) {
            try {
                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
                while (networkInterfaces.hasMoreElements()) {
                    NetworkInterface networkInterface = networkInterfaces.nextElement();
                    Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                    while (inetAddresses.hasMoreElements()) {
                        InetAddress inetAddress = inetAddresses.nextElement();
                        myServer.add(inetAddress);
                    }
                }
            } catch (SocketException e) {
                e.printStackTrace();
            }
        } else {
            myServer.addAll(array);
        }
        return myServer;
    }

好了,继续上边的难题,我们知晓flatMap会创设比超级多ObservableSource,看一下代码就领悟了,每一次调用一遍createIpObservable就能够回来二个新的createIpObservable.

 private ObservableSource<?> createIpObservable(final String s) {
        return Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> subscriber) throws Exception {
                Collection<InetAddress> array = getServerIP(s);
                for (InetAddress inetAddress : array){
                    if (!TextUtils.isEmpty(inetAddress.getHostAddress())) {
                        String ip = inetAddress.getHostAddress();
                        subscriber.onNext(ip);
                        Log.d(TAG, "Emit Data -> " + s+" : " +ip);
                    }
                }
                subscriber.onComplete();
            }
        }).subscribeOn(Schedulers.newThread());
    }

此处贴上flatMap代码

 private void flatMap(){
        Observable.just(

                "http://www.baidu.com/",
                "http://www.google.com/",
                "https://www.bing.com/").flatMap(new Function<String, ObservableSource<?>>() {
            @Override
            public ObservableSource<?> apply(@NonNull String s) throws Exception {
                return createIpObservable(s);
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Log.i(TAG, o.toString());
                    }
                });

打字与印刷结果如下,第一种获得ip的方法,报错了.从log能够看出来,并不曾据守baidu google bing的顺序,结合方面包车型大巴水管解释是不是很形象.

10-21 17:27:52.008 16057-19602/com.example.philos.rxjavademo W/System.err: java.net.UnknownHostException: Unable to resolve host "https://www.bing.com/": No address associated with hostname
10-21 17:27:52.018 16057-19598/com.example.philos.rxjavademo W/System.err: java.net.UnknownHostException: Unable to resolve host "http://www.baidu.com/": No address associated with hostname
10-21 17:27:52.018 16057-19599/com.example.philos.rxjavademo W/System.err: java.net.UnknownHostException: Unable to resolve host "http://www.google.com/": No address associated with hostname
10-21 17:27:52.018 16057-19599/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
10-21 17:27:52.018 16057-19599/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-21 17:27:52.018 16057-19599/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-21 17:27:52.018 16057-19599/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.getServerIP(TransformOperateExampleActivity.java:340)
10-21 17:27:52.018 16057-19599/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.access$700(TransformOperateExampleActivity.java:50)
10-21 17:27:52.018 16057-19599/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity$18.subscribe(TransformOperateExampleActivity.java:318)
10-21 17:27:52.018 16057-19599/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.getServerIP(TransformOperateExampleActivity.java:340)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.access$700(TransformOperateExampleActivity.java:50)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity$18.subscribe(TransformOperateExampleActivity.java:318)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10903)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err:     at java.lang.Thread.run(Thread.java:841)
10-21 17:27:52.028 16057-19602/com.example.philos.rxjavademo W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.getServerIP(TransformOperateExampleActivity.java:340)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.access$700(TransformOperateExampleActivity.java:50)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity$18.subscribe(TransformOperateExampleActivity.java:318)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10903)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
10-21 17:27:52.028 16057-19598/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
10-21 17:27:52.038 16057-19598/com.example.philos.rxjavademo W/System.err:     at   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-21 17:27:52.038 16057-19599/com.example.philos.rxjavademo W/System.err:     at libcore.io.Posix.getaddrinfo(Native Method)   at io.reactivex.Observable.subscribe(Observable.java:10903)
10-21 17:27:52.038 16057-19599/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
10-21 17:27:52.038 16057-19599/com.example.philos.rxjavademo W/System.err:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
10-21 17:27:52.038 16057-19599/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
10-21 17:27:52.038 16057-19599/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
10-21 17:27:52.038 16057-19602/com.example.philos.rxjavademo W/System.err:  at 
10-21 17:27:52.038 16057-19602/com.example.philos.rxjavademo W/System.err:     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
10-21 17:27:52.038 16057-19602/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
10-21 17:27:52.038 16057-19602/com.example.philos.rxjavademo W/System.err:  ... 17 more
10-21 17:27:52.038 16057-19598/com.example.philos.rxjavademo W/System.err: java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)这里
10-21 17:27:52.038 16057-19598/com.example.philos.rxjavademo W/System.err:     at java.lang.Thread.run(Thread.java:841)
10-21 17:27:52.038 16057-19598/com.example.philos.rxjavademo W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-21 17:27:52.038 16057-19599/com.example.philos.rxjavademo W/System.err: java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-21 17:27:52.038 16057-19599/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
10-21 17:27:52.038 16057-19599/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
10-21 17:27:52.038 16057-19598/com.example.philos.rxjavademo W/System.err:     at   at libcore.io.Posix.getaddrinfo(Native Method)
10-21 17:27:52.038 16057-19598/com.example.philos.rxjavademo W/System.err:     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
10-21 17:27:52.038 16057-19598/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
10-21 17:27:52.038 16057-19598/com.example.philos.rxjavademo W/System.err:  ... 17 more
10-21 17:27:52.048 16057-19599/com.example.philos.rxjavademo W/System.err: java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-21 17:27:52.048 16057-19599/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-21 17:27:52.048 16057-19599/com.example.philos.rxjavademo W/System.err:     at java.lang.Thread.run(Thread.java:841)
10-21 17:27:52.048 16057-19599/com.example.philos.rxjavademo W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-21 17:27:52.048 16057-19599/com.example.philos.rxjavademo W/System.err:     at libcore.io.Posix.getaddrinfo(Native Method)
10-21 17:27:52.048 16057-19599/com.example.philos.rxjavademo W/System.err:     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
10-21 17:27:52.048 16057-19599/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
10-21 17:27:52.048 16057-19599/com.example.philos.rxjavademo W/System.err:  ... 17 more
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ::1%1
10-21 17:27:52.068 16057-19599/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.google.com/ : ::1%1
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 127.0.0.1
10-21 17:27:52.068 16057-19602/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> https://www.bing.com/ : ::1%1
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ::1%1
10-21 17:27:52.068 16057-19599/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.google.com/ : 127.0.0.1
10-21 17:27:52.068 16057-19599/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.google.com/ : fe80::e1d:afff:fec5:96c3%wlan0
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 127.0.0.1
10-21 17:27:52.068 16057-19599/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.google.com/ : 192.168.1.13
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: fe80::e1d:afff:fec5:96c3%wlan0
10-21 17:27:52.068 16057-19599/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.google.com/ : 10.0.2.15
10-21 17:27:52.068 16057-19598/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.baidu.com/ : ::1%1
10-21 17:27:52.068 16057-19598/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.baidu.com/ : 127.0.0.1
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 192.168.1.13
10-21 17:27:52.068 16057-19598/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.baidu.com/ : fe80::e1d:afff:fec5:96c3%wlan0
10-21 17:27:52.068 16057-19598/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.baidu.com/ : 192.168.1.13
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 10.0.2.15
10-21 17:27:52.068 16057-19598/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.baidu.com/ : 10.0.2.15
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ::1%1
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 127.0.0.1
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: fe80::e1d:afff:fec5:96c3%wlan0
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 192.168.1.13
10-21 17:27:52.068 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 10.0.2.15
10-21 17:27:52.078 16057-19602/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> https://www.bing.com/ : 127.0.0.1
10-21 17:27:52.078 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: fe80::e1d:afff:fec5:96c3%wlan0
10-21 17:27:52.078 16057-19602/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> https://www.bing.com/ : fe80::e1d:afff:fec5:96c3%wlan0
10-21 17:27:52.078 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 192.168.1.13
10-21 17:27:52.078 16057-19602/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> https://www.bing.com/ : 192.168.1.13
10-21 17:27:52.078 16057-16057/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 10.0.2.15
10-21 17:27:52.078 16057-19602/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> https://www.bing.com/ : 10.0.2.15

concatMap 和 flatmap很周边,只然则他的水管不再是并连接入中游的,他是遵纪守法顺序串联之后接入中游的,所以她是能够保险发射和接收顺序一致的.

图片 5

concatMap.png

为了相比,我们还是选取方面获得ip的列子,代码如下:

private void concatMap(){
        Observable.fromArray(Arrays.asList(
                "http://www.baidu.com/",
                "http://www.google.com/",
                "https://www.bing.com/")).concatMap(new Function<List<String>, ObservableSource<?>>() {
            @Override
            public ObservableSource<?> apply(@NonNull List<String> strings) throws Exception {
                return createIpObservable(strings);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Log.i(TAG, o.toString());
                    }
                });

    }

上边的代码应用了fromArray,所以一下函数也是有一对出入,那几个也得以看下fromArray和justde

 private ObservableSource<?> createIpObservable(final List<String> urls) {
        return Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> subscriber) throws Exception {
                for (String s : urls) {
                    Collection<InetAddress> array = getServerIP(s);
                    for (InetAddress inetAddress : array){
                        if (!TextUtils.isEmpty(inetAddress.getHostAddress())) {
                            String ip = inetAddress.getHostAddress();
                            subscriber.onNext(ip);
                            Log.d(TAG, "Emit Data -> " + s+" : " +ip);
                        }
                    }
                }
                subscriber.onComplete();
            }
        }).subscribeOn(Schedulers.newThread());
    }

打印的结果如下:

10-21 18:03:44.158 3869-4593/com.example.philos.rxjavademo W/System.err: java.net.UnknownHostException: Unable to resolve host "http://www.baidu.com/": No address associated with hostname
10-21 18:03:44.158 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
10-21 18:03:44.158 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-21 18:03:44.158 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-21 18:03:44.158 3869-4593/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.getServerIP(TransformOperateExampleActivity.java:340)
10-21 18:03:44.158 3869-4593/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.access$700(TransformOperateExampleActivity.java:50)
10-21 18:03:44.158 3869-4593/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity$20.subscribe(TransformOperateExampleActivity.java:370)
10-21 18:03:44.158 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
10-21 18:03:44.158 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10903)
10-21 18:03:44.158 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
10-21 18:03:44.158 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.lang.Thread.run(Thread.java:841)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:     at libcore.io.Posix.getaddrinfo(Native Method)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
10-21 18:03:44.168 3869-4593/com.example.philos.rxjavademo W/System.err:    ... 17 more
10-21 18:03:44.218 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.baidu.com/ : ::1%1
10-21 18:03:44.218 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.baidu.com/ : 127.0.0.1
10-21 18:03:44.218 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.baidu.com/ : fe80::e1d:afff:fec5:96c3%wlan0
10-21 18:03:44.218 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.baidu.com/ : 192.168.1.13
10-21 18:03:44.218 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.baidu.com/ : 10.0.2.15
10-21 18:03:44.218 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ::1%1
10-21 18:03:44.218 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 127.0.0.1
10-21 18:03:44.218 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: fe80::e1d:afff:fec5:96c3%wlan0
10-21 18:03:44.218 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 192.168.1.13
10-21 18:03:44.218 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 10.0.2.15
10-21 18:04:00.568 3869-4593/com.example.philos.rxjavademo W/System.err: java.net.UnknownHostException: Unable to resolve host "http://www.google.com/": No address associated with hostname
10-21 18:04:00.568 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
10-21 18:04:00.568 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-21 18:04:00.568 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-21 18:04:00.578 3869-4593/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.getServerIP(TransformOperateExampleActivity.java:340)
10-21 18:04:00.578 3869-4593/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.access$700(TransformOperateExampleActivity.java:50)
10-21 18:04:00.578 3869-4593/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity$20.subscribe(TransformOperateExampleActivity.java:370)
10-21 18:04:00.578 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
10-21 18:04:00.578 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10903)
10-21 18:04:00.578 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
10-21 18:04:00.578 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
10-21 18:04:00.578 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
10-21 18:04:00.578 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
10-21 18:04:00.578 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-21 18:04:00.588 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
10-21 18:04:00.588 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
10-21 18:04:00.588 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-21 18:04:00.588 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-21 18:04:00.588 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.lang.Thread.run(Thread.java:841)
10-21 18:04:00.588 3869-4593/com.example.philos.rxjavademo W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-21 18:04:00.588 3869-4593/com.example.philos.rxjavademo W/System.err:     at libcore.io.Posix.getaddrinfo(Native Method)
10-21 18:04:00.588 3869-4593/com.example.philos.rxjavademo W/System.err:     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
10-21 18:04:00.588 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
10-21 18:04:00.588 3869-4593/com.example.philos.rxjavademo W/System.err:    ... 17 more
10-21 18:04:00.608 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.google.com/ : ::1%1
10-21 18:04:00.608 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.google.com/ : 127.0.0.1
10-21 18:04:00.608 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.google.com/ : fe80::e1d:afff:fec5:96c3%wlan0
10-21 18:04:00.608 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.google.com/ : 192.168.1.13
10-21 18:04:00.608 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> http://www.google.com/ : 10.0.2.15
10-21 18:04:00.608 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ::1%1
10-21 18:04:00.608 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 127.0.0.1
10-21 18:04:00.608 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: fe80::e1d:afff:fec5:96c3%wlan0
10-21 18:04:00.608 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 192.168.1.13
10-21 18:04:00.608 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 10.0.2.15
10-21 18:04:17.038 3869-4593/com.example.philos.rxjavademo W/System.err: java.net.UnknownHostException: Unable to resolve host "https://www.bing.com/": No address associated with hostname
10-21 18:04:17.038 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
10-21 18:04:17.038 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-21 18:04:17.038 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-21 18:04:17.038 3869-4593/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.getServerIP(TransformOperateExampleActivity.java:340)
10-21 18:04:17.038 3869-4593/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity.access$700(TransformOperateExampleActivity.java:50)
10-21 18:04:17.038 3869-4593/com.example.philos.rxjavademo W/System.err:     at com.example.philos.rxjavademo.operate.TransformOperateExampleActivity$20.subscribe(TransformOperateExampleActivity.java:370)
10-21 18:04:17.038 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
10-21 18:04:17.038 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10903)
10-21 18:04:17.038 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
10-21 18:04:17.048 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
10-21 18:04:17.048 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
10-21 18:04:17.048 3869-4593/com.example.philos.rxjavademo W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
10-21 18:04:17.048 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-21 18:04:17.048 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
10-21 18:04:17.048 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
10-21 18:04:17.048 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-21 18:04:17.048 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-21 18:04:17.048 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.lang.Thread.run(Thread.java:841)
10-21 18:04:17.058 3869-4593/com.example.philos.rxjavademo W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-21 18:04:17.058 3869-4593/com.example.philos.rxjavademo W/System.err:     at libcore.io.Posix.getaddrinfo(Native Method)
10-21 18:04:17.058 3869-4593/com.example.philos.rxjavademo W/System.err:     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
10-21 18:04:17.058 3869-4593/com.example.philos.rxjavademo W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
10-21 18:04:17.058 3869-4593/com.example.philos.rxjavademo W/System.err:    ... 17 more
10-21 18:04:17.078 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> https://www.bing.com/ : ::1%1
10-21 18:04:17.078 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ::1%1
10-21 18:04:17.078 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> https://www.bing.com/ : 127.0.0.1
10-21 18:04:17.078 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> https://www.bing.com/ : fe80::e1d:afff:fec5:96c3%wlan0
10-21 18:04:17.078 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> https://www.bing.com/ : 192.168.1.13
10-21 18:04:17.078 3869-4593/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> https://www.bing.com/ : 10.0.2.15
10-21 18:04:17.078 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 127.0.0.1
10-21 18:04:17.078 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: fe80::e1d:afff:fec5:96c3%wlan0
10-21 18:04:17.078 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 192.168.1.13
10-21 18:04:17.078 3869-3869/com.example.philos.rxjavademo I/TransformOperateExampleActivity: 10.0.2.15

flatMapIterable
flatMapIterable(卡塔尔和flatMap(State of Qatar大约是相仿的(把二个数量产生出五个数据,如图的把三个圆形成了五个别的项目标棱形卡塔尔,分歧是 flatMap 参数把每一种数据调换为 三个新的 Observable,而 flatMapIterable 参数把二个多少转变为二个新的 iterable 对象。
管理局地复杂的多寡,选用叁个Observable之后,重回一个Iterable,然后,那一个Iterable会依次的传递给上边一层仍然是Observer

图片 6

flatMapIterable.png

那边就不去证明他的冬辰性了(请参谋flatmap),这里来叁个简短的例证,大家打字与印刷出B,U,G以致他们背后的二个字符的ASCII码

private void flatMapIterable(){
        Observable.just("B", "U", "G").flatMapIterable(new Function<String, Iterable<Integer>>() {
            @Override
            public Iterable<Integer> apply(@NonNull String s) throws Exception {
                return getIntegers(s);
            }
        }).subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                Log.i(TAG, "ASCII 码:" + integer);
            }
        });

    }

打字与印刷结果如下

10-21 20:35:20.151 25291-25291/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ASCII 码:67
10-21 20:35:20.151 25291-25291/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ASCII 码:68
10-21 20:35:20.151 25291-25291/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ASCII 码:86
10-21 20:35:20.151 25291-25291/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ASCII 码:87
10-21 20:35:20.151 25291-25291/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ASCII 码:72
10-21 20:35:20.151 25291-25291/com.example.philos.rxjavademo I/TransformOperateExampleActivity: ASCII 码:73

switchMap

switchMap(卡塔尔(قطر‎和flatMap(卡塔尔很像,除了某个:每当源Observable发射三个新的数量项(Observable)时,它将注销订阅并结束监视此前特别数据项产生的Observable,并伊始监视当前发射的那三个。

图片 7

switchMap.png

private void  switchMap(){
        Observable.just(
                "http://www.baidu.com/",
                "http://www.google.com/",
                "https://www.bing.com/").switchMap(new Function<String, ObservableSource<String>>() {
            @Override
            public ObservableSource<String> apply(@NonNull String s) throws Exception {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return Observable.just(s).subscribeOn(Schedulers.newThread());
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                Log.i(TAG, "onSubscribe");
            }

            @Override
            public void onNext(@NonNull String s) {
                Log.i(TAG, "onNext:" + s);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.i(TAG, "onError " + e.getMessage());
            }

            @Override
            public void onComplete() {
                Log.i(TAG, "onComplete()");
            }
        });
    }

打字与印刷结果

10-21 20:42:47.721 32149-32149/com.example.philos.rxjavademo I/TransformOperateExampleActivity: onSubscribe
10-21 20:42:53.751 32149-32424/com.example.philos.rxjavademo I/TransformOperateExampleActivity: onNext:https://www.bing.com/
10-21 20:42:53.751 32149-32424/com.example.philos.rxjavademo I/TransformOperateExampleActivity: onComplete()

能够见到,前边三个域名发射出去后都被废除了

scan

scan( 卡塔尔国对三个类别的数量接收三个函数,并将以此函数的结果发射出来作为下个数据利用那些函数时候的首个参数使用,有一点相符于递归操作(第二个由于前边少之又少,只好向来原样输出卡塔尔国

图片 8

scan.jpg

private void scan(){
        Observable.just(1, 2, 3, 4, 5).scan(new BiFunction<Integer, Integer, Integer>() {
            @Override
            public Integer apply(@NonNull Integer integer, @NonNull Integer integer2) throws Exception {
                return integer + integer2;
            }
        }).subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                Log.d(TAG, "Emit Data -> " + integer);
            }
        });
    }

输出结果如下:

10-21 20:54:32.891 10353-10353/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> 1
10-21 20:54:32.891 10353-10353/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> 3
10-21 20:54:32.891 10353-10353/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> 6
10-21 20:54:32.891 10353-10353/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> 10
10-21 20:54:32.891 10353-10353/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Emit Data -> 15

buffer

buffer 先计算一定量的结果,之后再去回调结果给下一个Observable只怕是Observer

图片 9

buffer.png

 Observable.just("A", "B", "C")
                .buffer(2)
                .subscribe(new Consumer<List<String>>() {
                    @Override
                    public void accept(List<String> strings) throws Exception {
                        Log.i(TAG, strings + " == buffer");
                    }
                });

10-21 21:02:40.801 18367-18367/com.example.philos.rxjavademo I/TransformOperateExampleActivity: [A, B] == buffer
10-21 21:02:40.811 18367-18367/com.example.philos.rxjavademo I/TransformOperateExampleActivity: [C] == buffer

buffer(int count, int skip) // 三遍缓存几个,之后每一次跳过多少个,根据顺序,重复,每一次剔除最前方的
buffer(long timespan, long timeshift, TimeUnit unit)
buffer(long timespan, TimeUnit unit)// 每个多长期,取出数据

图片 10

buffer.jpg

经历正上面代码的结果,和上海教室一律

Observable.just("A", "B", "C", "E", "F", "G")
                .buffer(2,3)
                .subscribe(new Consumer<List<String>>() {
                    @Override
                    public void accept(List<String> strings) throws Exception {
                        Log.i(TAG, strings + " == buffer");
                    }
                });

地点的事例看不出有啥样用,那么在来看三个其实的例子.

大家付出有相逢这样的供给,比方地图要求把相邻的人的头像标记到地图上,为了优化聚合功效和体验,常常都是收获了头像之类的有关音讯,之后一切叁只体现到地图上.
上边为伪代码:

 ArrayList<LatLng> latLngs=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            LatLng latLng = new LatLng();
            latLng.setLat(24 + i);
            latLng.setLng(110 + i);
            latLngs.add(latLng);
        }

        Observable
                .fromArray(latLngs).map(new Function<ArrayList<LatLng>, Object>() {
            @Override
            public Object apply(@NonNull ArrayList<LatLng> latLngs) throws Exception {
                //伪代码
                Bitmap bimap = loadAvatar();

//                BitmapDescriptor bitDes = BitmapDescriptorFactory.fromBitmap(bimap);
//                OverlayOptions option = new MarkerOptions().position(latLng).icon(bitDes);
                //获得地图标注
                Object option = null;
                return option;
            }
        }).buffer(latLngs.size())//一次性全部集齐
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.computation())//Schedulers.computation()调度是精于计算工作的,它也是许多RxJava方法的默认调度器   buffer(),debounce(),delay(),interval(),sample(),skip()。
                .subscribe(new Consumer<List<Object>>() {
                    @Override
                    public void accept(List<Object> objects) throws Exception {
//                        baiduMap.addOverlays(overlayOptionses);//全部添加到地图上
                    }
                });
//                .subscribe(new Action1<List<OverlayOptions>>() {
//                    @Override
//                    public void call(List<OverlayOptions> overlayOptionses) {
//                        baiduMap.addOverlays(overlayOptionses);//全部添加到地图上
//                    }
//                });

        Observable.just("A","B","C").buffer(2).subscribe(new Consumer<List<String>>() {
            @Override
            public void accept(List<String> strings) throws Exception {
                Log.i(TAG, strings +" == buffer");
            }
        });

groupBy

groupBy(卡塔尔 分组 就是比照一体系型的key分组,将groupBy将原始Observable遵照key分解为三个发出多少个GroupedObservable的Observable,然后这几个小Observable分别发射其所富含的的数码,一旦有订阅,每一个GroupedObservable就开头缓存数据.其实和SQL中的groupBy近似,找寻相符条件的相干数据。

图片 11

groupby.png

Observable.just("A","AD","BC","AB","C","D").groupBy(new Function<String, Boolean>() {
            @Override
            public Boolean apply(@NonNull String s) throws Exception {
                return s.contains("A");
            }
        }).subscribe(new Consumer<GroupedObservable<Boolean, String>>() {
            @Override
            public void accept(GroupedObservable<Boolean, String> booleanStringGroupedObservable) throws Exception {
                Log.i(TAG, booleanStringGroupedObservable.getKey() + "====" +booleanStringGroupedObservable.toString());
booleanStringGroupedObservable.subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        Log.d(TAG, booleanStringGroupedObservable.getKey() + "包含的数据: " + s);
                    }
                });
            }
        });

据说s.contains("A"卡塔尔(قطر‎条件,大家了然全数的多少会被剪切为带有和不带有三种GroupedObservable,从打字与印刷音讯Log.i(TAG, booleanStringGroupedObservable.getKey(卡塔尔 + "====" +booleanStringGroupedObservable.toString(State of Qatar卡塔尔国;我们只看到了五个,验证了下边包车型大巴理论.可是多少个GroupedObservable发射的数目并不曾前后相继分开,那是因为subscribe订阅的时候,onext并从未这些作用.

10-21 21:23:42.381 6448-6448/com.example.philos.rxjavademo I/TransformOperateExampleActivity: true====io.reactivex.internal.operators.observable.ObservableGroupBy$GroupedUnicast@42e4f390
10-21 21:23:42.381 6448-6448/com.example.philos.rxjavademo D/TransformOperateExampleActivity: true包含的数据: A
10-21 21:23:42.381 6448-6448/com.example.philos.rxjavademo D/TransformOperateExampleActivity: true包含的数据: AD
10-21 21:23:42.381 6448-6448/com.example.philos.rxjavademo I/TransformOperateExampleActivity: false====io.reactivex.internal.operators.observable.ObservableGroupBy$GroupedUnicast@42e50120
10-21 21:23:42.391 6448-6448/com.example.philos.rxjavademo D/TransformOperateExampleActivity: false包含的数据: BC
10-21 21:23:42.391 6448-6448/com.example.philos.rxjavademo D/TransformOperateExampleActivity: true包含的数据: AB
10-21 21:23:42.391 6448-6448/com.example.philos.rxjavademo D/TransformOperateExampleActivity: false包含的数据: C
10-21 21:23:42.391 6448-6448/com.example.philos.rxjavademo D/TransformOperateExampleActivity: false包含的数据: D

window

window操作符与Buffer操作符相通,不过它发出的是Observable而不是列表。

图片 12

window.png

// 每间距ns聚集发射这段时光内的数据,实际不是一有数量就发射。(之发送后面包车型客车10次卡塔尔国 优化接口乞求次数

图片 13

window_skip.jpg

Observable.interval(1, TimeUnit.SECONDS).take(10)
                .window(3, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Observable<Long>>() {
                    @Override
                    public void accept(Observable<Long> observable) throws Exception {
                        Log.d(TAG, "Sub Divide begin....");
                        textView.append("Sub Divide begin ....");
                        textView.append(" ....... ");
                        observable
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Consumer<Long>() {
                                    @Override
                                    public void accept(Long value) {
                                        Log.d(TAG, "Next:" + value);
                                        textView.append("Next:" + value);
                                        textView.append(" ....... ");
                                    }
                                });
                    }
                });

interval 1秒发送三回数据,take(10卡塔尔(قطر‎获取前边的10个数据,window(3, 提姆eUnit.SECONDSState of Qatar3秒订阅叁次,从步向accept函数 Log.d(TAG, "Sub Divide begin...."卡塔尔;的时日间距能够看出.

10-21 21:34:18.051 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Sub Divide begin....
10-21 21:34:19.051 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Next:0
10-21 21:34:20.051 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Next:1
10-21 21:34:21.051 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Sub Divide begin....
10-21 21:34:21.071 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Next:2
10-21 21:34:22.051 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Next:3
10-21 21:34:23.051 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Next:4
10-21 21:34:24.051 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Sub Divide begin....
10-21 21:34:24.081 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Next:5
10-21 21:34:25.051 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Next:6
10-21 21:34:26.051 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Next:7
10-21 21:34:27.051 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Sub Divide begin....
10-21 21:34:27.091 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Next:8
10-21 21:34:27.101 18257-18265/com.example.philos.rxjavademo I/dalvikvm: Total arena pages for JIT: 11
10-21 21:34:27.101 18257-18265/com.example.philos.rxjavademo I/dalvikvm: Total arena pages for JIT: 12
10-21 21:34:28.051 18257-18257/com.example.philos.rxjavademo D/TransformOperateExampleActivity: Next:9

图片 14

device-2017-10-21-213514.png

cast
cast 在发出以前免强将Observable发射的全体数据调换为钦命项目 它是map(State of Qatar操作符的分化日常版本

图片 15

cast.png

public void cast() {
        List<Person> list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            if (i/11 == 0) {
                list.add(new TaiJian("i" + i));
            } else {
                list.add(new Male(true, "i" + i));
            }

        }
        for (Person person : list) {
            Observable.just(person).cast(Male.class).subscribe(new Observer<Male>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {
                    Log.i(TAG, "onSubscribe");
                }

                @Override
                public void onNext(@NonNull Male male) {
                    Log.i(TAG, "name:" + male.name + "n 我是男人:" + male.isMan);
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    Log.i(TAG, "onError: " + e.getMessage());
                }

                @Override
                public void onComplete() {
                    Log.i(TAG, "onComplete");
                }
            });
        }

    }

    class Person {
        String name;

        public Person(String name) {
            this.name = name;
        }
    }

    class Male extends Person {
        public boolean isMan;

        public Male(boolean isMan, String name) {
            super(name);
            this.isMan = isMan;
        }
    }

    class TaiJian extends Male {

        public TaiJian(String name) {
            super(false, name);
        }
    }

到此地,转变操作符都说完了,下一章商讨下过滤操作符.

024java.lang.ArithmeticException: / by zero

近五年来,GL450xJava能够说是特别的凌厉,受到广大开拓者的追求捧场与保养,即便后入门的路子较高,学习话费很大,但是如故引发一场学习牧马人xjava的狂潮。为啥吗?因为途观xJava的特点:轻易的线程切换、流式的API写法和强盛的操作符。那使得我们做异步操作变得很简单,不用像以前肖似写种种Handler来回调主线程,只须要多个操作符一行代码就解决。流式的API使大家的逻辑变得要命清晰,可读性很强。因而,福睿斯xJava也是大家项目重构的利器。

本文由乐虎游戏发布于计算机资讯,转载请注明出处:Android拾萃 - RxJava2之变换操作符及其demo

关键词:

计算机:RxJava2.0(一)

Outline 特此声明:本文为转载文章!尊重原创的劳动果实,严禁剽窃 本文转载于:http://www.jianshu.com/p/464fa025229e 出自于...

详细>>

Rxjava2~zip~学渣带你扣rxjava2~ map操作符到底干了什么

prestatic ObservableString sampleObservable() {returnObservable.defer(new CallableObservableSource? extendsString() {@Overridepublic ObservableSource? ...

详细>>

RXJava的使用<二>

vankeservice 前言 RxJava和Retrofit也火了一段时间了,不过最近一直在学习ReactNative和Node相关的姿势,一直没有时间研究这...

详细>>

OkHttp源码深入解读

简介 目前在HTTP协议请求库中,OKHttp应当是非常火的,使用也非常的简单。网上有很多文章写了关于OkHttp的特点以及使...

详细>>