Rxjava operator (ii) Transforming observables_php Tutorial

Source: Internet
Author: User
Tags emit

Rxjava operator (ii) transforming observables


In the previous article, we learned how to create a observable, and just creating a observable might not be enough to meet some complex scenarios, so we would probably need to convert the created observable install some sort of rule to launch the data. In this article we will look at how to convert observable

First, Buffer
As the name implies, what the buffer operator does is to cache the data as it is installed, and then launch the cached data as a collection. As shown in the first example, we specify a buffer size of 3, collect 3 data and then launch it, and in the second picture we add a skip parameter to specify how many data should be skipped each time a collection is emitted, how to specify a count of 2,skip as 3, Will emit a collection of two data per 3 data, and if count==skip, we will find that it is equivalent to the first case.


Buffer can not only be cached by the number rules, but also through the rules of time and other caching, such as the provision of 3 seconds cache launch once, see the code below, we created two observable, and use buffer to convert it, the first one through the number of cache, the second through the time to cache.

 
  
  
  1. Private Observable <> > Bufferobserver () {
  2. Return Observable.just (1, 2, 3, 4, 5, 6, 7, 8, 9). Buffer (2, 3);
  3. }

  4. Private Observable <> > 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 result is as follows, you can see that the first observable will emit the top two digits every 3 digits, and the second observable will output a few digits every 3 seconds.


Second, FlatMap
Flatmap is a useful operator that can convert data to the rules you want and then launch it. The principle is to convert the observable into a number of data emitted from the original observable as the source data observable, and then the multiple observable launched data integration launched, it is important to note that the final sequence may be staggered to launch out, You can use the CONCATMAP operator if you have strict requirements for the order. Flatmapiterable and Flatmap are the same, and many of the observable for which they are transformed are using iterable as the source data.


Below we create and convert two observable using Flatmap and flatmapiterable respectively.

 
  
  
  1. Private Observable 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 flatmapiterableobserver () {
  5. Return Observable.just (1, 2, 3, 4, 5, 6, 7, 8, 9)
  6. . flatmapiterable (
  7. Integer, {
  8. ArrayList s = new arraylist<> ();
  9. for (int i = 0; i < integer; i++) {
  10. S.add (integer);
  11. }
  12. return s;
  13. }
  14. );
  15. }
Subscribe to it 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 result of the run is as follows, the first operator adds a string prefix to the flat map for the emitted data, and the second expands the data to output n n numbers.



Third, GroupBy
The groupby operator splits the data emitted by the original observable into small observable by key, and then these small observable emit the data they contain, similar to the GroupBy in SQL.
In use, we need to provide a rule that generates a key, and all keys with the same data will be included in the same small observable species. In addition, we can provide a function to convert the data, a bit similar to the integration of Flatmap.

The following creates two GroupBy converted observable objects, first grouped by odd even number, followed by a string prefix after the second grouping

 
 
  1. Mlbutton.settext ("GroupBy");
  2. Mlbutton.setonclicklistener (E-groupbyobserver (). Subscribe (new Subscriber <> > () {
  3. @Override
  4. public void oncompleted () {

  5. }

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

  8. }

  9. @Override
  10. public void OnNext (groupedobservable groupedobservable) {
  11. Groupedobservable.count (). Subscribe (integer---Log ("key" + groupedobservable.getkey () + "contains:" + Integer + "numb ERs "));
  12. }
  13. }));
  14. Mrbutton.settext ("Groupbykeyvalue");
  15. Mrbutton.setonclicklistener (E-groupbykeyvalueobserver (). Subscribe (new Subscriber <> > () {
  16. @Override
  17. public void oncompleted () {

  18. }

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

  21. }

  22. @Override
  23. public void OnNext (groupedobservable integerintegergroupedobservable) {
  24. if (integerintegergroupedobservable.getkey () = = 0) {
  25. Integerintegergroupedobservable.subscribe (integer---log (integer));
  26. }
  27. }
  28. }));
  29. }

The result of the operation is as follows, we get the desired result.



Iv. Map, Cast
The map operator functions like Flatmap, except that the conversion of the data is done directly, and flatmap needs to be done through some intermediate observables.

Cast converts the data emitted by observable into another type, a specific implementation of map

Below we create two map and cast converted observable objects

 
 
  1. Private Observable Mapobserver () {
  2. Return Observable.just (1, 2, 3, 4, 5, 6, 7, 8, 9). Map (integer-and integer * 10);
  3. }

  4. Private Observable 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 ("Cast:" + i.getname ()));
After running, the results are as follows. As you can see, the map operator multiplies the data by 10 and then emits it, and the cast operator forces the object of the animal type into the object of the dog type. In addition, we can also verify that a knowledge point, in case of inheritance, the creation of the object will first call the parent class construction method Oh.


V. Scan
The scan operator applies a function to a sequence of data and emits the result of the function as the first argument to apply the function as the next data, somewhat like a recursive operation

Below we create a observable object with a list of 10 2 and convert it using scan, and the function of conversion is to multiply the result by one of the following numbers.

 
  
  
  1. Private Observable 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 (log ("Scan:" + i));
The results are as follows, and we can see that we have output 2 of the n-th square.


VI. Window
The window operator is similar to the buffer we talked about earlier, except that the window emits small observable objects that emit internally contained data from these small observable objects. As with buffer, window can be grouped by numbers and by rules such as time.

Below we create two observable objects that are grouped using the number and time rules of the window, respectively.

 
  
  
  1. Private Observable <> > Windowcountobserver () {
  2. Return Observable.just (1, 2, 3, 4, 5, 6, 7, 8, 9). window (3);
  3. }

  4. Private Observable <> > Wondowtimeobserver () {
  5. Return Observable.interval (Timeunit.milliseconds)
  6. . Window (Timeunit.milliseconds)
  7. . Observeon (Androidschedulers.mainthread ());
  8. }
Subscribe to it 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 result of the operation is as follows, you can see that the first observable object has not emitted a small observable of 3 data, and the second observable object emits a observable object containing up to four data every 3 seconds.


The transforming operator is an important embodiment of Rxjava's power, and it is essential to use Rxjava to master the transforming operator flexibly.

This demo program is shown in Github:https://github.com/chaoba/rxjavademo



http://www.bkjia.com/PHPjc/1077811.html www.bkjia.com true http://www.bkjia.com/PHPjc/1077811.html techarticle Rxjava operator (ii) transforming observables in the previous article, we learned how to create a observable, just create a observable that might not meet some complex scenarios ...

  • 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.