055.194--hashcode

theoffspring 2009-10-02 06:00:00
Which two statements are true about the hashCode method? (Choose two.)

A. The hashCode method for a given class can be used to test for object equality and object inequality for
that class.
B. The hashCode method is used by the java.util.SortedSet collection class to order the elements within
that set.
C. The hashCode method for a given class can be used to test for object inequality, but NOT object
equality, for that class.
D. The only important characteristic of the values returned by a hashCode method is that the distribution
of values must follow a Gaussian distribution.
E. The hashCode method is used by the java.util.HashSet collection class to group the elements within
that set
into hash buckets for swift retrieval.

Answer: CE

选C对吗?jdk的规范上明确说,不相等的两个对象,不必hashcode也不一样,尽管实践中都是不同的。
...全文
62 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
loveofmylife 2009-10-03
  • 打赏
  • 举报
回复
这句话强调的是如果两个对象的hashcode不同,那么这两个对象是不等的, 由于现实中很难达到完全的散列化,所以两个不同的对象他们的hashcode也可能相等,但是,重写hashcode的时候最重要的因素就是:在任何时刻对同一个对象调用Hashcode得到的都是相同的结果,也就是说只要死不同的hashcode,那肯定不是同一个对象
当然你可以让俩个对象的Hashcode不相等,然后在重写equal的时候让这两个对象不等,但是这样做是不合理的
theoffspring 2009-10-03
  • 打赏
  • 举报
回复
ding

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