RxJava operator (8) Aggregate_PHP tutorial

Source: Internet
Author: User
RxJava operator (8) Aggregate. RxJava operator (8) Aggregate in this article, let's take a look at Rxjava's Aggregate operators. 1. the ContactContact operator combines multiple Observable into one Observable and transmits the RxJava operator (8) Aggregate
In this article, let's take a look at Rxjava's aggregate operators.

1. Contact
The Contact operator combines multiple Observable into an Observable and transmits data in strict sequence. The data of the previous Observable is not completely transmitted, and the data of the subsequent Observable cannot be transmitted.
There are two operators similar to it, but there is a difference:
1. startWith: just insert a data in front.
2. merge: the data it launches is out of order.

II. Count
The Count operator is used to Count the number of data transmitted by the source Observable, and finally the number is sent out. if the source Observable fails to launch, the error is reported directly. before the source Observable ends, count does not transmit statistics.

The following uses the Contact and Count operators to combine multiple Observable and calculate the number of transmitted data.

 
  1. private Observable contactObserver() {
  2. Observable obser1 = Observable.just(1, 2, 3);
  3. Observable obser2 = Observable.just(4, 5, 6);
  4. Observable obser3 = Observable.just(7, 8, 9);
  5. return Observable.concat(obser1, obser2, obser3);
  6. }
  7. private Observable conuntObserver() {
  8. return Observable.just(1, 2, 3).count();
  9. }
Subscribe separately

 
  1. mLButton.setText("contact");
  2. mLButton.setOnClickListener(e -> contactObserver().subscribe(i -> log("contact:" + i)));
  3. mRButton.setText("conunt");
  4. mRButton.setOnClickListener(e -> conuntObserver().subscribe(i -> log("conunt:" + i)));
The running result is as follows:


III. Reduce and Collect
The Reduce operator applies a function to receive data transmitted by the Observable and the calculation result of the function as the parameter for the next calculation, and outputs the final result. Similar to the scan operator we have learned earlier, the scan operator only outputs the result of each calculation, while the reduce operator only outputs the final result.
The Collect operator is similar to Reduce, but for different purposes, collect is used to Collect the data transmitted by the source Observable into a data structure. two parameters are required:
1. a function that generates the data collection structure.
2. a function that receives the data structure generated by the first function and the data transmitted by the source Observable as a parameter.


Next we will use the reduce and collect operators. As in the original scan example, we used a list containing 10 digits 2 to create the source Observable.

 
  1. private Observable reduceObserver() {
  2. return Observable.from(list).reduce((x, y) -> x * y);
  3. }
  4. private Observable> collectObserver() {
  5. return Observable.from(list).collect(() -> new ArrayList<>(), (integers, integer) -> integers.add(integer));
  6. }
Subscribe separately

 
  1. mLButton.setText("reduce");
  2. mLButton.setOnClickListener(e -> reduceObserver().subscribe(i -> log("reduce:" + i)));
  3. mRButton.setText("collect");
  4. mRButton.setOnClickListener(e -> collectObserver().subscribe(i -> log("collect:" + i)));
The running result is as follows:


Aggregation Operator is relatively small, it is here, the source code in this article see https://github.com/Chaoba/RxJavaDemo

Aggregate (8) Aggregate in this article, let's take a look at Rxjava's Aggregate operators. 1. the Contact operator combines multiple Observable into an Observable and transmits the object...

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.