operator []= method

  1. @override
void operator []=(
  1. K key,
  2. V value
)
override

Associates the key with the given value.

If the key was already in the map, its associated value is changed. Otherwise the key/value pair is added to the map.

Implementation

@override
void operator []=(K key, V value) {
  if (_size / _capacity > _loadFactorThreshold) {
    _resize();
  }

  final index = _getBucketIndex(key);
  _buckets[index] ??= []; // Initialize bucket if null

  final bucket = _buckets[index]!;
  for (final entry in bucket) {
    if (entry.key == key) {
      entry.value = value; // Update existing value
      return;
    }
  }

  // Key not found, add new entry
  bucket.add(_HashMapEntry(key, value));
  _size++;
}