RxJava operator (2) Transforming Observables

Source: Internet
Author: User

RxJava operator (2) Transforming Observables
In the previous article, we learned how to create an Observable. Creating only one Observable may not meet complex scenarios, therefore, we may need to convert the created Observable installation rules to launch data. In this article, let's take a look at how to convert Observable

I. Buffer
As the name suggests, what the Buffer operator needs to do is to cache the specified size of Data installation, and then transmit the cached data as a set. As shown in, in the first example, we specify the buffer size as 3. After three pieces of data are collected, the data is transmitted, in the second figure, we add a skip parameter to specify how many data needs to be skipped each time a set is initiated. In the figure, how can we specify count to 2 and skip to 3, A set containing two data is sent for each three data items. If count = skip, we will find that it is equivalent to the first case.


Buffer can be cached not only by quantity rules, but also by time and other rules, such as setting a three-second cache to be released once. See the following code. We have created two Observable, and use the buffer to convert it. The first one is cached by quantity, and the second one is cached by time.

 
 
  1. private Observable<List<Integer>> bufferObserver() {
  2. return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).buffer(2, 3);
  3. }

  4. private Observable<List<Long>> bufferTimeObserver() {
  5. return Observable.interval(1, TimeUnit.SECONDS).buffer(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread());
  6. }
Subscribe to it

 
 
  1. mLButton.setText("buffer");
  2. mLButton.setOnClickListener(e -> bufferObserver().subscribe(i -> log("buffer:" + i)));
  3. mRButton.setText("bufferTime");
  4. mRButton.setOnClickListener(e -> bufferTimeObserver().subscribe(i -> log("bufferTime:" + i)));

The running result is as follows. We can see that the first Observable emits the first two digits every three digits. The second Observable outputs two to three seconds ~ 4 digits.


Ii. FlatMap
FlatMap is a useful operator that converts data according to the rules you want before transmitting it. The principle is to convert the Observable into multiple Observable data transmitted from the original Observable as the source data, and then integrate and launch the data transmitted from the multiple Observable, note that the final sequence may be transmitted out in staggered ways. If there are strict requirements on the sequence, you can use the concatmap operator. The FlatMapIterable and FlatMap bases are the same. In contrast, Iterable is used as the source data for multiple converted Observable objects.


Next we will use FlatMap and FlatMapIterable to create and convert two Observable.

 
 
  1. private Observable<String> flatMapObserver() {
  2. return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).flatMap(integer -> Observable.just("flat map:" + integer));
  3. }

  4. private Observable<? extends Integer> flatMapIterableObserver() {
  5. return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9)
  6. .flatMapIterable(
  7. integer -> {
  8. ArrayList<Integer> s = new ArrayList<>();
  9. for (int i = 0; i < integer; i++) {
  10. s.add(integer);
  11. }
  12. return s;
  13. }
  14. );
  15. }
Subscribe to them separately

 
 
  1. mLButton.setText("flatMap");
  2. mLButton.setOnClickListener(e -> flatMapObserver().subscribe(i -> log(i)));
  3. mRButton.setText("flatMapIterable");
  4. mRButton.setOnClickListener(e -> flatMapIterableObserver().subscribe(i -> log("flatMapIterable:" + i)));

The running result is as follows. The first operator adds the imported data with a flat map string prefix. The second operator expands the data and outputs n numbers.



Iii. GroupBy
The GroupBy operator splits the data transmitted by the original Observable into some small Observable according to the key, and then these small Observable separately transmits the data they contain, similar to the groupBy in SQL.
In use, we need to provide a key generation rule. All data with the same key will be contained in the same small Observable type. In addition, we can provide a function to convert the data, which is a bit similar to integrating flatMap.

Next, we will create two Observable objects converted by groupBy. The first one is grouped by an odd and even number, and the second one is grouped by a string prefix.

 
 
  1. mLButton.setText("groupBy");
  2. mLButton.setOnClickListener(e -> groupByObserver().subscribe(new Subscriber<GroupedObservable<Integer, Integer>>() {
  3. @Override
  4. public void onCompleted() {

  5. }

  6. @Override
  7. public void onError(Throwable e) {

  8. }

  9. @Override
  10. public void onNext(GroupedObservable<Integer, Integer> groupedObservable) {
  11. groupedObservable.count().subscribe(integer -> log("key" + groupedObservable.getKey() + " contains:" + integer + " numbers"));
  12. }
  13. }));
  14. mRButton.setText("groupByKeyValue");
  15. mRButton.setOnClickListener(e -> groupByKeyValueObserver().subscribe(new Subscriber<GroupedObservable<Integer, String>>() {
  16. @Override
  17. public void onCompleted() {

  18. }

  19. @Override
  20. public void onError(Throwable e) {

  21. }

  22. @Override
  23. public void onNext(GroupedObservable<Integer, String> integerIntegerGroupedObservable) {
  24. if (integerIntegerGroupedObservable.getKey() == 0) {
  25. integerIntegerGroupedObservable.subscribe(integer -> log(integer));
  26. }
  27. }
  28. }));
  29. }

