associateWith<V> method

Map<E, V> associateWith<V>(
  1. V valueSelector(
    1. int index,
    2. E element
    )
)

Returns a Map containing the values returned from valueSelector function applied to each element indexed by the elements from the collection.

If any of elements (-> keys) would be the same the last one gets added to the map.

Implementation

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

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

  return map;
}