数学和集合操作符

该页展示执行数学或者其他覆盖Obseravble/Flowable发射的整个序列的操作符。因为这些操作符必须等待源Obseravble/Flowable 完成发射item在他们能构建他们自己的排放之前(且必须经常缓存这些item),这些操作符用在有非常长或者无限的Obseravble/Flowable序列上是非常危险的。

Outline

  • Mathematical Operators

  • averageDouble

  • averageFloat

  • max

  • min

  • sumDouble

  • sumFloat

  • sumInt

  • sumLong

  • Standard Aggregate Operators

  • count

  • reduce

  • reduceWith

  • collect

  • collectInto

  • toList

  • toSortedList

  • toMap

  • toMultimap

数学操作符

这些操作符是RxJava2Extensions 工程的一部分。你必须添加rxjava2-extensions模块作为你工程的依赖。它能够在http://search.maven.org找到。

注意:不像标准的RxJava 聚合器操作符,这些数学操作符返回obseravble和Flowable而不是Single 或者Maybe.

如下例子假定MathObseravble 和MathFlowable是被rxjava2-extensions模块引入的:

import hu.akarnokd.rxjava2.math.MathObservable;

import hu.akarnokd.rxjava2.math.MathFlowable;

averageDouble

Available in:

ReactiveX doumentation: http://reactivex.io/documentation/operators/average.html

计算被一个obseravble发射的平均值,且以double类型发射该平均值。

averageDouble example

Observable numbers = Observable.just(1, 2, 3);
MathObservable.averageDouble(numbers).subscribe((Double avg) -> System.out.println(avg));

// prints 2.0

averageFloat

Available in: Flowable,Observable
ReactiveX doumentation: http://reactivex.io/documentation/operators/average.html

计算被一个obseravble发射的平均值,且以Float类型发射该平均值。

averageFloat example

Observable numbers = Observable.just(1, 2, 3);
MathObservable.averageFloat(numbers).subscribe((Float avg) -> System.out.println(avg));

// prints 2.0

max

Available in:Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/max.html

发射被源Obseravable发射的最大值。一个Comparator能被指定且将被用于比较被Obseravable发射的元素。

max example

Observable numbers = Observable.just(4, 9, 5);
MathObservable.max(numbers).subscribe(System.out::println);

// prints 9

下面的例子指定一个Comparator去寻找源Observable最长的String

final Observable names = Observable.just("Kirk", "Spock", "Chekov", "Sulu");
MathObservable.max(names, Comparator.comparingInt(String::length))
        .subscribe(System.out::println);

// prints Chekov

min

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/min.html

发射被源Observable发射的最小值。一个Comparator能被指定且将被用于比较被Observable发射的元素。

min example

Observable numbers = Observable.just(4, 9, 5);
MathObservable.min(numbers).subscribe(System.out::println);

// prints 4

sumDouble

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/sum.html

添加被Observable发射的Double值且发射这个和

sumDouble example

Observable numbers = Observable.just(1.0, 2.0, 3.0);
MathObservable.sumDouble(numbers).subscribe((Double sum) -> System.out.println(sum));

// prints 6.0

sumFloat

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/sum.html

添加被Observable发射的Float值且发射这个和

sumFloat example

Observable numbers = Observable.just(1.0F, 2.0F, 3.0F);
MathObservable.sumFloat(numbers).subscribe((Float sum) -> System.out.println(sum));

// prints 6.0

sumInt

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/sum.html

添加被Observable发射的Int值且发射这个和

sumInt example

Observable numbers = Observable.range(1, 100);
MathObservable.sumInt(numbers).subscribe((Integer sum) -> System.out.println(sum));

// prints 5050

sumLong

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/sum.html

添加被Observable发射的Long值且发射这个和

sumLong example

Observable numbers = Observable.rangeLong(1L, 100L);
MathObservable.sumLong(numbers).subscribe((Long sum) -> System.out.println(sum));

// prints 5050

标准集合操作符

注意:这些标准的集合操作符返回一个Single或Maybe因为输出item的数量最多只有一个

count

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/count.html

计算被一个Observable发射的item的数量,且以Long型返回这个值

count example

Observable.just(1, 2, 3).count().subscribe(System.out::println);

// prints 3

reduce

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/reduce.html

对每个发射的item应用一个函数,且只发射最终积累的值

reduce example

Observable.range(1, 5)
    .reduce((product, x) -> product * x)
    .subscribe(System.out::println);

// prints 120

reduceWith

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/reduce.html

对每个发射的item应用一个函数,且只发射最终积累的值

reduceWith example

Observable.just(1, 2, 2, 3, 4, 4, 4, 5)
        .reduceWith(TreeSet::new, (set, x) -> {
            set.add(x);
            return set;
        })
        .subscribe(System.out::println);

// prints [1, 2, 3, 4, 5]

collect

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/reduce.html

收集被源Observable发射的item 到一个可变的单一的数据结构且返回一个发射该数据结构的Observable.

collect example

Observable.just("Kirk", "Spock", "Chekov", "Sulu")
        .collect(() -> new StringJoiner(" \uD83D\uDD96 "), StringJoiner::add)
        .map(StringJoiner::toString)
        .subscribe(System.out::println);

// prints Kirk  Spock  Chekov  Sulu

collectInto

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/reduce.html

收集被源Observable发射的item 到一个可变的单一的数据结构且返回一个发射该数据结构的Observable.

collectInto example

Observable.just("Kirk", "Spock", "Chekov", "Sulu")
        .collect(() -> new StringJoiner(" \uD83D\uDD96 "), StringJoiner::add)
        .map(StringJoiner::toString)
        .subscribe(System.out::println);

// prints Kirk  Spock  Chekov  Sulu

toList

**Available in:Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/to.html

从Observable中收集所有的item且发射他们为一个单一的List

toList example

Observable.just(2, 1, 3)
        .toList()
        .subscribe(System.out::println);

// prints [2, 1, 3]

toSortedList

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/to.html

从一个Observable中收集所有的item且发射他们为一个单一的、排好序的List

toSortedList example

Observable.just(2, 1, 3)
        .toSortedList(Comparator.reverseOrder())
        .subscribe(System.out::println);

// prints [3, 2, 1]

toMap

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/to.html

把Observable发射的item序列转换成一个以一个指定的key函数为key的Map

toMap example

Observable.just(1, 2, 3, 4)
        .toMap((x) -> {
            // defines the key in the Map
            return x;
        }, (x) -> {
            // defines the value that is mapped to the key
            return (x % 2 == 0) ? "even" : "odd";
        })
        .subscribe(System.out::println);

// prints {1=odd, 2=even, 3=odd, 4=even}

toMultimap

Available in: Flowable,Observable

ReactiveX doumentation: http://reactivex.io/documentation/operators/to.html

把Observable发射的item序列转换成一个Collection ,该Collection也是一个以一个指定的key函数为key的Map

toMultimap example

Observable.just(1, 2, 3, 4)
        .toMultimap((x) -> {
            // defines the key in the Map
            return (x % 2 == 0) ? "even" : "odd";
        }, (x) -> {
            // defines the value that is mapped to the key
            return x;
        })
        .subscribe(System.out::println);

// prints {even=[2, 4], odd=[1, 3]}

你可能感兴趣的:(数学和集合操作符)