- Learning RxJava
- Thomas Nield
- 193字
- 2021-07-02 22:22:57
elementAt()
You can get a specific emission by its index specified by a Long, starting at 0. After that item is found and emitted, onComplete() will be called and the subscription will be disposed of.
If you want to get the fourth emission coming from an Observable, you can do it as shown in the following code snippet:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable.just("Alpha", "Beta", "Zeta", "Eta", "Gamma",
"Delta")
.elementAt(3)
.subscribe(i -> System.out.println("RECEIVED: " + i));
}
}
The output of the following code snippet is as follows:
RECEIVED: Eta
You may not have noticed, but elementAt() returns Maybe<T> instead of Observable<T>. This is because it will yield one emission, but if there are fewer emissions than the sought index, it will be empty.
There are other flavors of elementAt(), such as elementAtOrError(), which return a Single and will emit an error if an element at that index is not found. singleElement() will turn an Observable into a Maybe, but will produce an error if there is anything beyond one element. Finally, firstElement() and lastElement() will yield, maybe emitting the first or last emission, respectively.
- Mastering JavaScript Functional Programming
- Delphi程序設計基礎:教程、實驗、習題
- PHP 7底層設計與源碼實現
- Visual Basic程序設計教程
- 數據庫系統原理及MySQL應用教程
- Clojure for Domain:specific Languages
- Elastic Stack應用寶典
- 從0到1:Python數據分析
- Procedural Content Generation for C++ Game Development
- Babylon.js Essentials
- Statistical Application Development with R and Python(Second Edition)
- 跟戴銘學iOS編程:理順核心知識點
- 分布式架構原理與實踐
- Python Machine Learning Cookbook
- Implementing Microsoft Dynamics NAV(Third Edition)