rxjava : contains、isEmpty、defaultIfEmpty、switchIfEmpty、all 、 count 、 cast

contains

contains:如果发射的数据包含指定值,那就返回true

@Test
public void contains1() {
    Observable.just(1, 2, 3)
            .contains(1)
            .subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean aBoolean) throws Exception {
                    System.out.println("aBoolean========" + aBoolean);
                }
            });
}
//aBoolean========true




@Test
public void contains2() {
    Observable.just(11, 2, 3)
            .contains(1)
            .subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean aBoolean) throws Exception {
                    System.out.println("aBoolean========" + aBoolean);
                }
            });
}
//aBoolean========false

isEmpty

isEmpty:判断是否发射的数据为空,如果为空,返回true;如果不为空,返回false

@Test
public void isEmpty1() {
    Observable.just(1, 2, 3)
            .isEmpty()
            .subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean aBoolean) throws Exception {
                    System.out.println("aBoolean========" + aBoolean);
                }
            });
}
//aBoolean========false



@Test
public void isEmpty2() {
    Observable.empty()
            .isEmpty()
            .subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean aBoolean) throws Exception {
                    System.out.println("aBoolean========" + aBoolean);
                }
            });
}
//aBoolean========true

defaultIfEmpty

defaultIfEmpty:如果被观察者发射的数据为空,那么就发射defaultIfEmpty中给的那个值

@Test
public void defaultIfEmpty1() {
    Flowable.just("李晓明", "张宝庆", "赵无极")
            .defaultIfEmpty("请输入姓名")
            .subscribe(new Consumer<String>() {
                @Override
                public void accept(String value) throws Exception {
                    System.out.println("value = " + value);
                }
            });
}
//value = 李晓明
//value = 张宝庆
//value = 赵无极




@Test
public void defaultIfEmpty2() {
    Flowable.empty()
            .defaultIfEmpty("请输入姓名")
            .subscribe(new Consumer<Object>() {
                @Override
                public void accept(Object o) throws Exception {
                    System.out.println("o = " + o);
                }
            });
}
//o = 请输入姓名


switchIfEmpty

@Test
public void switchIfEmpty1() {
    Flowable alternate = Flowable.just("备选方案1", "备选方案2");
    Disposable disposable = Flowable.just("主方案")
            .switchIfEmpty(alternate)
            .subscribe(new Consumer<Object>() {
                @Override
                public void accept(Object o) throws Exception {
                    System.out.println("o = " + o);
                }
            });
}
//o = 主方案





@Test
public void switchIfEmpty2() {
    Flowable alternate = Flowable.just("备选方案1", "备选方案2");
    Disposable disposable = Flowable.empty()
            .switchIfEmpty(alternate)
            .subscribe(new Consumer<Object>() {
                @Override
                public void accept(Object o) throws Exception {
                    System.out.println("o = " + o);
                }
            });
}
//o = 备选方案1
//o = 备选方案2

all

all:判断Observable里的所有数据是否全部发射了,如果没有,返回false

@Test
public void all1() {
    Observable.just(1, 2, 3, 4)
            .all(new Predicate<Integer>() {
                @Override
                public boolean test(Integer integer) throws Exception {
                    return integer < 3;
                }
            })
            .subscribe(new SingleObserver<Boolean>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onSuccess(Boolean aBoolean) {
                    System.out.println("aBoolean=============" + aBoolean);
                }

                @Override
                public void onError(Throwable e) {

                }
            });
}
//aBoolean=============false


@Test
public void all2() {
    Observable.just(1, 2, 3, 4)
            .all(new Predicate<Integer>() {
                @Override
                public boolean test(Integer integer) throws Exception {
                    return integer < 5;
                }
            })
            .subscribe(new SingleObserver<Boolean>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onSuccess(Boolean aBoolean) {
                    System.out.println("aBoolean=============" + aBoolean);
                }

                @Override
                public void onError(Throwable e) {

                }
            });
}
//aBoolean=============true


cast

类型转换

@Test
public void cast() {
    List<Float> source = new ArrayList<>();
    source.add(199f);
    source.add(102f);
    source.add(100f);
    Flowable flowable = Flowable.fromIterable(source);

    Disposable disposable = flowable.cast(Number.class)
            .subscribe(new Consumer<Number>() {
                @Override
                public void accept(Number number) throws Exception {
                    System.out.println(number + " : " 
                    				+ number.getClass());
                }
            });
}
//199.0 : class java.lang.Float
//102.0 : class java.lang.Float
//100.0 : class java.lang.Float

count

count仅仅是统计发射项目数目

@Test
public void count() {
    Single<Long> single = Flowable.just("1", "2", "3").count();
    Disposable disposable = single.subscribe(new Consumer<Long>() {
        @Override
        public void accept(Long count) throws Exception {
            System.out.println("发射项目总数 count = " + count);
        }
    });
}
//发射项目总数 count = 3


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值