minBy<R extends Comparable> method Null safety

KtMapEntry<K, V>? minBy<R extends Comparable>(
  1. R selector(
    1. KtMapEntry<K, V>
    )
)

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

Implementation

KtMapEntry<K, V>? minBy<R extends Comparable>(
    R Function(KtMapEntry<K, V>) selector) {
  final i = iterator();
  if (!iterator().hasNext()) return null;
  KtMapEntry<K, V> minElement = i.next();
  R minValue = selector(minElement);
  while (i.hasNext()) {
    final e = i.next();
    final v = selector(e);
    if (minValue.compareTo(v) > 0) {
      minElement = e;
      minValue = v;
    }
  }
  return minElement;
}