本文主要是介绍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>() {@Overridepublic 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>() {@Overridepublic 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>() {@Overridepublic void accept(Boolean aBoolean) throws Exception {System.out.println("aBoolean========" + aBoolean);}});
}
//aBoolean========false@Test
public void isEmpty2() {Observable.empty().isEmpty().subscribe(new Consumer<Boolean>() {@Overridepublic 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>() {@Overridepublic 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>() {@Overridepublic 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>() {@Overridepublic 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>() {@Overridepublic 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>() {@Overridepublic boolean test(Integer integer) throws Exception {return integer < 3;}}).subscribe(new SingleObserver<Boolean>() {@Overridepublic void onSubscribe(Disposable d) {}@Overridepublic void onSuccess(Boolean aBoolean) {System.out.println("aBoolean=============" + aBoolean);}@Overridepublic void onError(Throwable e) {}});
}
//aBoolean=============false@Test
public void all2() {Observable.just(1, 2, 3, 4).all(new Predicate<Integer>() {@Overridepublic boolean test(Integer integer) throws Exception {return integer < 5;}}).subscribe(new SingleObserver<Boolean>() {@Overridepublic void onSubscribe(Disposable d) {}@Overridepublic void onSuccess(Boolean aBoolean) {System.out.println("aBoolean=============" + aBoolean);}@Overridepublic 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>() {@Overridepublic 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>() {@Overridepublic void accept(Long count) throws Exception {System.out.println("发射项目总数 count = " + count);}});
}
//发射项目总数 count = 3
这篇关于rxjava : contains、isEmpty、defaultIfEmpty、switchIfEmpty、all 、 count 、 cast的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!