Java - Point 类的 hashCode() 方法有什么好处,还是我应该重写它并编写自己的方法?

发布于 2025-01-05 00:33:27 字数 210 浏览 1 评论 0原文

有什么方法可以真正看到标准java类的源代码吗?我正在制作一个点的哈希表 (HashSet),并且我想确保它能够很好地进行哈希处理,但我看不到 Point 的 hashCode() 方法实际上是什么样子,所以我不知道它到底有多好。谁能帮助我吗?我应该覆盖它吗?如果是这样,有没有一种简单的方法可以做到这一点,而无需创建一个全新的 java 文件/类?

Is there any way to actually see the source code of standard java classes by the way? I'm making a hash table of points (HashSet<Point>) and I want to make sure that it will hash well, but I can't see what Point's hashCode() method actually looks like, so I don't know how good it really is. Can anyone help me? Should I override it? And if so, is there an easy way to do this without creating a whole new java file/class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

烧了回忆取暖 2025-01-12 00:33:27

如果您要搜索 java.awt.PointhashCode(),它是在 java.awt.geom.Point2D 中定义的。

/**
 * Returns the hashcode for this <code>Point2D</code>.
 * @return      a hash code for this <code>Point2D</code>.
 */
public int hashCode() {
    long bits = java.lang.Double.doubleToLongBits(getX());
    bits ^= java.lang.Double.doubleToLongBits(getY()) * 31;
    return (((int) bits) ^ ((int) (bits >> 32)));
}

请注意,“它会很好地哈希吗?”这个问题很难回答,它主要取决于使用模式。

您可以访问几乎所有“标准Java类”的源代码,只需在JDK安装目录中搜索src.zip文件(或使用像Eclipse/这样的IDE) NetBeans 并在类名称上单击 F3)。

If you're searching for the hashCode() of java.awt.Point, it is defined in java.awt.geom.Point2D.

/**
 * Returns the hashcode for this <code>Point2D</code>.
 * @return      a hash code for this <code>Point2D</code>.
 */
public int hashCode() {
    long bits = java.lang.Double.doubleToLongBits(getX());
    bits ^= java.lang.Double.doubleToLongBits(getY()) * 31;
    return (((int) bits) ^ ((int) (bits >> 32)));
}

Note that the question "Will it hash well?" is hard to answer, it depends primarily on usage pattern.

You can access the source code of nearly all "standard Java classes", just search for the src.zip file in your JDK installation directory (or use an IDE like Eclipse/NetBeans and click F3 on the class name).

末が日狂欢 2025-01-12 00:33:27

有什么办法可以顺便真正看到标准java类的源代码吗?

是的 - 我相信它通常随 JDK 一起提供,位于 JDK 目录中的 src.zip 文件中。如果不是,那么获取它的方式将取决于您使用的 Java 版本。例如,完整的 JDK 6 源代码可以在此处获取,或者 JDK 7 有单独的源代码下载页面,提供各种选项。

至于哈希值有多好 - 为什么不使用您的实际点样本来测试它呢?碰撞的可能性总是存在,但是否真的发生将取决于您的数据。了解您的情况下哈希的无冲突程度的一种简单方法是使用 Multiset 来自 Guava - 将每个点的哈希码添加到集合中,然后基本上就会给出每个哈希码的频率。

老实说,我期望哈希算法对于一般用途来说是相当合理的。但如果您担心的话,进行测试总是一个好主意。

Is there any way to actually see the source code of standard java classes by the way?

Yes - I believe it usually comes with the JDK, in a src.zip file within your JDK directory. If it's not, then the way to get it will depend on the version of Java you're using. The full JDK 6 source is available here for example, or JDK 7 has a separate source code download page with various options.

As for how good the hash is - why not test it with a sample of your actual points? There will always be the possibility of collisions, but whether or not they really occur will depend on your data. One easy way of finding out how collision-free the hash is in your case is to use a Multiset from Guava - add the hash code from each point to the set, and then afterwards that will basically give you the frequency of each hash code.

To be honest, I'd expect the hash algorithm to be pretty reasonable for general purpose use. But testing is always a good idea if you're concerned.

Java 源代码随 JDK 一起提供在 src.zip 文件中。 请注意,PointhashCode() 是在其父级 java.awt.geom.Point2D 中定义的。

如果您认为现有的实现不符合您的标准,您可能更愿意使用“动态”定义的匿名类来覆盖 hashCode 方法:

Point myPoint = new Point() {

    public int hashCode() {
        // custom implementation
    }

}; // <-- note required semicolon

这样您就不必创建一个新文件。

Java source code comes with the JDK in the src.zip file. Note that Point's hashCode() is defined in its parent, java.awt.geom.Point2D.

If you decide the existing implementation is not up to your standards, you might prefer to override the hashCode method using an anonymous class, defined "on the fly":

Point myPoint = new Point() {

    public int hashCode() {
        // custom implementation
    }

}; // <-- note required semicolon

This way you won't have to create a new file.

执笔绘流年 2025-01-12 00:33:27

转到此链接并搜索Java SE 6 JDK 源代码。下载源代码并自行阅读。我怀疑你会做得更好,但持怀疑态度是件好事。

Go to this link and search for Java SE 6 JDK Source Code. Download the source and read it for yourself. I doubt you'll do better but it's good to be skeptical.

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