简体   繁体   中英

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? 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?

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).

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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