This article introduces operators for arithmetic and aggregation. The average, min, Max, and sum operators do not exist in the RxJava2 Math library. They are not used in the RxJava2 Math library. This section describes the common operators ~.

The count operator

Count the number of events emitted by an Observable.

  • Calculate whether can load more, for example, judge condition here is that the back-end returns the number of data, usually we ask how much return, unless there are not enough, return not enough, so you can traverse the back-end data returned by the collection, called count operator, the number is greater than the specified number is judged to be can load more.
Observable. Just (modelList) // First iterates through multiple interfaces. FlatMap (new Function<List<HttpModel<? >>, ObservableSource<HttpModel<? >>>() { @Override public ObservableSource<HttpModel<? >> apply(List<HttpModel<? >> httpModels) throws Exception {returnObservable.fromIterable(httpModels); Map (new Function<HttpModel<? >, Object>() { @Override public Object apply(HttpModel<? > model) throws Exception {returnmodel.getData(); Filter (new Predicate<Object>() {@override public Booleantest(Object target) throws Exception {
                returntarget instanceof RecommendFastTestServerModel; }}) // Count the number.count () // greater than the number of requests, Map (new Function<Long, Boolean>() {@override public Boolean apply(Long count) throws Exception {returncount >= ApiUrl.PER_PAGE; }});Copy the code

Concat operator

Emission of two or more Observables without interlacing. The idea is to combine data sent from multiple sources in sequence, so they are concatenated, but not aggregated and returned together! You need to use the toList or Collect operator for aggregation.

  • For example, if a list needs to be returned by two interfaces together, the concAT operator can be used in conjunction with the COLLECT operator.
Request teacher details and teacher services, And at the same time return observables. Concat (getViewModel () getUserDetail (tag, uid), UCRequestManager. GetTeacherService (context, tag, Collect (new Callable<ArrayList<HttpModel<? extends Serializable>>>() { @Override public ArrayList<HttpModel<? extends Serializable>> call() throws Exception {returnnew ArrayList<>(); } }, new BiConsumer<ArrayList<HttpModel<? extends Serializable>>, HttpModel<? extends Serializable>>() { @Override public void accept(ArrayList<HttpModel<? extends Serializable>> modelList, HttpModel<? extends Serializable> httpModel) throws Exception { modelList.add(httpModel); } }).subscribe(new Consumer<ArrayList<HttpModel<? extends Serializable>>>() { @Override public void accept(ArrayList<HttpModel<? extends Serializable>> httpModels) throws Exception { //... } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { //... }}))Copy the code

Reduce operator

  • The reduce operator is used to calculate cumulative results. The reduce operator aggregates all the events emitted by a data source, calls back BiFunction several times, and the results of the previous one are passed in and finally uniformly output to the observer’s onNext

Observable.just(1, 2, 3, 4, 5)
        .reduce(new BiFunction<Integer, Integer, Integer>() {
            @Override
            public Integer apply(Integer value, Integer value2) throws Exception {
                returnvalue + value2; Subscribe (new Consumer<Integer>() {@override public void accept(Integer result) throws Exception {// Final result 15... }});Copy the code