hash 静态方法

  1. @Since("2.14")
int hash(
  1. Object? object1,
  2. Object? object2, [
  3. Object? object3 = sentinelValue,
  4. Object? object4 = sentinelValue,
  5. Object? object5 = sentinelValue,
  6. Object? object6 = sentinelValue,
  7. Object? object7 = sentinelValue,
  8. Object? object8 = sentinelValue,
  9. Object? object9 = sentinelValue,
  10. Object? object10 = sentinelValue,
  11. Object? object11 = sentinelValue,
  12. Object? object12 = sentinelValue,
  13. Object? object13 = sentinelValue,
  14. Object? object14 = sentinelValue,
  15. Object? object15 = sentinelValue,
  16. Object? object16 = sentinelValue,
  17. Object? object17 = sentinelValue,
  18. Object? object18 = sentinelValue,
  19. Object? object19 = sentinelValue,
  20. Object? object20 = sentinelValue,
])

为多个对象创建一个组合哈希码。

即使参数为 null,也会为所有实际提供的参数计算哈希码,通过数值组合每个参数的 Object.hashCode 来计算。

示例

class SomeObject {
  final Object a, b, c;
  SomeObject(this.a, this.b, this.c);
  bool operator ==(Object other) =>
      other is SomeObject && a == other.a && b == other.b && c == other.c;
  int get hashCode => Object.hash(a, b, c);
}

在单个程序执行期间,如果函数多次以相同的参数调用,则计算出的值将是一致的。

此函数生成的哈希值**不保证**在不同的程序运行之间或在不同程序的隔离器之间是稳定的。所使用的确切算法可能因平台或平台库的不同版本而异,并且可能取决于每次程序执行时都会变化的值。

hashAll 函数在调用时传入一个包含此函数实际参数的集合,并且参数顺序与此函数相同的情况下,会得到与该函数相同的结果。

实现

@Since("2.14")
static int hash(Object? object1, Object? object2,
    [Object? object3 = sentinelValue,
    Object? object4 = sentinelValue,
    Object? object5 = sentinelValue,
    Object? object6 = sentinelValue,
    Object? object7 = sentinelValue,
    Object? object8 = sentinelValue,
    Object? object9 = sentinelValue,
    Object? object10 = sentinelValue,
    Object? object11 = sentinelValue,
    Object? object12 = sentinelValue,
    Object? object13 = sentinelValue,
    Object? object14 = sentinelValue,
    Object? object15 = sentinelValue,
    Object? object16 = sentinelValue,
    Object? object17 = sentinelValue,
    Object? object18 = sentinelValue,
    Object? object19 = sentinelValue,
    Object? object20 = sentinelValue]) {
  if (sentinelValue == object3) {
    return SystemHash.hash2(object1.hashCode, object2.hashCode, _hashSeed);
  }
  if (sentinelValue == object4) {
    return SystemHash.hash3(
        object1.hashCode, object2.hashCode, object3.hashCode, _hashSeed);
  }
  if (sentinelValue == object5) {
    return SystemHash.hash4(object1.hashCode, object2.hashCode,
        object3.hashCode, object4.hashCode, _hashSeed);
  }
  if (sentinelValue == object6) {
    return SystemHash.hash5(object1.hashCode, object2.hashCode,
        object3.hashCode, object4.hashCode, object5.hashCode, _hashSeed);
  }
  if (sentinelValue == object7) {
    return SystemHash.hash6(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object8) {
    return SystemHash.hash7(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object9) {
    return SystemHash.hash8(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object10) {
    return SystemHash.hash9(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        object9.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object11) {
    return SystemHash.hash10(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        object9.hashCode,
        object10.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object12) {
    return SystemHash.hash11(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        object9.hashCode,
        object10.hashCode,
        object11.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object13) {
    return SystemHash.hash12(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        object9.hashCode,
        object10.hashCode,
        object11.hashCode,
        object12.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object14) {
    return SystemHash.hash13(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        object9.hashCode,
        object10.hashCode,
        object11.hashCode,
        object12.hashCode,
        object13.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object15) {
    return SystemHash.hash14(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        object9.hashCode,
        object10.hashCode,
        object11.hashCode,
        object12.hashCode,
        object13.hashCode,
        object14.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object16) {
    return SystemHash.hash15(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        object9.hashCode,
        object10.hashCode,
        object11.hashCode,
        object12.hashCode,
        object13.hashCode,
        object14.hashCode,
        object15.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object17) {
    return SystemHash.hash16(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        object9.hashCode,
        object10.hashCode,
        object11.hashCode,
        object12.hashCode,
        object13.hashCode,
        object14.hashCode,
        object15.hashCode,
        object16.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object18) {
    return SystemHash.hash17(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        object9.hashCode,
        object10.hashCode,
        object11.hashCode,
        object12.hashCode,
        object13.hashCode,
        object14.hashCode,
        object15.hashCode,
        object16.hashCode,
        object17.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object19) {
    return SystemHash.hash18(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        object9.hashCode,
        object10.hashCode,
        object11.hashCode,
        object12.hashCode,
        object13.hashCode,
        object14.hashCode,
        object15.hashCode,
        object16.hashCode,
        object17.hashCode,
        object18.hashCode,
        _hashSeed);
  }
  if (sentinelValue == object20) {
    return SystemHash.hash19(
        object1.hashCode,
        object2.hashCode,
        object3.hashCode,
        object4.hashCode,
        object5.hashCode,
        object6.hashCode,
        object7.hashCode,
        object8.hashCode,
        object9.hashCode,
        object10.hashCode,
        object11.hashCode,
        object12.hashCode,
        object13.hashCode,
        object14.hashCode,
        object15.hashCode,
        object16.hashCode,
        object17.hashCode,
        object18.hashCode,
        object19.hashCode,
        _hashSeed);
  }
  return SystemHash.hash20(
      object1.hashCode,
      object2.hashCode,
      object3.hashCode,
      object4.hashCode,
      object5.hashCode,
      object6.hashCode,
      object7.hashCode,
      object8.hashCode,
      object9.hashCode,
      object10.hashCode,
      object11.hashCode,
      object12.hashCode,
      object13.hashCode,
      object14.hashCode,
      object15.hashCode,
      object16.hashCode,
      object17.hashCode,
      object18.hashCode,
      object19.hashCode,
      object20.hashCode,
      _hashSeed);
}