定义
在java.lang
包下的Object
类中注释了HashCode
的定义:
/**
* 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™ 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();
从上面的注释中我们可以知道:
HashCode
的存在主要是为了查找的快捷性;- 如果两个对象相同(
equals
相等),那么他们的HashCode
一定相同; - 如果重写了类的
equals()
方法,建议同时重写类的hashCode()
方法,务必不要违反第 2 点; HashCode
相同的两个对象并不一定相同,只能断定他们存放在散列储存结构中的相同位置。
例子
针对第 4 点,就拿常用的String
类举一个例子。
在String
类中,hashCode()
方法是这样的:
/**
* Returns a hash code for this string. The hash code for a
* {@code String} object is computed as
* <blockquote><pre>
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* </pre></blockquote>
* using {@code int} arithmetic, where {@code s[i]} is the
* <i>i</i>th character of the string, {@code n} is the length of
* the string, and {@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;
}
照着上面的HashCode
计算方式,我们可以很简单地举出反例:
package com.vingyun;
public class HashCodeTest {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String vingStr = "Ving";
String mirrorStr = "WKPH";
System.out.println("vingStr.equals(mirrorStr): " + vingStr.equals(mirrorStr));
System.out.print("vingStr.hashCode() == mirrorStr.hashCode(): ");
System.out.println(vingStr.hashCode() == mirrorStr.hashCode());
}
}
用途
主要在散列储存结构中使用,例如:HashSet
、HashMap
等。