作用:从事件中是不是 满足 得 到结果( 一个)
1 all
2 contains
3 any
4 isEmpty
5 defaultIfEmpty
6 skipWhile
1 all
判断发送的每项数据是否都满足 设置的函数条件
判断发送的数据中是否包含指定数据
若包含,返回 true;否则,返回 false
内部实现 = exists()
若满足,返回 true;否则,返回 false
@Test
public void testAll() throws Exception {
Observable.just(8, 5, 3, 4, 5).all(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) throws Exception {
return integer > 2;
}
}).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
System.out.println(aBoolean);
}
});
}
输出结果为 true
如果:return integer > 3;
则输出false
2 contains
@Test
public void testContains() throws Exception {
Observable.just(3, 4, 5, 6).contains(5).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
System.out.println(aBoolean);
}
});
}
输出结果:true
3 any
@Test
public void testAny() throws Exception {
Observable.just(1, 2, 3).any(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) throws Exception {
return integer == 3;
}
}).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
System.out.println(aBoolean);
}
});
}
输出结果为 true
4 isEmpty
判断发送的数据是否为空
若为空,返回 true;否则,返回 false
@Test
public void testIsEmpty() {
Observable.just(1, 2, 3, 4, 5, 6).isEmpty().subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
System.out.println(aBoolean);
}
});
Observable.empty().isEmpty().subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
System.out.println(aBoolean);
}
});
}
输出结果:
false
true
5 defaultIfEmpty
在不发送任何有效事件( Next事件)、仅发送了 Complete 事件的前提下,发送一个默认值
@Test
public void defaultEmpty() {
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
// 不发送任何有效事件
// e.onNext(1);
// e.onNext(2);
// 仅发送Complete事件
e.onComplete();
}
}).defaultIfEmpty(10) // 若仅发送了Complete事件,默认发送 值 = 10
.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
System.out.println("开始采用subscribe连接");
}
@Override
public void onNext(Integer value) {
System.out.println("接收到了事件" + value);
}
@Override
public void onError(Throwable e) {
System.out.println("对Error事件作出响应");
}
@Override
public void onComplete() {
System.out.println("对Complete事件作出响应");
}
});
}
开始采用subscribe连接
接收到了事件10
对Complete事件作出响应
6 skipWhile
判断发送的每项数据是否满足 设置函数条件
直到该判断条件 = false时,才开始发送Observable的数据
@Test
public void testSkipWhile() throws Exception {
// Observable.intervalRange(0, 5, 0, 100, TimeUnit.MILLISECONDS)
// .skipWhile(new Predicate<Long>() {
// @Override
// public boolean test(Long aLong) throws Exception {
// return aLong < 2;
// }
// }).subscribe(new Consumer<Long>() {
// @Override
// public void accept(Long aLong) throws Exception {
// System.out.println(aLong);
// }
// });
Observable.just(1,2,3,4,5,6,7,8).skipWhile(new Predicate<Integer>() {
@Override
public boolean test(Integer integer) throws Exception {
return integer<4;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println(integer + "");
}
});
}
4
5
6
7
8