hashAll static method

int hashAll(
  1. Iterable iterable
)
override

This method hashes an iterable in such a way that two different iterables that look the same, will have the same hash.

List<String> list1 = {"item1", "item2", "item3"};
List<String> list2 = {"item1", "item2", "item3"};
List<String> list3 = {"item6", "item7", "item8"};

hashAll(list1) == hashAll(list2) // true
hashAll(list1) == hashAll(list3) // false

Unlike hashAllUnordered, this method does care about the order of the values.

List<String> list1 = {"item1", "item2", "item3"};
List<String> list2 = {"item2", "item3", "item1"};

hashAll(list1) == hashAll(list2) // false

Implementation

static int hashAll(Iterable iterable) {
  if (iterable is Map) return hashAllMap(iterable as Map);

  List<int> hashes = [];

  for (var element in iterable) {
    if (element is Map) {
      hashes.add(hashAllMap(element));
    } else if (element is List || element is Set || element is Iterable) {
      hashes.add(hashAll(element));
    } else {
      hashes.add(element.hashCode);
    }
  }

  return Object.hashAll(hashes);
}