A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李龙涛   /  2011-7-22 19:14  /  8621 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

hashCode方法是在哪个类中被定义的呢?不同的类的hashCode方法是不是覆盖掉父类的hashCode方法而来的呢?那我可以这样理解吗:不同类的hashCode方法的返回值,是由不同的代码块计算出来的。

评分

参与人数 1技术分 +2 收起 理由
小龙 + 2 继续加油!

查看全部评分

5 个回复

倒序浏览
黑马网友  发表于 2011-7-22 19:46:23
沙发
恩 可以那么理解,如果一个类重写了hashcode方法,就调用它的hashcode方法,如果没有就调用它父类的hashcode方法。如果还没有就找他的父类的父类,最后到Object类。Object有最后的hashcode方法....

评分

参与人数 1技术分 +1 收起 理由
小龙 + 1

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-7-22 20:02:12
藤椅
java中所有的类都默认继承Object,hashcode是在Object中定义出来的,有时我们需要重写hashCode方法。
jdk文档中是这样写的hashCode
public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
The general contract of hashCode is:

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)


Returns:
a hash code value for this object.
See Also:
equals(java.lang.Object), Hashtable

该方法在 Object  类中是这样定义的 public native int hashCode();

我觉着是系统定义的,返回的值就是一个标号,有利于hashMap,hashTable等等结合框架或其他,在内存中找到他。具体为什么这个方法是native的还有他的值是如何来的 我觉着应该是系统里面的产生的,我只能按自己的想法解答到这。

评分

参与人数 1技术分 +2 收起 理由
小龙 + 2 看得头晕,辛苦分

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-7-22 20:50:34
板凳
散列码是由对象导出的一个整数值。散列码是没有规律的。如果x和y是两个不同的对象,x.hashCode()与y.hashCode()基本上不会相同。举个例子,String类的对象的hashcode是这样计算出来的:
String x;//定义某个String对象。
int hash=0;
for(int i=0;i<x.length();i++)
hash=31*hash+charAt(i);
其hashcode就是该对象在内存中的存储位置。(当然默认的hashCode()方法适用,重写的话就按自己写的来算。)

评分

参与人数 1技术分 +1 收起 理由
小龙 + 1 参与有分!

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-7-22 21:03:00
报纸
覆盖hashCode是为了保证集合中的元素不重复,以HashSet为例,当HashSet中放入了两个对象后,默认他们的hashCode都是不一样的,因此不需要调用equals方法就能确定这两个对象不是同一个,但是如果这两个对象的值是一样的,这种判断结果就不符合用户要求了,因此需要重写hashcode方法,将两个对象的hashcode值设为相同,这样就可以进入equals判断阶段,equals方法是继承自object类的,比较的是对象的地址值,所以判断的结果也必定不同(除非两个引用指向同一对象),这又不符合要求了,因此也需要重写equals方法。经过改写以上两个方法,可以完成两个对象的比较,只要两个对象的内容(值)相同,就判断为同一个对象,不得重复存储。String就是本身就重写了这两个方法,所以在存入HashSet时不会造成重复。但是如果我们自定对象,在没有重写这两个方法的情况下,会造成元素内容重复,所以最好依据对象的特有条件来建立hashcode和euqals的实现。

评分

参与人数 1技术分 +2 收起 理由
小龙 + 2 希望你能帮助到他!

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-7-22 22:34:35
地板
hashcode如果没有重写调用父类的方法。我目前的认识是,一个hashcode值是对象的类的类型确定了类在内存映射的大区域,而对象的参数(不仅仅是成员变量的参数)决定了具体位置,也就是他的hashcode值

评分

参与人数 1技术分 +1 收起 理由
小龙 + 1 好好学习,天天向上!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马