Java - Point 类的 hashCode() 方法有什么好处,还是我应该重写它并编写自己的方法?
有什么方法可以真正看到标准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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您要搜索
java.awt.Point
的hashCode()
,它是在java.awt.geom.Point2D
中定义的。请注意,“它会很好地哈希吗?”这个问题很难回答,它主要取决于使用模式。
您可以访问几乎所有“标准Java类”的源代码,只需在JDK安装目录中搜索
src.zip
文件(或使用像Eclipse/这样的IDE) NetBeans 并在类名称上单击 F3)。If you're searching for the
hashCode()
ofjava.awt.Point
, it is defined injava.awt.geom.Point2D
.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).是的 - 我相信它通常随 JDK 一起提供,位于 JDK 目录中的 src.zip 文件中。如果不是,那么获取它的方式将取决于您使用的 Java 版本。例如,完整的 JDK 6 源代码可以在此处获取,或者 JDK 7 有单独的源代码下载页面,提供各种选项。
至于哈希值有多好 - 为什么不使用您的实际点样本来测试它呢?碰撞的可能性总是存在,但是否真的发生将取决于您的数据。了解您的情况下哈希的无冲突程度的一种简单方法是使用 Multiset 来自 Guava - 将每个点的哈希码添加到集合中,然后基本上就会给出每个哈希码的频率。
老实说,我期望哈希算法对于一般用途来说是相当合理的。但如果您担心的话,进行测试总是一个好主意。
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
文件中。 请注意,Point
的hashCode()
是在其父级java.awt.geom.Point2D
中定义的。如果您认为现有的实现不符合您的标准,您可能更愿意使用“动态”定义的匿名类来覆盖
hashCode
方法:这样您就不必创建一个新文件。
Java source code comes with the JDK in the
src.zip
file. Note thatPoint
'shashCode()
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":This way you won't have to create a new file.
转到此链接并搜索
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.