Make writing a habit together! This is the second day of my participation in the “Gold Digging Day New Plan · April More text challenge”. Click here for more details.

Create operator

1.interval

Create an Observable that emits integer sequences at fixed intervals, the equivalent of a timer.

Observable .interval(3, Timeunit.seconds)// Specify the time interval and the unit of time.subscribe (new Consumer<Long>() {@override public void accept(Long aLong) throws Exception { Log.i(TAG, "accept: "+aLong); }});Copy the code

The results for

01-25 14:48:08.920 16172-16199/com.xp.note. rxJava I/RxJavaActivity30: onNext: 31 01-25 14:48:11.919 16172-16199/com.xp.note. RxJavaActivity30: onNext: 32 01-25 14:48:14.920 16172-16199/com.xp.note. rxJava I/RxJavaActivity30: onNext: 33Copy the code

2.range

Creates an Observable that emits a sequence of integers in a specified range, similar to a for loop, with ranges closed and ranges open.

Observable.range (0,3). Subscribe (new Consumer<Integer>() {@override public void accept(Integer Integer) throws Exception { Log.i(TAG, "accept: "+integer); }});Copy the code

The results for

01-25 14:55:39.341 16711-16711/com.xp.note. rxJava I/RxJavaActivity30: Accept: 0 01-25 14:55:40.341 16711-16711/com.xp.note. rxJava I/RxJavaActivity30: Accept: 1 01-25 14:55:41.343 16711-16711/com.xp.note. rxJava I/RxJavaActivity30: Accept: 2Copy the code

3.repeat

Create an Observable that emits repeated data n times

Observable.range (0,2).repeat(2).subscribe(new Consumer<Integer>() {@override public void accept(Integer) integer) throws Exception { Log.i(TAG, "accept: "+integer); }});Copy the code

The output

01-25 15:03:50.446 17017-17017/com.xp.note. rxJava I/RxJavaActivity30: Accept: 0 01-25 15:03:50.446 17017-17017/com.xp.note. rxJava I/RxJavaActivity30: Accept: 1 01-25 15:03:50.446 17017-17017/com.xp.note. rxJava I/RxJavaActivity30: Accept: 0 01-25 15:03:50.446 17017-17017/com.xp.note. rxJava I/RxJavaActivity30: Accept: 1Copy the code

Transformation operator

The transform operator performs some transformation operations on the data emitted by an Observable according to certain rules, and then transmits the transformed data.

1.map

The Map operator converts an Observable to a new Observable by specifying a Function object and then emits it. The observer takes over the new Observable and processes it.

Observables. Range (0, 5). The map (new Function < Integer, String>() {@override public String apply(Integer Integer) throws Exception {return "After conversion: "+ Integer; } }).subscribe(new Consumer<String>() { @Override public void accept(String s) throws Exception { Log.i("TAG", "accept: "+s); }});Copy the code

The output

01-29 21:08:25.157 17201-17201/com.xp.note.rxjava I/TAG: Accept: 0 01-29 21:08:25.158 17201-17201/com.xp.note. Rxjava I/TAG: Accept: 1 01-29 21:08:25.158 17201-17201/com.xp.note. Rxjava I/TAG: Accept: 17201-17201/com.xp.note. Rxjava I/TAG: Accept: 17201-17201/com.xp.note. Rxjava I/TAG: accept: 4Copy the code

2.flatMap(concatMap)

flatMapWill aObservableConvert to multipleObservablesAnd then put their launch data into a separateObservable

 Observable.range(0, 5)
    .flatMap(new Function<Integer, ObservableSource<String>>() {
        @Override
        public ObservableSource<String> apply(Integer integer) throws Exception {
            return Observable.fromArray(integer + "");
        }
    }).subscribe(new Consumer<String>() {
        @Override
        public void accept(String s) throws Exception {
            Log.i("TAG", "accept: " + s);
        }
    });
Copy the code

