API有云:
/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hash tables such as those provided by
* {@link java.util.HashMap}.
* <p>
* The general contract of {@code hashCode} is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the {@code hashCode} method
* must consistently return the same integer, provided no information
* used in {@code 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.
* <li>If two objects are equal according to the {@code equals(Object)}
* method, then calling the {@code hashCode} method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the {@code 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 hash tables.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class {@code 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
* Java<font size="-2"><sup>TM</sup></font> programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.System#identityHashCode
*/
public native int hashCode();
英语渣,没有看错的话就是这么几个意思:
1.该方法有益于HashMap。而hashset的无重复性也调用了map的hashcode(),HashSet API:
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
2.同样的环境,每次执行某对象的hashcode方法,返回值要一样。
3.不同的对象的并不意味着一定要产生不同的hashcode,而且返回值是int,有上限,超过int存储值的对象个数必有相同的hashcode。但是为了提高哈希表的效率,我们最好可以尽量分散,看看String如何重写hashcode的吧:
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value; //string数组
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
值为s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1],应该来说很分散了,这里是31不是32,因为32容易存在倍数关系导致不分散。
在Object的equals方法也有这样写:
* Note that it is generally necessary to override the {@code hashCode}
* method whenever this method is overridden, so as to maintain the
* general contract for the {@code hashCode} method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return {@code true} if this object is the same as the obj
* argument; {@code false} otherwise.
* @see #hashCode()
* @see java.util.HashMap
*/
public boolean equals(Object obj) {
return (this == obj);
}
注意到StringBuffer没有重写equals和hashcode,可能是String是不可变的吧,已经用了final的char数组去存储字符串了。
/** The value is used for character storage. */
private final char value[];
而StringBuffer是可变的,要重写还真不知道如何定义这两个方法。