下面是代码,谁能跟我说说为什么要加e.hash == hash 这句话
/**
* Returns the entry associated with the specified key in the
* HashMap. Returns null if the HashMap contains no mapping
* for the key.
*/
final Entry<K,V> getEntry(Object key) {
int hash = (key == null) ? 0 : hash(key.hashCode());
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
/**
* Returns the entry associated with the specified key in the
* HashMap. Returns null if the HashMap contains no mapping
* for the key.
*/
final Entry<K,V> getEntry(Object key) {
int hash = (key == null) ? 0 : hash(key.hashCode());
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
所以平时如果要重写equals方法,那么hashcode方法也必须重写
这里,如果key的hashcode不同,那么可以肯定不是同一个key
所以平时如果要重写equals方法,那么hashcode方法也必须重写
如果两个对象相同,那么它们的hashCode值一定要相同所以先e.hash == hash,如果两个对象的hashCode相同,它们并不一定相同所以后面再key.equals(k)
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))看这句话
如果e.key.equals(key)那么hashCode返回的int值应该是一样的呀,为什么要多加一个e.hash == hash 呢
为什么不直接写成:
if (
((k = e.key) == key || (key != null && key.equals(k))))
package test;import java.util.HashMap;
import java.util.Map;public class Test {
public static void main(String[] args) {
Map<Test1, String> m = new HashMap<Test1, String>();
Test1 t1 = new Test1("test");
Test1 t2 = new Test1("test");
System.out.println(t1.equals(t2));//这里将始终输出false,因为重写了equals.
m.put(t1, "aaaa");
// 这里,如果把hashCode注释掉,那么这里输出的将是false,原因是t1和t2的hashCode不一样
// 反过来说你如果把e.hash == hash注释掉,即使你认为t1和t2是同一个key,但是返回的hashCode不一样,所以t1和t2还不是同一个对象
System.out.println(m.containsKey(t2));
}
}class Test1{
private String test; public Test1(String t){
test = t;
}
public String getTest() {
return test;
}
public boolean equals(Object o){
Test1 t = (Test1) o;
return test.equals(t.getTest());
}
public int hashCode(){
return test.hashCode();
}
}
这里明明输出的是true
如果相同,才作进一步判断,因为不等或不同的key也是有可能具有相同hash的,这一点有sun文档佐证
* <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 <tt>hashCode</tt> 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.
* Returns a hash code value for the object. This method is
* supported for the benefit of hashtables such as those provided by
* <code>java.util.Hashtable</code>.
* <p>
* The general contract of <code>hashCode</code> is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the <tt>hashCode</tt> method
* must consistently return the same integer, provided no information
* used in <tt>equals</tt> 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 <tt>equals(Object)</tt>
* method, then calling the <code>hashCode</code> 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 <tt>hashCode</tt> 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.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class <tt>Object</tt> 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.util.Hashtable
*/
public native int hashCode();