The functions of concatMap and flatMap are the same. Transform one Observables emitting data into multiple Observables, and then put their emitting data into a single Observable. In the end, however, Observables are merged. FlatMap uses merge, while concatMap uses concat. In a word, the difference between them lies in that concatMap is ordered while flatMap is unordered. The final output order of concatMap remains the same as the original sequence, while flatMap is not necessarily staggered.

Results output

01-29 22:43:34.796 29345-29469/com.xp.note. Rxjava I/TAG: Accept: 0 01-29 22:43:34.796 29345-29469/com.xp.note. Rxjava I/TAG: Accept: 1 01-29 22:43:34.796 29345-29469/com.xp.note. Rxjava I/TAG: Accept: 796 29345-29469/com.xp.note. Rxjava I/TAG: Accept: 3 01-29 22:43:34.796 29345-29469/com.xp.note. Rxjava I/TAG: Accept :4Copy the code

Map is suitable for one-to-one conversion and can also be used with flatMap

Flatmap is suitable for one-to-many, many-to-many scenarios

Flatmap can be used to resolve circular nesting, and another scenario is to request two interfaces in a row, where the return value of the first interface is the request parameter of the second interface. In this case, we used to request another interface after the completion of one request by obtaining the result in onResponse. This kind of interface nesting, code looks very ugly, using flatMap can be a good solution to this problem. The code looks elegant and logical. If you need to ensure order, use concatMap

More operators

3.buffer

The buffer transforms the original Observable into a new Observable that emits list values one at a time, rather than one at a time.

Observable. Range (0, 7).buffer(3). Subscribe (new Consumer<List<Integer>>() {// The argument is List instead of Integer, @override public void accept(List<Integer> integers) throws Exception {log. I ("TAG", "accept: " + integers.size()); }});Copy the code

Results output

04-19 22:00:12.901 11686-11686/com.xp.note. Rxjava I/TAG: Accept: 3 04-19 22:00:12.902 11686-11686/com.xp.note. Rxjava I/TAG: Accept: 3 04-19 22:00:12.902 11686-11686/com.xp.note. Rxjava I/TAG: Accept :1Copy the code

4.groupBy

groupByThe original of theObservableBreak it upObservablesEach of these collections emits a subsequence of the original Observable, which data item is whichObservableThe emission is determined by a function that assigns a Key to each entry, and the same Key will be assigned to the same dataObservableLaunch.

User user1 = new User(" zhang Sanfeng ",100); User user2 = new User(" user2 ",30); User user3 = new User(" zhang Wouji ",18); User user4 = new User(" pearl ",15); User user5 = new User(" zhou Zhiguo ",16); User user6 = new User("小 小 ",15); User user7 = new User(" user7 ",100); Observable<GroupedObservable<Integer,User>> obs = Observable.just(user1, user2, user3, user4, user5, user6, user7) .groupBy(new Function<User, Integer>() { @Override public Integer apply(User user) throws Exception { return user.getAge(); }}); Observable.concat(obs).subscribe(new Consumer<User>() { @Override public void accept(User user) throws Exception { Log.i("TAG", "accept: "+user.toString()); }});Copy the code

Results output

04-19 22:00:12.950 11686-11686/com.xp.note. Rxjava I/TAG: Accept: 04-19 22:00:12.951 11686-11686/com.xp.note. Rxjava I/TAG: Accept: 04-19 22:00:12.953 11686-11686/com.xp.note. Rxjava I/TAG: Accept: 04-19 22:00:12.953 11686-11686/com.xp.note. Rxjava I/TAG: Accept: 04-19 22:00:12.954 11686-11686/com.xp.note. Rxjava I/TAG: Accept: 04-19 22:00:12.954 11686-11686/com.xp.note. Rxjava I/TAG: Accept: 04-19 22:00:12.954 11686-11686/com.xp.note. Rxjava I/TAG: Accept: Zhou Zhiif 16 years oldCopy the code