updateAndJoin method

Map<K, V> updateAndJoin(
  1. Map<K, V>? map
)

Merges this map with another map. If a key already exists, the new map's value overrides the existing one.

Example:

Map<String, int>? map1 = {"a": 1, "b": 2};
Map<String, int>? map2 = {"b": 3, "c": 4};
print(map1.updateAndJoin(map2)); // {a: 1, b: 3, c: 4}

Implementation

Map<K, V> updateAndJoin(Map<K, V>? map) {
  if (isNullOrEmpty) return map ?? {};
  if (map.isNullOrEmpty) return this ?? {};
  final newMap = this!;
  for (final key in map!.keys) {
    if (this!.containsKey(key)) {
      newMap[key] = map[key] as V;
    } else {
      newMap.addAll({key: map[key] as V});
    }
  }
  return map;
}