GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com โ Mathematical and Aggregate Operators Mathematical and Aggregate Operators
akarnokd edited this page
This page shows operators that perform mathematical or other operations over an entire sequence of items emitted by an
The examples below assume that the import hu.akarnokd.rxjava2.math.MathObservable; import hu.akarnokd.rxjava2.math.MathFlowable; Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/average.html Calculates the average of Observable<Integer> numbers = Observable.just(1, 2, 3); MathObservable.averageDouble(numbers).subscribe((Double avg) -> System.out.println(avg)); // prints 2.0 Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/average.html Calculates the average of Observable<Integer> numbers = Observable.just(1, 2, 3); MathObservable.averageFloat(numbers).subscribe((Float avg) -> System.out.println(avg)); // prints 2.0 Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/max.html Emits the maximum value emitted by a source Observable<Integer> numbers = Observable.just(4, 9, 5); MathObservable.max(numbers).subscribe(System.out::println); // prints 9 The following example specifies a final Observable<String> names = Observable.just("Kirk", "Spock", "Chekov", "Sulu"); MathObservable.max(names, Comparator.comparingInt(String::length)) .subscribe(System.out::println); // prints Chekov Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/min.html Emits the minimum value emitted by a source Observable<Integer> numbers = Observable.just(4, 9, 5); MathObservable.min(numbers).subscribe(System.out::println); // prints 4 Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/sum.html Adds the Observable<Double> numbers = Observable.just(1.0, 2.0, 3.0); MathObservable.sumDouble(numbers).subscribe((Double sum) -> System.out.println(sum)); // prints 6.0 Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/sum.html Adds the Observable<Float> numbers = Observable.just(1.0F, 2.0F, 3.0F); MathObservable.sumFloat(numbers).subscribe((Float sum) -> System.out.println(sum)); // prints 6.0 Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/sum.html Adds the Observable<Integer> numbers = Observable.range(1, 100); MathObservable.sumInt(numbers).subscribe((Integer sum) -> System.out.println(sum)); // prints 5050 Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/sum.html Adds the Observable<Long> numbers = Observable.rangeLong(1L, 100L); MathObservable.sumLong(numbers).subscribe((Long sum) -> System.out.println(sum)); // prints 5050
Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/count.html Counts the number of items emitted by an Observable.just(1, 2, 3).count().subscribe(System.out::println); // prints 3 Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/reduce.html Apply a function to each emitted item, sequentially, and emit only the final accumulated value. Observable.range(1, 5) .reduce((product, x) -> product * x) .subscribe(System.out::println); // prints 120 Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/reduce.html Apply a function to each emitted item, sequentially, and emit only the final accumulated value. 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] Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/reduce.html Collect items emitted by the source 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 Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/reduce.html Collect items emitted by the source Note: the mutable value that will collect the items (here the Observable.just('R', 'x', 'J', 'a', 'v', 'a') .collectInto(new StringBuilder(), StringBuilder::append) .map(StringBuilder::toString) .subscribe(System.out::println); // prints RxJava Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/to.html Collect all items from an Observable.just(2, 1, 3) .toList() .subscribe(System.out::println); // prints [2, 1, 3] Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/to.html Collect all items from an Observable.just(2, 1, 3) .toSortedList(Comparator.reverseOrder()) .subscribe(System.out::println); // prints [3, 2, 1] Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/to.html Convert the sequence of items emitted by an 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} Available in: ReactiveX documentation: http://reactivex.io/documentation/operators/to.html Convert the sequence of items emitted by an 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]} Copyright (c) 2016-present, RxJava Contributors. Clone this wiki locally |
| You are visitor 01402775 โ GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร600. No cookies required. |