set method

void set(
  1. K key,
  2. V value
)

Sets the value for the given key.

If the key already exists, updates its value. Otherwise, appends the new key-value pair to the end of the internal list.

Time complexity: O(1) amortized

Example:

final map = FastMap<String, int>();
map.set('counter', 1);
map.set('counter', 2); // Updates existing value

Implementation

void set(K key, V value) {
  if (_keyToIndex.containsKey(key)) {
    _values[_keyToIndex[key]!] = value;
  } else {
    _keyToIndex[key] = _values.length;
    _values.add(value);
  }
}