The running result is as follows.



Iv. Map and Cast
The function of the Map operator is similar to FlatMap. The difference is that it directly converts data, and FlatMap needs to be implemented through some intermediate Observables.

Cast forcibly converts the data transmitted by Observable to another type, which is a specific implementation of Map.

Next we will create two Observable objects converted by map and cast.

 
 
  1. private Observable<Integer> mapObserver() {
  2. return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).map(integer -> integer * 10);
  3. }

  4. private Observable<Dog> castObserver() {
  5. return Observable.just(getAnimal())
  6. .cast(Dog.class);
  7. }

  8. Animal getAnimal() {
  9. return new Dog();
  10. }

  11. class Animal {
  12. protected String name = "Animal";

  13. Animal() {
  14. log("create " + name);
  15. }

  16. String getName() {
  17. return name;
  18. }
  19. }

  20. class Dog extends Animal {
  21. Dog() {
  22. name = getClass().getSimpleName();
  23. log("create " + name);
  24. }

  25. }
Register it

 
 
  1. mLButton.setText("Map");
  2. mLButton.setOnClickListener(e -> mapObserver().subscribe(i -> log("Map:" + i)));
  3. mRButton.setText("Cast");
  4. mRButton.setOnClickListener(e -> castObserver().subscribe(i -> log("Cast:" + i.getName())));
The result is as follows. As you can see, the map operator times the data by 10 and then transmits the data. The cast operator forcibly converts an Animal type object to a Dog type object. In addition, we can also verify the next knowledge point. When an object is created with inheritance, the constructor of the parent class will be called first.


5. Scan
The Scan operator applies a function to the data of a sequence, and transmits the result of this function as the first parameter for the next data application. It is somewhat similar to recursive operations.

Next we will create an Observable object through a list containing 10 2 objects and convert it using scan. The conversion function is to multiply the calculation result by the next number.

 
 
  1. private Observable<Integer> scanObserver() {
  2. return Observable.from(list).scan((x, y) -> x * y).observeOn(AndroidSchedulers.mainThread());
  3. }
Subscribe to it

 
 
  1. mLButton.setText("scan");
  2. mLButton.setOnClickListener(e -> scanObserver().subscribe(i -> log("scan:" + i)));
The result is as follows. We can see that the Npower of 2 is output.


Vi. Window
The Window operator is similar to the buffer we mentioned earlier. The difference is that window emits some small Observable objects, which are used to transmit internal data. Same as buffer, windows can be grouped not only by quantity, but also by time and other rules.

Next, we create two Observable objects that use the number of windows and time rules for grouping.

 
 
  1. private Observable<Observable<Integer>> windowCountObserver() {
  2. return Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9).window(3);
  3. }

  4. private Observable<Observable<Long>> wondowTimeObserver() {
  5. return Observable.interval(1000, TimeUnit.MILLISECONDS)
  6. .window(3000, TimeUnit.MILLISECONDS)
  7. .observeOn(AndroidSchedulers.mainThread());
  8. }
Subscribe to them separately

 
 
  1. mLButton.setText("window");
  2. mLButton.setOnClickListener(e -> windowCountObserver().subscribe(i -> {
  3. log(i);
  4. i.subscribe((j -> log("window:" + j)));
  5. }));
  6. mRButton.setText("Time");
  7. mRButton.setOnClickListener(e -> wondowTimeObserver().subscribe(i -> {
  8. log(i);
  9. i.observeOn(AndroidSchedulers.mainThread()).subscribe((j -> log("wondowTime:" + j)));
  10. }));
The running result is as follows. We can see that the first Observable object never emits a small Observable containing three data, and the second Observable object emits a 2 ~ object every 3 seconds ~ Observable object of four data items


The Transforming operator is an important embodiment of Rxjava's power. It is essential to use Rxjava flexibly to master the Transforming operator.

The demo program of this article see github: https://github.com/Chaoba/RxJavaDemo



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.