如何散列复合类?

发布于 2024-10-26 18:01:37 字数 600 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

树深时见影 2024-11-02 18:01:37

如果这些列表在构造后可以更改,那么将哈希函数基于它们将是一个坏主意。想象一下,如果您将对象放入 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 the HashMap because its hashCode 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 basic Object.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.

冷夜 2024-11-02 18:01:37

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文