官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 博罗县| 西盟| 军事| 宜阳县| 夏河县| 石林| 仁寿县| 冷水江市| 栾川县| 北安市| 永顺县| 缙云县| 遵化市| 沾益县| 中江县| 阳朔县| 宁德市| 崇仁县| 涞源县| 保山市| 肃宁县| 垫江县| 朝阳县| 江油市| 丁青县| 开化县| 西平县| 威信县| 库尔勒市| 汤阴县| 柳州市| 黔江区| 东乡县| 麟游县| 从江县| 荆州市| 白城市| 德兴市| 济南市| 阳谷县| 桂东县|