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