RxJava2 的基础方法使用(一)

本文介绍了RxJava2中的一些基础方法,包括just用于发送数据,zip进行事件组合,map对数据类型进行转换,以及flatMap和concatMap在数据转换中的区别。通过实例分析展示了它们的使用场景和行为特性,帮助理解这些方法在实际应用中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:Rxjava2,想想写个基础的方法使用小结

1、just

io.reactivex.Observable.just("1", "2")
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String strings) throws Exception {

                    }
                });

just用来依次发送数据,用法可以参考上面的代码,但是注意 just()里参数是一串数据,但是不能单独写个变量,只能在参数里写,否则返回的数据还是原来的数组,即:

 String[] nn = {"1", "2"};
 io.reactivex.Observable.just(nn)
             .subscribe(new Consumer<String[]>() {
                  @Override
                  public void accept(String[] strings) throws Exception {
                        
                  }
              });

2、zip

Observable.zip(new Observable<String>() {
            @Override
            protected void subscribeActual(Observer<? super String> observer) {

            }
        }, new Observable<Integer>() {
            @Override
            protected void subscribeActual(Observer<? super Integer> observer) {

            }
        }, new BiFunction<String, Integer, String>() {
            @Override
            public String apply(String s, Integer integer) throws Exception {
                return s + integer;
            }
        }).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {

            }
        });

多个事件组合合并方法,两两对应,多余的方法舍弃。BiFunction 这个参数就是合并后的回调,泛型前面是对应合并事件的泛型,最后一个是返回的数据类型。

3、map

    Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("1");
            }
        }).map(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) throws Exception {
                return Integer.parseInt(s);
            }
        }).subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {

            }
        });

对数据的参数类型进行加工, map第一个参数是当前的数据类型,第二个参数是需要加工后返回的参数类型。

4、flatMap&concatMap

 io.reactivex.Observable.create(new ObservableOnSubscribe<NumberPicker[]>() {
            @Override
            public void subscribe(ObservableEmitter<NumberPicker[]> e) throws Exception {

            }
        }).flatMap(new Function<NumberPicker[], ObservableSource<NumberPicker>>() {
            @Override
            public ObservableSource<NumberPicker> apply(NumberPicker[] numberPickers) throws Exception {
                return io.reactivex.Observable.fromArray(numberPickers);
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<NumberPicker>() {
                    @Override
                    public void accept(NumberPicker numberPicker) throws Exception {

                    }
                });

flatMap和map类似,不过第二个参数是ObservableSource<T>类型的对象。返回的是Observable<T>的对象,因为

Observable<T> implements ObservableSource<T>

实例分析比较flatMap 以及 concatMap的区别:

  • flatMap实例
io.reactivex.Observable.create(new ObservableOnSubscribe<CustomerInfor>() {
            @Override
            public void subscribe(ObservableEmitter<CustomerInfor> e) throws Exception {
                ArrayList<CustomerInfor> list = new ArrayList<>();
                CustomerInfor customerInfor = new CustomerInfor();
                customerInfor.setAddress("科技");
                customerInfor.setAddressDetail("0000");
                list.add(customerInfor);
                CustomerInfor customerInfor1 = new CustomerInfor();
                customerInfor1.setAddress("贝图");
                customerInfor1.setAddressDetail("1111");
                list.add(customerInfor1);
                CustomerInfor customerInfor2 = new CustomerInfor();
                customerInfor2.setAddress("贝1图");
                customerInfor2.setAddressDetail("133111");
                list.add(customerInfor2);
                for (CustomerInfor customerInfor3 : list) {
                    e.onNext(customerInfor3);
                }
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new Function<CustomerInfor, ObservableSource<String>>() {
                    @Override
                    public ObservableSource<String> apply(CustomerInfor customerInfor) throws Exception {
                        List<String> nn = new ArrayList<>();
                        nn.add(customerInfor.getAddress());
                        nn.add(customerInfor.getAddressDetail());
                        return Observable.fromIterable(nn).delay(10, TimeUnit.MILLISECONDS);
                    }
                })
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        Log.e("TAG_RE", s);
                    }
                });

打印出来的结果:

2019-06-04 17:11:43.227 10417-10523/com.besttop.new_xbsy E/TAG_RE: 科技
2019-06-04 17:11:43.228 10417-10523/com.besttop.new_xbsy E/TAG_RE: 0000
2019-06-04 17:11:43.230 10417-10524/com.besttop.new_xbsy E/TAG_RE: 贝图
2019-06-04 17:11:43.231 10417-10524/com.besttop.new_xbsy E/TAG_RE: 1111
2019-06-04 17:11:43.233 10417-10525/com.besttop.new_xbsy E/TAG_RE: 贝1图
2019-06-04 17:11:43.233 10417-10525/com.besttop.new_xbsy E/TAG_RE: 133111
2019-06-04 17:12:31.873 10417-10523/com.besttop.new_xbsy E/TAG_RE: 科技
2019-06-04 17:12:31.874 10417-10524/com.besttop.new_xbsy E/TAG_RE: 贝图
2019-06-04 17:12:31.874 10417-10524/com.besttop.new_xbsy E/TAG_RE: 1111
2019-06-04 17:12:31.876 10417-10524/com.besttop.new_xbsy E/TAG_RE: 贝1图
2019-06-04 17:12:31.877 10417-10524/com.besttop.new_xbsy E/TAG_RE: 133111
2019-06-04 17:12:31.877 10417-10524/com.besttop.new_xbsy E/TAG_RE: 0000

分析:通过两次执行这个事件,发现第一次和第二次的事件顺序不一样,不是执行完一个customer的信息后再执行另外一个,它是无序的。。。

  • 把.flatMap替换为.concatMap,打印结果如下:
2019-06-04 17:19:31.873 11948-12065/com.besttop.new_xbsy E/TAG_RE: 科技
2019-06-04 17:19:31.873 11948-12065/com.besttop.new_xbsy E/TAG_RE: 0000
2019-06-04 17:19:31.885 11948-12066/com.besttop.new_xbsy E/TAG_RE: 贝图
2019-06-04 17:19:31.886 11948-12066/com.besttop.new_xbsy E/TAG_RE: 1111
2019-06-04 17:19:31.897 11948-12067/com.besttop.new_xbsy E/TAG_RE: 贝1图
2019-06-04 17:19:31.898 11948-12067/com.besttop.new_xbsy E/TAG_RE: 133111
2019-06-04 17:20:34.828 11948-12065/com.besttop.new_xbsy E/TAG_RE: 科技
2019-06-04 17:20:34.832 11948-12065/com.besttop.new_xbsy E/TAG_RE: 0000
2019-06-04 17:20:34.843 11948-12066/com.besttop.new_xbsy E/TAG_RE: 贝图
2019-06-04 17:20:34.844 11948-12066/com.besttop.new_xbsy E/TAG_RE: 1111
2019-06-04 17:20:34.855 11948-12067/com.besttop.new_xbsy E/TAG_RE: 贝1图
2019-06-04 17:20:34.855 11948-12067/com.besttop.new_xbsy E/TAG_RE: 133111
2019-06-04 17:22:29.447 11948-12065/com.besttop.new_xbsy E/TAG_RE: 科技
2019-06-04 17:22:29.448 11948-12065/com.besttop.new_xbsy E/TAG_RE: 0000
2019-06-04 17:22:29.459 11948-12066/com.besttop.new_xbsy E/TAG_RE: 贝图
2019-06-04 17:22:29.460 11948-12066/com.besttop.new_xbsy E/TAG_RE: 1111
2019-06-04 17:22:29.471 11948-12067/com.besttop.new_xbsy E/TAG_RE: 贝1图
2019-06-04 17:22:29.471 11948-12067/com.besttop.new_xbsy E/TAG_RE: 133111

分析:根据打印结果,可以很明显的发现,使用concatMap后事件是有序的打印出来的,每一个consumer信息打印完了才会打印下一个数据,故flatMap和concatMap都是 可以进行数据转换,但是他们一个是有序的(concatMap),一个是无序(flatMap)的。

5、doOnNext

和map类似,改变下边执行方法的数据,但是不能改变数据类型,map是可以的。(这个实际中不太常用,可以了解下)

6、filter

Observable.just(1, 3, 100, -3, 999, -89, 33, 0)
                .filter(new Predicate<Integer>() {
                    @Override
                    public boolean test(Integer integer) throws Exception {
                        return integer <= 0;
                    }
                }).subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                Log.e("TAGWW", integer + "->");
            }
        });

