maxBy<R extends Comparable> method

MapEntry<K, V>? maxBy<R extends Comparable>(
  1. R selector(
    1. MapEntry<K, V>
    )
)

Returns the first entry yielding the largest value of the given function or null if there are no entries.

Implementation

MapEntry<K, V>? maxBy<R extends Comparable>(
  R Function(MapEntry<K, V>) selector,
) {
  final i = entries.iterator;
  if (!i.moveNext()) return null;
  MapEntry<K, V> maxElement = i.current;
  R maxValue = selector(maxElement);
  while (i.moveNext()) {
    final e = i.current;
    final v = selector(e);
    if (maxValue.compareTo(v) < 0) {
      maxElement = e;
      maxValue = v;
    }
  }
  return maxElement;
}