Android Rxjava Usage Introduction (v) Rxjava operator

Source: Internet
Author: User

This article continues to describe the following types of operators
-Mathematical and Aggregate Operators (observable mathematical operations and aggregation operators)

First, Concat
The concat operator combines multiple observable into a observable and emits data, and the data is emitted in strict order, and the previous observable data is not emitted, and the data behind the observable is not emitted.
There are two operators similar to it, but there are differences, namely
1.startWith: Just insert a data in front of it.
2.merge: The data emitted is unordered.

Second, Count
The count operator is used to count the amount of data emitted by the source observable, and finally the number is emitted, and if the source observable emits an error, the error is reported directly, and count does not emit statistics until the source observable is terminated.

The following uses the Concat and count operators to combine multiple observable and statistics on the number of transmit data

PrivateObservable<Integer>Concatobserver () {Observable<Integer>Obser1=Observable.Just1,2,3); Observable<Integer>Obser2=Observable.Just4,5,6); Observable<Integer>Obser3=Observable.Just7,8,9);returnObservable.Concat (Obser1, Obser2, Obser3); }PrivateObservable<Integer>Conuntobserver () {returnObservable.Just1,2,3).Count (); }

To subscribe separately

mLButton.setText("concat");mLButton.setOnClickListener(e -> concatObserver().subscribe(i -> log("concat:" + i)));mRButton.setText("conunt");mRButton.setOnClickListener(e -> conuntObserver().subscribe(i -> log("conunt:" + i)));

The operation results are as follows

Three, Reduce, Collect
The reduce operator uses a function to receive the computed results of the observable emitted data and functions as the next calculated parameter, outputting the final result. Similar to the scan operators we've seen before, only scan outputs the results of each calculation, and reduce only outputs the final result.
The Collect operator is similar to reduce, but for different purposes, the collect is used to collect the data emitted by the source observable into a single data structure, which requires two parameters:
1. A function that generates a collection of data structures.
2. A function that receives the data structure generated by the first function and the data emitted by the source observable as a parameter.


Let's use the reduce and collect operators. As with the original scan example, we created the source observable using a list of 10 numbers 2.

private observable<integer> reduceObserver () {
    
     return  observable.from
      (list ). reduce      ((x, y) x * y) ; } private  observable  < arraylist  <integer  >> collectobserver   ()  {return  observable< /span>. from  . collect   (() new  arraylist<> (), (integers, integer)- > Integers.add (integer)) ; }
    

To subscribe separately

mLButton.setText("reduce");mLButton.setOnClickListener(e -> reduceObserver().subscribe(i -> log("reduce:" + i)));mRButton.setText("collect");mRButton.setOnClickListener(e -> collectObserver().subscribe(i -> log("collect:" + i)));

The operation results are as follows

The aggregation operator is relatively small, it is here, the source code in this article see Https://github.com/Chaoba/RxJavaDemo

Android Rxjava Usage Introduction (v) Rxjava operator

Related Article

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.