- Learning RxJava
- Thomas Nield
- 277字
- 2021-07-02 22:22:57
takeWhile() and skipWhile()
Another variant of the take() operator is the takeWhile() operator, which takes emissions while a condition derived from each emission is true. The following example will keep taking emissions while emissions are less than 5. The moment it encounters one that is not, it will call the onComplete() function and dispose of this:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable.range(1,100)
.takeWhile(i -> i < 5)
.subscribe(i -> System.out.println("RECEIVED: " + i));
}
}
The output of the preceding code snippet is as follows:
RECEIVED: 1
RECEIVED: 2
RECEIVED: 3
RECEIVED: 4
Just like the takeWhile() function, there is a skipWhile() function. It will keep skipping emissions while they qualify with a condition. The moment that condition no longer qualifies, the emissions will start going through. In the following code, we skip emissions as long as they are less than or equal to 95. The moment an emission is encountered that does not meet this condition, it will allow all subsequent emissions going forward:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable.range(1,100)
.skipWhile(i -> i <= 95)
.subscribe(i -> System.out.println("RECEIVED: " + i));
}
}
The output of the preceding code snippet is as follows:
RECEIVED: 96
RECEIVED: 97
RECEIVED: 98
RECEIVED: 99
RECEIVED: 100
- 程序員修煉之道:程序設計入門30講
- Java 開發從入門到精通(第2版)
- Learning Real-time Processing with Spark Streaming
- JavaScript高效圖形編程
- 區塊鏈架構與實現:Cosmos詳解
- 程序員面試算法寶典
- Python進階編程:編寫更高效、優雅的Python代碼
- ASP.NET動態網頁設計教程(第三版)
- STM32F0實戰:基于HAL庫開發
- 程序員修煉之道:通向務實的最高境界(第2版)
- 零基礎學Python網絡爬蟲案例實戰全流程詳解(入門與提高篇)
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- Learning Apache Cassandra
- Scratch3.0趣味編程動手玩:比賽訓練營
- 零基礎看圖學ScratchJr:少兒趣味編程(全彩大字版)