rxjava-Operation appended Conversion observable

Source: Internet
Author: User
Tags iterable throwable


In the filter observables of the rxjava-operator, it is understood that the basic use of the RxJava filter operator is mainly for the observable list. This article focuses on the use of object operators in the observable list. Used to transform observable sequences to create a sequence that better meets our needs.

MapThe map operator, by specifying a Func object, converts observables to a new observable object and launches, and the observer receives the new observable processing.
The flowchart of the map operator is as follows:

Example code:
    Observable.from (mlists)        . Map (new func1<student, student> () {            @Override public            Student Call ( Student Student) {                StringBuilder sb = new StringBuilder ();                String age = Student.getage ();                Sb.append ("Age-");                Sb.append (age);                Student.setage (Sb.tostring ());                return student;            }        })        . Subscribe (new observer<student> () {            @Override public            void oncompleted () {            }            @Override Public            void OnError (Throwable e) {            }            @Override public            void OnNext (Student Student) {                Madastu.adddata (student);            }        });

As you can see, after creating the observable we launched, we append a map call, and in its callback calls method, add the Age field "age-", update the object and launch it, and the observer receives the updated observable.
FlatMapThe flatmap operator is used to emit a sequence of data that, at the same time, has a transmit observable. Flatmap is in a flattened sequence, then merges the data emitted by these observables and finally takes the combined result as the final observable. However, FlatMap () may stagger the sending of events, and the order of the final results may be and is not the order in which the original observable is sent.
The flowchart for the FLATMAP operator is as follows:

Example code:
      private void Doflatmap () {list<string> lists = new arraylist<> ();        Lists.add ("Flat-a");        Lists.add ("Flat-b");        Lists.add ("Flat-c");        Lists.add ("flat-d"); Observable.from (lists). FLATMAP (New func1<string, observable<student>> () {@ Override Public observable<student> Call (String s) {return getstudentdata                    (s);                    }}). Subscribe (New observer<student> () {@Override public void oncompleted () {} @Override public void OnError (T                        Hrowable e) {} @Override public void OnNext (Student Student) {                    Madastu.adddata (student);    }                }); }//For different s to launch different Observable sequence private observable<        Student> Getstudentdata (final String s) {list<student> lists_0 = new arraylist<> (); if (Textutils.equals ("Flat-a", s) | |            Textutils.equals ("Concat-a", s)) {Lists_0.add (new Student (S + "11", "20", "1101"));            Lists_0.add (new Student (S + "12", "20", "1102"));        Lists_0.add (new Student (S + "13", "20", "1103")); } else if (Textutils.equals ("Flat-b", s) | |        Textutils.equals ("Concat-b", s)) {Lists_0.add (new Student (S + "11", "20", "1101")); } else if (Textutils.equals ("Flat-c", s) | |            Textutils.equals ("Concat-c", s)) {Lists_0.add (new Student (S + "11", "20", "1101"));        Lists_0.add (new Student (S + "12", "20", "1102")); } else if (Textutils.equals ("flat-d", s) | |            Textutils.equals ("concat-d", s)) {Lists_0.add (new Student (S + "11", "20", "1101"));            Lists_0.add (new Student (S + "12", "20", "1102")); Lists_0.add (new Student (S + "13", "20","1103"));        Lists_0.add (new Student (S + "13", "20", "1104"));    } return Observable.from (LISTS_0); }


Note:
1.flatMap allows cross. As the flowchart shows, this means that flatmap () cannot guarantee the exact launch order of the source observables in the resulting observable.
2. When we are dealing with potentially large amounts of observables, it is important to remember that any observables error occurs, and FLATMAP () will trigger its own onerror () function and discard the entire chain.
ConcatmapThe concatmap operator function is consistent with the FLATMAP operator, but it solves the crossover problem of the Flatmap (), providing a way to keep the values of the emission together.
Flattening the functions, not merging them,
The flowchart for the CONCATMAP operator is as follows:

