- 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
- 零基礎(chǔ)搭建量化投資系統(tǒng):以Python為工具
- 數(shù)字媒體應(yīng)用教程
- Photoshop智能手機(jī)APP UI設(shè)計(jì)之道
- C語(yǔ)言程序設(shè)計(jì)案例式教程
- Python Network Programming Cookbook(Second Edition)
- 零基礎(chǔ)學(xué)Java(第4版)
- 單片機(jī)應(yīng)用與調(diào)試項(xiàng)目教程(C語(yǔ)言版)
- Python忍者秘籍
- Getting Started with Hazelcast(Second Edition)
- Developing SSRS Reports for Dynamics AX
- SQL Server 2016 從入門(mén)到實(shí)戰(zhàn)(視頻教學(xué)版)
- Hacking Android
- Python無(wú)監(jiān)督學(xué)習(xí)
- MySQL核心技術(shù)與最佳實(shí)踐
- 威脅建模:設(shè)計(jì)和交付更安全的軟件