In this post under RxJava, I will explain with example how to use the static “range” method of Observable class.
This static method generates a stream of integers which fall within the specified range.
The method takes two arguments
1) The starting integer
2) The count of sequential integers from the starting integer.
Below is an example
Example
1 import io.reactivex.rxjava3.core.Observable;
2
3 public class Example3 {
4 public static void main(String[] args) {
5 Observable<Integer> observable = Observable.range(0, 10);
6 observable.subscribe(s -> System.out.println(s));
7 }
8 }
In the above code, at line 5, I have created an Observable instance using static “range” method passing 0 as the starting integer and 10 as the count of sequential integers from 0.
In the above code at line 6, we subscribe to the Observable by calling the “subscribe” method, and passing the callback functionality as an argument to “subscribe” method. This callback function is called whenever an item is sent from the Observable. In our example this callback function simply prints the item sent.
The output will be as shown below
Output
0
1
2
3
4
5
6
7
8
9