maxWith method Null safety

KtMapEntry<K, V>? maxWith(
  1. Comparator<KtMapEntry<K, V>> comparator

Returns the first entry having the largest value according to the provided comparator or null if there are no entries.


KtMapEntry<K, V>? maxWith(Comparator<KtMapEntry<K, V>> comparator) {
  final i = iterator();
  if (!i.hasNext()) return null;
  var max =;
  while (i.hasNext()) {
    final e =;
    if (comparator(max, e) < 0) {
      max = e;
  return max;