hashAllUnorderedMap static method
This method hashes a map in such a way that two different maps that have the same key-value pairs, 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 hashAllMap, this method does not 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) // true
hashAllUnorderedMap(map1) == hashAllUnorderedMap(map3) // false
Implementation
static int hashAllUnorderedMap(Map map) {
List<int> hashes = [];
map.forEach((key, value) {
dynamic valToHash;
if (value is Map) {
valToHash = hashAllUnorderedMap(value);
} else if (value is List || value is Set || value is Iterable) {
valToHash = hashAllUnordered(value);
} else {
valToHash = value;
}
return hashes.add(Object.hash(key, valToHash));
});
return Object.hashAllUnordered(hashes);
}