associateBy<K, V> method

Map<K, String> associateBy<K, V>(
  1. K keySelector(
    1. String value
    )
)

Returns a Map containing the charachters from the string indexed by the key from keySelector.

If any of the returned keys would contain the same key, the last one gets added to the map.

The iteration order of the string is preserved.

final string = 'bonne journée';
// Associate each character with its code.
final result = string.associateBy((char) => char.codeUnitAt(0));

// Notice each letter occurs only once.
print(result); // {98: 'b', 111: 'o', 110: 'n', 101: 'e', 32: ' ', 106: 'j', 117: 'u', 114: 'r', 233: 'é'}

Implementation

Map<K, String> associateBy<K, V>(K Function(String value) keySelector) {
  return split('').associateBy(keySelector);
}