打印结果:

2019-06-04 17:34:48.386 14925-14925/com.besttop.new_xbsy E/TAGWW: -3->
2019-06-04 17:34:48.386 14925-14925/com.besttop.new_xbsy E/TAGWW: -89->
2019-06-04 17:34:48.386 14925-14925/com.besttop.new_xbsy E/TAGWW: 0->
2019-06-04 17:35:11.884 14925-14925/com.besttop.new_xbsy E/TAGWW: -3->
2019-06-04 17:35:11.884 14925-14925/com.besttop.new_xbsy E/TAGWW: -89->
2019-06-04 17:35:11.885 14925-14925/com.besttop.new_xbsy E/TAGWW: 0->

分析:过滤的作用,Predicate这个对象里有个test方法里就是过滤的条件,返回true的就是可以通过的数据,false的就是需要被过滤掉的数据。

  Observable.just(1, 3, 100, -3, 999, -89, 33, 0)
                .filter(integer -> true).subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                Log.e("TAGWW", integer + "->");
            }
        });

打印数据:

2019-06-04 17:40:02.061 15626-15626/com.besttop.new_xbsy E/TAGWW: 1->
2019-06-04 17:40:02.061 15626-15626/com.besttop.new_xbsy E/TAGWW: 3->
2019-06-04 17:40:02.061 15626-15626/com.besttop.new_xbsy E/TAGWW: 100->
2019-06-04 17:40:02.061 15626-15626/com.besttop.new_xbsy E/TAGWW: -3->
2019-06-04 17:40:02.061 15626-15626/com.besttop.new_xbsy E/TAGWW: 999->
2019-06-04 17:40:02.061 15626-15626/com.besttop.new_xbsy E/TAGWW: -89->
2019-06-04 17:40:02.061 15626-15626/com.besttop.new_xbsy E/TAGWW: 33->
2019-06-04 17:40:02.061 15626-15626/com.besttop.new_xbsy E/TAGWW: 0->

以上过滤方法其实没有什么作用,因为没有条件都返回true。类似的返回false,那就是把所有的数据都过滤掉了,故过滤的重点就是test返回的Boolean值。

总结:还有其他一些方法,不过不太常用,可以了解下https://www.jianshu.com/p/2327e5deb491

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值