目录
1 接口
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. 返回仅发出此Flowable发出的最后一项的Single,如果此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信号。 |
2 图解和说明
3 测试用例
@Test
public void last() {
System.out.println("------> last");
Flowable.just("one","two","three").last("is empty").subscribe(new SingleObserver<Object>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onSuccess(Object value) {
System.out.println("onSuccess:" + value.toString());
}
@Override
public void onError(Throwable e) {
System.out.println("onError:" + e.getMessage().toString());
}
});
Flowable.empty().last("is empty").subscribe(new SingleObserver<Object>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onSuccess(Object value) {
System.out.println("onSuccess:" + value.toString());
}
@Override
public void onError(Throwable e) {
System.out.println("onError:" + e.getMessage().toString());
}
});
System.out.println("------> lastElement");
Flowable.just("one","two","three").lastElement().subscribe(new MaybeObserver<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onSuccess(String value) {
System.out.println("onSuccess:" + value.toString());
}
@Override
public void onError(Throwable e) {
System.out.println("onError:" + e.getMessage().toString());
}
@Override
public void onComplete() {
}
});
Flowable.empty().lastElement().subscribe(new MaybeObserver<Object>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onSuccess(Object value) {
System.out.println("onSuccess:" + value.toString());
}
@Override
public void onError(Throwable e) {
System.out.println("onError:" + e.getMessage().toString());
}
@Override
public void onComplete() {
System.out.println("onComplete");
}
});
System.out.println("------> lastOrError");
Flowable.just("one","two","three").lastOrError().subscribe(new SingleObserver<Object>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onSuccess(Object value) {
System.out.println("onSuccess:" + value.toString());
}
@Override
public void onError(Throwable e) {
System.out.println("onError:" + e.getMessage().toString());
}
});
Flowable.empty().lastOrError().subscribe(new SingleObserver<Object>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onSuccess(Object value) {
System.out.println("onSuccess:" + value.toString());
}
@Override
public void onError(Throwable e) {
System.out.println("onError:");
}
});
}
------> last
onSuccess:three
onSuccess:is empty
------> lastElement
onSuccess:three
onComplete
------> lastOrError
onSuccess:three
onError: