如何散列复合类?
令 Abstract
为抽象类,A1,A2,...,An
为继承于 Abstact
的具体类。每个 Ai
都有一个 Abstract
列表和一组预定义的、在编译时已知的原始类型集,假设我们有一个针对它们的 hush 函数,并且每个具体元素的结构中没有“循环”。
如果两个元素 e1 和 e2 具有相同的预定义基元值,则它们是相同的,并且如果对于 e1 中的每个 Abstract
,e2 中都存在一个 Abstract
,使得 e1 和e2 相同。 (换句话说,顺序并不重要)。
我正在为此类问题寻找一个好的哈希启发式方法。它不应该(据我所知,不可能)是一个完美的哈希函数,但它应该很好并且易于在运行时计算。
如果有人能给我一些如何实现这样的功能的指导,或者指导我一篇解决这个问题的文章,我会很高兴。
PS 我正在用 Java 编写,并且我认为(如果我错了请纠正我)内置的 hash()
不足以解决这个问题。
编辑:
列表和基元在构造后是固定的,但在编译时是未知的。
Let Abstract
be an abstract class, and A1,A2,...,An
concrete classes that inherit from Abstact
. Each one of Ai
has a list of Abstract
and a pre-defined, known at compile time, set of primitive types, let's assume we have a hush function for them, and there are no 'loops' in the structure of each concrete element.
Two elements e1 and e2 are identical if they have the same values for the predefined primitives, and if for each Abstract
in e1, there exists an Abstract
in e2 such that e1 and e2 are identical. (in other words, order is not important).
I am looking for a good hash heuristic for this kind of problem. It shouldn't (and as far as I know, can't be) a perfect hash function, but it should be good and easy to compute at run time.
I'll be glad if someone can give me some guidelines how to implement such a function, or direct me to an article that addresses this problem.
PS I am writing in Java, and I assume (correct me if I am wrong) the built in hash()
won't be good enough for this problem.
EDIT :
the lists and primitives are fixed after construction, but are unknown at compile time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这些列表在构造后可以更改,那么将哈希函数基于它们将是一个坏主意。想象一下,如果您将对象放入
HashMap
中,然后更改了其中的一部分。您将无法再在HashMap
中找到它,因为它的hashCode
会有所不同。您应该仅将
hashCode
结果基于不可变值。如果您的对象中没有任何不可变值,则最好的选择可能是简单地使用基本的Object.hashCode()
,尽管您会失去相等性测试的机会。但是,如果这些对象是不可变的,那么我建议为您的元素选择某种排序顺序。然后,您可以计算列表中的哈希代码,即使列表的顺序不同,它也会是相同的,因为您在哈希之前对值进行排序。
If these lists can change after they are constructed, it would be a bad idea to base the hash function on them. Imagine if you stuck your object into a
HashMap
, and then changed part of it. You would no longer be able to locate it in theHashMap
because itshashCode
would be different.You should only base the result of
hashCode
on immutable values. If you don't have any immutable values in your object, your best bet would probably be to simply use the basicObject.hashCode()
, although you'll lose out on equality testing.If these objects are immutable, however, then I recommend choosing some kind of sort order for your elements. Then you can compute a hash code across your lists, knowing that it will be the same even if the lists are in different orders, because you are sorting the values before hashing.
使用 Google Guava 的实用程序... Objects.hashCode() 很棒。另外,来源是可用的,他们已经解决了你所说的问题,所以你可以看看他们的解决方案。
Use Google Guava's utilities... Objects.hashCode() is great. Also, the source is available, and they have solved the problem you state, so you can take a look at their solution.