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};

hashAllUnorderedMap(map1) == hashAllUnorderedMap(map2) // true
hashAllUnorderedMap(map1) == hashAllUnorderedMap(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};

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

Implementation

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

  map.forEach((key, value) => hashes.add(Object.hash(key, value)));

  return Object.hashAll(hashes);
}