Create operator and transform operator in RxJava2

Get into the habit of writing together! This is the second day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

create operator

1.interval

Creates a sequence of integers that emits at regular intervals Observable, equivalent to a timer.

Observable
    .interval(3, TimeUnit.SECONDS)//指定时间间隔以及时间的单位
    .subscribe(new Consumer<Long>() {
        @Override
        public void accept(Long aLong) throws Exception {
            Log.i(TAG, "accept: "+aLong);
        }
    });
复制代码

The result is

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.rxjava I/RxJavaActivity30: onNext: 32
01-25 14:48:14.920 16172-16199/com.xp.note.rxjava I/RxJavaActivity30: onNext: 33
复制代码

2.range

Creates a sequence of integers that emits a specified range Observable, similar to a for loop, with the value range closed on the left and open on the right.

Observable
    .range(0,3)
    .subscribe(new Consumer<Integer>() {
        @Override
        public void accept(Integer integer) throws Exception {
            Log.i(TAG, "accept: "+integer);
        }
    });
复制代码

The result is

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: 2
复制代码

3.repeat

Create an n-emission repeat of the dataObservable

Observable
    .range(0,2)
    .repeat(2)//重复次数
    .subscribe(new Consumer<Integer>() {
        @Override
        public void accept(Integer integer) throws Exception {
            Log.i(TAG, "accept: "+integer);
        }
    });
复制代码

output result

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: 1
复制代码

transformation operator

The function of the transformation operator is to Observableperform some transformation operations on the transmitted data according to certain rules, and then transmit the transformed data.

1.map

The map operator works by specifying an Functionobject that will be Observableconverted to a new Observableobject and emitted, and the observer will take over the new one Observableand process it.

Observable.range(0,5).map(new Function<Integer, String>() {
    @Override
    public String apply(Integer integer) throws Exception {
        return "转换之后:"+integer;
    }
	}).subscribe(new Consumer<String>() {
	    @Override
	    public void accept(String s) throws Exception {
	        Log.i("TAG", "accept: "+s);
	    }
	});
复制代码

output result

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: 转换之后:2
01-29 21:08:25.158 17201-17201/com.xp.note.rxjava I/TAG: accept: 转换之后:3
01-29 21:08:25.158 17201-17201/com.xp.note.rxjava I/TAG: accept: 转换之后:4

复制代码

2.flatMap(concatMap)

flatMapObservableTransform one into many Observables, then put the data they emit into a singleObservable 1167421-7728bea372bd612c.png

 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);
        }
    });
复制代码

concatMap and flatMap have the same function, transform an Observable that emits data into multiple Observables, and then put the data they emit into a single Observable. It's just that the Observables are merged in the end, the merge used by flatMap, and the connection used by concatMap (concat). In a word, the difference between them is: concatMap is ordered, flatMap is unordered, the final output order of concatMap is consistent with the original sequence, while flatMap is not necessarily, there may be interleaving.

result 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: 2
01-29 22:43:34.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: 4
复制代码

map is suitable for one-to-one conversion, of course, it can also be used with flatmap

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

Flatmap can be used to resolve loop nesting. Another scenario is to request two interfaces in succession. The return value of the first interface is the request parameter of the second interface. Get the result in onResponse and then request another interface. This kind of interface is nested, and the code looks very ugly. Using flatMap can solve this problem very well. The code looks very elegant and logically clear. If you need to guarantee the order, use concatMap

more operators

3.buffer

bufferConverts the original Observable to a new one Observablethat Observableemits a list of values ​​each time instead of one by one.

aaa8dc.jpg

Observable.range(0, 7)
            .buffer(3)
            .subscribe(new Consumer<List<Integer>>() {//参数是List而不是Integer,说明发射了一组
                @Override
                public void accept(List<Integer> integers) throws Exception {
                    Log.i("TAG", "accept: " + integers.size());
                }
            });
复制代码

result 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: 1
复制代码

4.groupBy

groupBySplit the original Observableinto Observablessets, each of which emits a subsequence of the original Observable, and which data item is Observableemitted by which is determined by a function that assigns each item a Key, the same Key data will be Observableemitted by the same one .iamge

    User user1 = new User("张三丰",100);
    User user2 = new User("张翠山",30);
    User user3 = new User("张无极",18);
    User user4 = new User("珠儿",15);
    User user5 = new User("周芷若",16);
    User user6 = new User("小昭",15);
    User user7 = new User("白眉鹰王",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());
        }
    });
复制代码

result output

04-19 22:00:12.950 11686-11686/com.xp.note.rxjava I/TAG: accept: 张三丰 100岁
04-19 22:00:12.951 11686-11686/com.xp.note.rxjava I/TAG: accept: 白眉鹰王 100岁
04-19 22:00:12.953 11686-11686/com.xp.note.rxjava I/TAG: accept: 张翠山 30岁
04-19 22:00:12.953 11686-11686/com.xp.note.rxjava I/TAG: accept: 张无极 18岁
04-19 22:00:12.954 11686-11686/com.xp.note.rxjava I/TAG: accept: 珠儿 15岁
04-19 22:00:12.954 11686-11686/com.xp.note.rxjava I/TAG: accept: 小昭 15岁
04-19 22:00:12.954 11686-11686/com.xp.note.rxjava I/TAG: accept: 周芷若 16岁

复制代码

Guess you like

Origin juejin.im/post/7082344250666385421