- 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
- R語言經典實例(原書第2版)
- Three.js開發指南:基于WebGL和HTML5在網頁上渲染3D圖形和動畫(原書第3版)
- 微服務設計原理與架構
- Learning Laravel 4 Application Development
- Visual Basic程序設計與應用實踐教程
- INSTANT Django 1.5 Application Development Starter
- 程序設計基礎教程:C語言
- Python High Performance Programming
- Python極簡講義:一本書入門數據分析與機器學習
- 大話Java:程序設計從入門到精通
- C++ Fundamentals
- Android應用開發實戰
- Oracle Database XE 11gR2 Jump Start Guide
- Google Adsense優化實戰
- 計算機應用基礎案例教程(第二版)