在Dart中重写hashcode的好方法是什么?

ebdffaop  于 2024-01-03  发布在  其他
关注(0)|答案(6)|浏览(170)

我发现自己想为一个对象覆盖hashcode和==,我想知道是否有最佳实践来实现依赖于多个属性的hashcode,似乎有一些Dart特定的考虑因素。
最简单的答案是对所有属性的哈希值进行异或,这可能不会太糟糕。

  1. // Override hashCode using strategy from Effective Java, Chapter 11.
  2. int get hashCode {
  3. int result = 17;
  4. result = 37 * result + firstName.hashCode;
  5. result = 37 * result + lastName.hashCode;
  6. return result;
  7. }

字符串
但这看起来像是它期望截断整数语义,而在Dart中,JS整数的范围溢出似乎不利于哈希。
我们也可以这样做,在每次操作后截断为32位。
对于我的应用程序来说,集合的预期大小非常小,几乎任何东西都可以,但我很惊讶没有看到一般情况下的标准配方。有人对此有任何经验或经验丰富吗?

du7egjpx

du7egjpx1#

quiver package提供了帮助函数hash2hash3等,这些帮助函数简化了hashCode的实现任务,并在一定程度上保证了hashCode在Dart VM * 和 * 下编译为JavaScript时能够正常工作。

  1. import 'package:quiver/core.dart';
  2. class Person {
  3. String name;
  4. int age;
  5. Person(this.name, this.age);
  6. @override
  7. bool operator ==(o) => o is Person && name == o.name && age == o.age;
  8. @override
  9. int get hashCode => hash2(name.hashCode, age.hashCode);
  10. }

字符串
另请参阅this post以获得稍微详细的讨论。

展开查看全部
jv2fixgn

jv2fixgn2#

从2.14版开始,Dart语言增加了对Object.hash()的支持,沿着还有Object.hashAll()Object.hashAllUnordered()
hash()文档:
为多个对象创建组合哈希代码。
范例:

  1. class SomeObject {
  2. final Object a, b, c;
  3. SomeObject(this.a, this.b, this.c);
  4. bool operator==(Object other) =>
  5. other is SomeObject && a == other.a && b == other.b && c == other.c;
  6. int get hashCode => Object.hash(a, b, c); // <----- here
  7. }

字符串
关于实施的文件说明:
此函数生成的哈希值不能保证在同一程序的不同运行中或在同一程序的不同隔离中运行的代码之间保持稳定。所使用的确切算法可能在不同平台之间或平台库的不同版本之间有所不同,并且可能取决于每次程序执行时更改的值。

展开查看全部
2w3rbyxf

2w3rbyxf3#

为了最小化依赖关系,如果你已经依赖于flutter,但不依赖于像quiver这样的东西,dart:ui库包含用于创建和组合哈希值的工具,hashValueshashList。如果组合列表值,必须小心确保相等运算符和hashcode匹配行为。如果哈希代码深入计算它的哈希,然后使用深度相等,否则使用浅相等。

  1. class Example {
  2. final String value1;
  3. final Object value2;
  4. final List<Object> deep;
  5. final List<Object> shallow;
  6. Example({this.value1, this.value2, this.deep, this.shallow});
  7. @override
  8. operator ==(o) =>
  9. o is Example &&
  10. o.value1 == value1 &&
  11. o.value2 == value2 &&
  12. listEquals(o.deep, deep) &&
  13. o.shallow == shallow;
  14. @override
  15. int get hashCode => hashValues(value1, value2, hashList(deep), shallow);
  16. }

字符串
Flutter API docs for hashValues
Flutter API docs for hashList

展开查看全部
o2rvlv0m

o2rvlv0m4#

equatable软件包可以帮助

  1. import 'package:equatable/equatable.dart';
  2. class Person extends Equatable {
  3. final String name;
  4. final int age;
  5. Person(this.name, this.age);
  6. @override
  7. List<Object> get props => [name, age];
  8. }

字符串
现在Person将使用来自Equatable的==hashCode,Equatable接受您给予的props列表

atmip9wb

atmip9wb5#

我recomend“equatable”插件
https://pub.dev/packages/equatable
范例:
原始模式:

  1. class Person {
  2. final String name;
  3. const Person(this.name);
  4. @override
  5. bool operator ==(Object other) =>
  6. identical(this, other) ||
  7. other is Person &&
  8. runtimeType == other.runtimeType &&
  9. name == other.name;
  10. @override
  11. int get hashCode => name.hashCode;
  12. }

字符串
equatable:

  1. import 'package:equatable/equatable.dart';
  2. class Person extends Equatable {
  3. final String name;
  4. Person(this.name);
  5. @override
  6. List<Object> get props => [name];
  7. }

展开查看全部
ctrmrzij

ctrmrzij6#

由于Dart与Java非常相似,您肯定可以找到适用于Dart的Java hashCodes的好参考资料。
我在谷歌上搜索了一下Wikipedia page on Java's Object.hashCode()。它有一个简单对象的散列代码的非常基本的例子。一个流行的方法是用一个素数(不同的素数)执行乘法,并为对象的每个属性添加一些值。
This question f.e.解释了为什么选择数字31作为String.hashCode()方法的乘法。
更详细的hashcode实现的例子可以很容易地使用Google找到。

相关问题