java源代码研究:HashMap的containsKey
下面是代码,谁能跟我说说为什么要加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; }
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(); } }
[解决办法]
帮顶~!
[解决办法]
4楼有提到,根据key提取entry 我想也许是出于效率考虑,如果key的hash不同,就已经没有再判断计算下去的必要了
如果相同,才作进一步判断,因为不等或不同的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.
[解决办法]
摘自Object.class源码中hashCode方法的注释
/**
* 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();
[解决办法]
听风中叶的视频的吧?
哈哈^_^