Example code:
    list<string> lists = new arraylist<> ();    Lists.add ("Concat-a");    Lists.add ("Concat-b");    Lists.add ("Concat-c");    Lists.add ("concat-d");    Observable.from (lists)            . Concatmap (New func1<string, observable<student>> () {                @Override                Public observable<student> Call (String s) {                    return getstudentdata (s);                }            })            . Subscribe (new observer<student> () {                @Override public                void oncompleted () {                }                @Override Public                void OnError (Throwable e) {                }                @Override public                void OnNext (Student Student) {                    Madastu.adddata (student);                }            });



flatmapiterableThe flatmapiterable operator is similar to Flatmap, and the only difference is that it pairs the source data 22 and generates iterable instead of the original data item and the generated observables.
The flowchart for the flatmapiterable operator is as follows:

Example code:
     private void Doflatmapiterable () {list<string> lists = new arraylist<> ();        Lists.add ("flatmapiterable-a");        Lists.add ("Flatmapiterable-b");        Lists.add ("Flatmapiterable-c");        Lists.add ("flatmapiterable-d");                    Observable.from (lists). flatmapiterable (New func1<string, iterable<student>> () { @Override Public iterable<student> Call (String s) {return getstudent                    List (s);                    }}). Subscribe (New observer<student> () {@Override public void oncompleted () {} @Override public void OnError (T                        Hrowable e) {} @Override public void OnNext (Student Student) {                    Madastu.adddata (student);    }                }); }    Different observable sequences for different s are emitted private arraylist<student> getstudentlist (final String s) {arraylist<st        udent> lists_0 = new arraylist<> ();            if (Textutils.equals ("flatmapiterable-a", s)) {Lists_0.add (new Student (S + "11", "20", "1101"));            Lists_0.add (new Student (S + "12", "20", "1102"));        Lists_0.add (new Student (S + "13", "20", "1103"));        } else if (Textutils.equals ("Flatmapiterable-b", s)) {Lists_0.add (new Student (S + "11", "20", "1101"));            } else if (Textutils.equals ("Flatmapiterable-c", s)) {Lists_0.add (new Student (S + "11", "20", "1101"));        Lists_0.add (new Student (S + "11", "20", "1102"));            } else if (Textutils.equals ("flatmapiterable-d", s)) {Lists_0.add (new Student (S + "11", "20", "1101"));            Lists_0.add (new Student (S + "12", "20", "1102"));            Lists_0.add (new Student (S + "13", "20", "1103")); Lists_0.add (New Student (S + "13", "20", "1104"));    } return lists_0; }


SwitchmapThe switchmap operator, Flatmap, is a bit different, and whenever the source Observable launches a new data item (Observable), it cancels the subscription and stops monitoring the Observable generated by that data item, and began to monitor the current launch of this one.

The flowchart for the SWITCHMAP operator is as follows:


ScanThe scan operator can be thought of as a sum function, and the scan () function applies a function to each item of data emitted by the original observable, calculates the result value of the function, and populates that value back into the observable sequence, waiting to be used with the next emitted data.
The scan operator has the following flowchart:

