简体   繁体   English

Java - Point类的hashCode()方法是否有用,或者我应该覆盖它并自己编写?

[英]Java - Is the Point class's hashCode() method any good, or should I override it and write my own?

Is there any way to actually see the source code of standard java classes by the way? 有没有办法真正看到标准java类的源代码? 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. 我正在制作一个点哈希表( HashSet<Point> ),我想确保它会很好地散列,但我看不出Point的hashCode()方法实际上是什么样的,所以我不知道怎么做真的很棒。 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? 如果是这样,有没有一个简单的方法来做到这一点,而无需创建一个全新的java文件/类?

If you're searching for the hashCode() of java.awt.Point , it is defined in java.awt.geom.Point2D . 如果您正在搜索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)));
}

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

Is there any way to actually see the source code of standard java classes by the way? 有没有办法真正看到标准java类的源代码?

Yes - I believe it usually comes with the JDK, in a src.zip file within your JDK directory. 是的 - 我相信它通常带有JDK,位于JDK目录中的src.zip文件中。 If it's not, then the way to get it will depend on the version of Java you're using. 如果不是,那么获取它的方式将取决于您正在使用的Java版本。 The full JDK 6 source is available here for example, or JDK 7 has a separate source code download page with various options. 例如, 这里提供完整的JDK 6源代码,或者JDK 7有一个单独的源代码下载页面,其中包含各种选项。

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. 在您的情况下找出散列无冲突的一种简单方法是使用来自GuavaMultiset - 将每个点的哈希码添加到集合中,然后基本上为您提供每个哈希码的频率。

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 source code comes with the JDK in the src.zip file. Java源代码随src.zip文件中的JDK一起提供。 Note that Point 's hashCode() is defined in its parent, java.awt.geom.Point2D . 请注意, PointhashCode()在其父级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": 如果您认为现有的实现不符合您的标准,您可能更喜欢使用“动态”定义的匿名类来覆盖hashCode方法:

Point myPoint = new Point() {

    public int hashCode() {
        // custom implementation
    }

}; // <-- note required semicolon

This way you won't have to create a new file. 这样您就不必创建新文件。

Go to this link and search for Java SE 6 JDK Source Code . 转到此链接并搜索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. 我怀疑你会做得更好,但怀疑是好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM