Java-Point类的hashCode()方法有什么好处,还是应该重写它并编写自己的方法? [英] Java - Is the Point class's hashCode() method any good, or should I override it and write my own?
问题描述
有什么办法可以实际看到标准Java类的源代码吗?我正在制作一个散列点(HashSet<Point>
),我想确保它能很好地散列,但是我看不到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?
推荐答案
如果要搜索java.awt.Point
的hashCode()
,则在java.awt.geom.Point2D
中定义.
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.
您可以访问几乎所有标准Java类" 的源代码,只需在JDK安装目录中搜索src.zip
文件(或使用Eclipse/NetBeans之类的IDE,然后单击F3在班级名称上).
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-Point类的hashCode()方法有什么好处,还是应该重写它并编写自己的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!