- Learning RxJava
- Thomas Nield
- 221字
- 2021-07-02 22:22:53
Observable.range()
To emit a consecutive range of integers, you can use Observable.range(). This will emit each number from a start value and increment each emission until the specified count is reached. These numbers are all passed through the onNext() event, followed by the onComplete() event:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable.range(1,10)
.subscribe(s -> System.out.println("RECEIVED: " + s));
}
}
The output is as follows:
RECEIVED: 1
RECEIVED: 2
RECEIVED: 3
RECEIVED: 4
RECEIVED: 5
RECEIVED: 6
RECEIVED: 7
RECEIVED: 8
RECEIVED: 9
RECEIVED: 10
Note closely that the two arguments for Observable.range() are not lower/upper bounds. The first argument is the starting value. The second argument is the total count of emissions, which will include both the initial value and incremented values. Try emitting Observable.range(5,10), and you will notice that it emits 5 followed by the next nine consecutive integers following it (for a grand total of 10 emissions):
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable.range(5,10)
.subscribe(s -> System.out.println("RECEIVED: " + s));
}
}
The output is as follows:
RECEIVED: 5
RECEIVED: 6
RECEIVED: 7
RECEIVED: 8
RECEIVED: 9
RECEIVED: 10
RECEIVED: 11
RECEIVED: 12
RECEIVED: 13
RECEIVED: 14
Note that there is also a long equivalent called Observable.rangeLong() if you need to emit larger numbers.
推薦閱讀
- 零基礎(chǔ)學(xué)Visual C++第3版
- Redis Applied Design Patterns
- Learning Data Mining with Python
- Hands-On RESTful Web Services with Go
- 深度學(xué)習(xí):算法入門與Keras編程實(shí)踐
- 深入淺出RxJS
- Mastering Apache Maven 3
- 深度學(xué)習(xí)原理與PyTorch實(shí)戰(zhàn)(第2版)
- Unity 2018 Augmented Reality Projects
- C++程序設(shè)計(jì)教程(第2版)
- Mastering Gephi Network Visualization
- 官方 Scratch 3.0 編程趣味卡:讓孩子們愛上編程(全彩)
- SAS編程演義
- Getting Started with RethinkDB
- The Applied Data Science Workshop