hashAllMap static method

int hashAllMap(
  1. Map map
)

This method hashes a map in such a way that two different maps that look the same, will have the same hash.

Map map1 = {"item1": 1, "item2": 2, "item3": 3};
Map map2 = {"item1": 1, "item2": 2, "item3": 3};
Map map3 = {"item6": 6, "item7": 7, "item8": 8};

hashAllMap(map1) == hashAllMap(map2) // true
hashAllMap(map1) == hashAllMap(map3) // false

Unlike hashAllUnorderedMap, this method does care about the order of the key-value pairs.

Map map1 = {"item1": 1, "item2": 2, "item3": 3};
Map map2 = {"item2": 2, "item3": 3, "item1": 1};
Map map3 = {"item1": 2, "item2": 1, "item3": 3};

hashAllMap(map1) == hashAllMap(map2) // false
hashAllMap(map1) == hashAllMap(map3) // false

Implementation

static int hashAllMap(Map map) {
  List<int> hashes = [];

  map.forEach((key, value) {
    dynamic valToHash;

    if (value is Map) {
      valToHash = hashAllMap(value);
    } else if (value is List || value is Set || value is Iterable) {
      valToHash = hashAll(value);
    } else {
      valToHash = value;
    }

    return hashes.add(Object.hash(key, valToHash));
  });

  return Object.hashAll(hashes);
}