associate<K, V> method

Map<K, V> associate<K, V>(
  1. (K, V) transform(
    1. int index,
    2. E element
    )
)

Returns a Map containing key-value pairs provided by transform function applied to elements of this collection.

If any of two pairs would have the same key the last one gets added to the map.

Implementation

Map<K, V> associate<K, V>(
  (K, V) Function(int index, E element) transform,
) {
  final map = <K, V>{};
  final it = iterator;

  for (var i = 0; it.moveNext(); i++) {
    final (key, value) = transform(i, it.current);
    map[key] = value;
  }

  return map;
}