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

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.

主站蜘蛛池模板: 高要市| 富阳市| 綦江县| 平和县| 丹阳市| 尖扎县| 错那县| 哈巴河县| 磐石市| 甘肃省| 普兰店市| 南投市| 蕉岭县| 玉屏| 自贡市| 五大连池市| 德化县| 白玉县| 长春市| 波密县| 灯塔市| 西昌市| 新竹市| 厦门市| 正安县| 岳池县| 介休市| 阳谷县| 江门市| 威信县| 齐齐哈尔市| 三门峡市| 正安县| 崇阳县| 西充县| 上犹县| 驻马店市| 盖州市| 廉江市| 兰西县| 安徽省|