java.lang.Object中对hashCode的约定:
1. 在一个应用程序执行期间,如果一个对象的equals方法做比较所用到的信息没有被修改的话,则对该对象调用hashCode方法多次,它必须始终如一地返回同一个整数。
2. 如果两个对象根据equals(Object o)方法是相等的,则调用这两个对象中任一对象的hashCode方法必须产生相同的整数结果。
3. 如果两个对象根据equals(Object o)方法是不相等的,则调用这两个对象中任一个对象的hashCode方法,不要求产生不同的整数结果。但如果能不同,则可能提高散列表的性能。
在java的集合中,判断两个对象是否相等的规则是:
1)、判断两个对象的hashCode是否相等 。
如果不相等,认为两个对象也不相等,完毕
如果相等,转入2)
(这一点只是为了提高存储效率而要求的,其实理论上没有也可以,但如果没有,实际使用时效率会大大降低,所以我们这里将其做为必需的。后面会重点讲到这个问题。)
2)、判断两个对象用equals运算是否相等 。
如果不相等,认为两个对象也不相等
如果相等,认为两个对象相等(equals()是判断两个对象是否相等的关键)
对于一般类的对象(除String等封装类型对象外):
若普通类没有重写hashcode()和equals()方法,那么其对象在比较时,是继承的object类中的hashcode()方法,object类中的hashcode()方法是一个本地方法,对该方法的返回值进行比较时,比较的是对象的地址(引用地址),使用new方法创建内容相同的对象,两次生成的当然是不同的对象。除非重写hashcode()方法。在object类中定义的equals()方法也是对对象地址的比较。一句话总结:若不重写普通类的hashcode()和equals()方法,在Set集合中对象引用地址不一样,对象即不重复。
对于String等对象(String、Integer、Double····等等):
由于这些封装类本身已经重写了hashcode()方法,并且重写的方法的返回值跟对象的内容相关,而不是跟引用地址相关。这些封装类中的equals()方法同样进行了重写,比较的是对象的内容,而非引用地址。一句话总结:String等类的对象在集合中均比较他们的内容,内容相同则覆盖已存在的对象。
JDK1.7中,String 类的hashCode()方法:
/**
* Returns a hash code for this string. The hash code for a
* <code>String</code> object is computed as
* <blockquote><pre>
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* </pre></blockquote>
* using <code>int</code> arithmetic, where <code>s[i]</code> is the
* <i>i</i>th character of the string, <code>n</code> is the length of
* the string, and <code>^</code> indicates exponentiation.
* (The hash value of the empty string is zero.)
*
* @return a hash code value for this object.
*/
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
String 类重写的equals()方法:
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) { //两个对象地址一样,则内容肯定一样,反之不成立
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}