Max

emits the item from the source Observable that had the maximum value

The Max operator operates on an Observable that emits numbers (or items that can be evaluated as numbers), and emits a single item: the item with the largest number.

See Also

Language-Specific Information:

In RxGroovy, this operator is not in the ReactiveX core, but is part of the distinct rxjava-math module.

max

RxGroovy implements a max operator. It takes an optional comparator that it will use instead of its default to compare the value of two items. If more than one item has the identical maximum value, max will emit the last such item emitted by the source Observable.

maxBy

The maxBy operator is similar to max, but instead of emitting the item with the maximum value, it emits the item with the maximum key, where that key is generated based on a function you provide to maxBy

In RxJava, this operator is not in the ReactiveX core, but is part of the distinct rxjava-math module.

max

RxJava implements a max operator. It takes an optional comparator that it will use instead of its default to compare the value of two items. If more than one item has the identical maximum value, max will emit the last such item emitted by the source Observable.

maxBy

The maxBy operator is similar to max, but instead of emitting the item with the maximum value, it emits the item with the maximum key, where that key is generated based on a function you provide to maxBy

max

RxJS implements the max operator. It takes an optional comparer function that it will use instead of its default to compare the value of two items.

Sample Code

var source = Rx.Observable.fromArray([1,3,5,7,9,2,4,6,8]).max();

var subscription = source.subscribe(
  function (x) { console.log('Next: ' + x); },
  function (err) { console.log('Error: ' + err); },
  function () { console.log('Completed'); } );
Next: 9
Completed
maxBy

The maxBy operator is similar to max, but instead of emitting the item with the maximum value, it emits the item with the maximum key, where that key is generated based on a function you provide to maxBy. maxBy also takes an optional second parameter: a comparer function that it will use instead of its default to compare the keys of the two items.

maxBy emits a list. If more than one item has the maximum key value, each such item will be represented in the list.

Sample Code

var source = Rx.Observable.fromArray([1,3,5,7,9,2,4,6,8,9])
               .maxBy( function (x) { return x; } );

var subscription = source.subscribe(
  function (x) { console.log('Next: ' + x); },
  function (err) { console.log('Error: ' + err); },
  function () { console.log('Completed'); } );
Next: 9,9
Completed

max and maxBy are found in the following distributions:

  • rx.all.js
  • rx.all.compat.js
  • rx.aggregates.js

They requires one of the following:

  • rx.js
  • rx.compat.js
  • rx.lite.js
  • rx.lite.compat.js

RxPHP implements this operator as max.

Returns the maximum value in an observable sequence according to the specified comparer.

Sample Code

//from https://github.com/ReactiveX/RxPHP/blob/master/demo/max/max.php

/* Without comparer */
$source = \Rx\Observable::fromArray([1, 3, 5, 7, 9, 2, 4, 6, 8])
    ->max();

$subscription = $source->subscribe($createStdoutObserver());

   
Next value: 9
Complete!
    
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/max/max-with-comparer.php

/* With a comparer */
$comparer = function ($x, $y) {
    if ($x > $y) {
        return 1;
    } elseif ($x < $y) {
        return -1;
    }
    return 0;
};

$source = \Rx\Observable::fromArray([1, 3, 5, 7, 9, 2, 4, 6, 8])
    ->max($comparer);

$subscription = $source->subscribe($createStdoutObserver());

   
Next value: 9
Complete!