Example code:
    Observable.just (1,2,3,4,5)        . Scan ((sum,item), Sum + Item)        . Subscribe (New subscriber<integer> () {            @Override public            void oncompleted () {                log.d ("RXJAVA", "Sequence completed.");            @Override public            void OnError (Throwable e) {                log.e ("RXJAVA", "Something went south!");            }            @Override public            void OnNext (Integer item) {                LOG.D ("RXJAVA", "Item is:" + Item);            }        );

Goupby

The goupby operator is used for grouping elements, transforming the source observable into a new observable (after grouping) that emits observables. Each of these new observable launches a set of specified data.

The flowchart for the GOUPBY operator is as follows:


Example code:
    observable<groupedobservable<string,student>> Groupeditems = Observable.from (mLists)        . GroupBy (New Func1<student,string> () {            @Override public            String Call (Student Student) {                return student.getage ();            }        });    Observable.concat (Groupeditems)            . Subscribe (New observer<student> () {                @Override public                Void OnCompleted () {                }                @Override public                void OnError (Throwable e) {                }                @Override public                Void OnNext (Student Student) {                    madastu.adddata (Student);                }            });

BufferThe buffer operator transforms the source observable into a new observable, and the new observable launches a set of list values each time instead of one.
The flowchart for the buffer operator is as follows:

Example code:
Specify cout only
    Observable.from (mlists)        . Buffer (2)        . Subscribe (New action1<list<student>> () {            @Override Public            void Call (List<student> List) {                cout++;                LOG.I ("123", "cout =" + cout);                LOG.I ("123", list.tostring ());                Madastu.adddatdlists (list);            }        });    Log print    cout = 1    [Student (Name=a11, age=26, no=1101), Student (Name=a12, age=21, no=1102)]    cout = 2    [ Student (Name=a13, age=20, no=1103), Student (NAME=B11, age=22, no=1201)]    cout = 3    [Student (NAME=B12, age=30, no=1202), Student (Name=b13, age=20, no=1203)]    cout = 4    [Student (Name=s10, age=28, no=1301), Student (name=e10 , age=30, no=1401)]    cout = 5    [Student (NAME=F10, age=26, no=1501)]


Looking at the log above, you can see that the source observable amount is grouped and transformed into a new observable, and the new observable launches a set of list values each time.

set count and Skip


look at the flowchart above, if the skip value is set, the source observable is grouped by length, but the first count of each group is added to the new observable and emitted.
Example code:
    Observable.from (mlists)        . Buffer (2,3)        . Subscribe (New action1<list<student>> () {            @Override Public            void Call (List<student> List) {                cout++;                LOG.I ("123", "cout =" + cout);                LOG.I ("123", list.tostring ());                Madastu.adddatdlists (list);            }        });    Log print    cout = 1    [Student (Name=a11, age=26, no=1101), Student (Name=a12, age=21, no=1102)]    cout = 2    [Student (Name=b11, age=22, no=1201), Student (NAME=B12, age=30, no=1202)]    cout = 3    [Student (Name=s10, age=28, no=1301), Student (Name=e10, age=30, no=1401)]

set count and TimeSpan


If you set a timespan parameter for buffer, you create a observable that emits a list every timespan time period.
windowThe window operator is similar to the buffer operator, but it emits observable instead of a list. Looking at the flowchart below, Winow is caching 3 data items and launching them as a new observable.
The flowchart for the window operator is as follows:

Example code:
just set Count
    Observable.from (mlists). window (2). Subscribe (New action1<observable<student>> () { @Override public void Call (Observable<student> studentobservable) {studentobservable.su                         Bscribe (New action1<student> () {@Override public void call (Student Student) {                    Madastu.adddata (student);            }                });    }        });    Log print i:cout = 1 i:student (name=a11, age=26, no=1101) i:cout = 2 I:student (name=a12, age=21, no=1102)  I:cout = 3 I:student (name=a13, age=20, no=1103) i:cout = 4 I:student (NAME=B11, age=22, no=1201) I:cout = 5 i:student (NAME=B12, age=30, no=1202) i:cout = 6 i:student (name=b13, age=20, no=1203) i:cout = 7 i:s Tudent (Name=s10, age=28, no=1301) I:cout = 8 I:student (name=e10, age=30, no=1401) i:cout = 9 I:student (nam E=F10, age=26, no=1501)


Take a closer look at the top Log,window to cache the source observable by Count and then launch them all.

set count and Skip


    Observable.from (mlists)        . Window (2,3)        . Subscribe (New action1<observable<student>> () {            @ Override public            void Call (Observable<student> studentobservable) {                studentobservable.subscribe (new Action1<student> () {                    @Override public                    void Call (Student Student) {                        madastu.adddata (Student);                    }                });            }        });    I:cout = 1    i:student (name=a11, age=26, no=1101)    i:cout = 2    i:student (Name=a12, age=21, no=1102)    I: cout = 3    i:student (NAME=B11, age=22, no=1201)    i:cout = 4    i:student (NAME=B12, age=30, no=1202)    i:c out = 5    i:student (NAME=S10, age=28, no=1301)    i:cout = 6    i:student (Name=e10, age=30, no=1401)


Take a closer look at the top Log,window the source observable is cached by the skip value, but the new observable only launches the first count one by one in the group.

castThe cast operator is a special version of the map () operator, which converts each item of data in the source observable to a new type, turning it into a different class.

The cast operator is the flowchart of the map operator as follows:


rxjava-Operation appended Conversion observable

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.