过滤操作符first和last
目录
first
1) first作用和使用场景
作用:返回一个新的Single或者Maybe,在源Publisher发射内容不为空的情况下将源Publisher发射的第一个项目作为发射项,如果为空则发射指定的默认项或者NoSuchElementException信号,对于Maybe则直接完成。
使用场景:如果您只对Publisher发出的第一个项目或满足某些条件的第一个项目感兴趣,则可以使用first操作符过滤Publisher。
2) first接口
Single<T> | first(T defaultItem) Returns a Single that emits only the very first item emitted by this Flowable, or a default item if this Flowable completes without emitting anything. 返回一个Single,它仅发出此Flowable发出的第一个项目,如果此Flowable完成而没有发出任何内容,则返回默认项目。 |
Maybe<T> | firstElement() Returns a Maybe that emits only the very first item emitted by this Flowable or completes if this Flowable is empty. 返回一个Maybe,它只发出此Flowable发出的第一个项,或者如果此Flowable为空则完成。 |
Single<T> | firstOrError() Returns a Single that emits only the very first item emitted by this Flowable or signals a 返回一个Single,它仅发出此Flowable发出的第一个项,或者如果此Flowable为空则发出NoSuchElementException信号。 |
3) first图解说明
仅仅只是将源Publisher的第一个发射项作为新发射源的唯一发值
4) first测试用例
测试代码
@Test
public void first() {
System.out.println("######first#####");
System.out.println("测试1:源Publisher不为空");
Flowable.just("item2", "item1", "item7").first("item0").subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
System.out.println("s = " + s);
}
});
System.out.println("测试2:源Publisher为空,first发射默认值");
Flowable.empty().first("item0").subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
System.out.println("o = " + o.toString());
}
});
System.out.println("测试3:源Publisher为空,发射错误信号");
Flowable.empty().firstOrError().subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
System.out.println("o = " + o.toString());
}
});
}
测试结果:
######first#####
测试1:源Publisher不为空
s = item2
测试2:源Publisher为空,first发射默认值
o = item0
测试3:源Publisher为空,发射错误信号
java.util.NoSuchElementException
last
1) last作用和使用场景
作用:last操作符和first刚好相反,对对于Single,在源Publisher不为空的情况下返回它发射的项目的最后一项,如果为空则发射指定默认值或者发出NoSuchElementException信号,对于Maybe如果有则发射源Publisher发射的项目的最后一项,没有则直接完成
使用场景:如果只对源Publisher发出的最后一项或满足某些条件的最后一项感兴趣,可以使用last运算符过滤源Publisher。
2) last接口
Single<T> | last(T defaultItem) Returns a Single that emits only the last item emitted by this Flowable, or a default item if this Flowable completes without emitting any items. 返回一个Single,仅发出此Flowable发出的最后一项,如果此Flowable完成而不发出任何项,则返回默认项。 |
Maybe<T> | lastElement() Returns a Maybe that emits the last item emitted by this Flowable or completes if this Flowable is empty. 返回一个Maybe,它发出此Flowable发出的最后一项,或者如果此Flowable为空则完成。 |
Single<T> | lastOrError() Returns a Single that emits only the last item emitted by this Flowable or signals a 返回一个Single,它仅发出此Flowable发出的最后一项,或者如果此Flowable为空则发出NoSuchElementException信号。 |
3) last图解和说明
4) last测试用例
测试代码
@Test
public void last() {
System.out.println("######last#####");
System.out.println("测试1:源Publisher不为空");
Flowable.just("item2", "item1", "item7").last("item0").subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
System.out.println("s = " + s);
}
});
System.out.println("测试2:源Publisher为空,last");
Flowable.empty().first("item0").subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
System.out.println("o = " + o.toString());
}
});
System.out.println("测试3:源Publisher为空,发射错误信号");
Flowable.empty().lastOrError().subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
System.out.println("o = " + o.toString());
}
});
}
测试结果:
######last#####
测试1:源Publisher不为空
s = item7
测试2:源Publisher为空,last
o = item0
测试3:源Publisher为空,发射错误信号java.util.NoSuchElementException