您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Map的key、value值的數(shù)據(jù)類型不能為基本類型的原因有哪些,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
interface Map<K,V>
Map源碼
/** * Returns the hash code value for this map entry. The hash code * of a map entry <tt>e</tt> is defined to be: <pre> * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ * (e.getValue()==null ? 0 : e.getValue().hashCode()) * </pre> * This ensures that <tt>e1.equals(e2)</tt> implies that * <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries * <tt>e1</tt> and <tt>e2</tt>, as required by the general * contract of <tt>Object.hashCode</tt>. * * @return the hash code value for this map entry * @see Object#hashCode() * @see Object#equals(Object) * @see #equals(Object) */ int hashCode();
hashCode返回 (e.getKey()==null ? 0 : e.getKey().hashCode()) ^(e.getValue()==null ? 0 : e.getValue().hashCode())
class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
HashMap源碼中:
public final int hashCode() { return Objects.hashCode(key) ^ Objects.hashCode(value); }
static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); }
public final boolean equals(Object o) { if (o == this) return true; if (o instanceof Map.Entry) { Map.Entry<?,?> e = (Map.Entry<?,?>)o; if (Objects.equals(key, e.getKey()) && Objects.equals(value, e.getValue())) return true; } return false; }
補充知識:java hashmap key long 和int 區(qū)別
最近同事問起,map里面存的key 是int 類型的,存了一個 Integera =123,為什么使用long 123 作為key get,取出來的是空,這個問題粗想了一下,感覺long和int 本身 類型不同,肯定不能是同一個key,后來細(xì)研究了一下,跟了一下源碼,發(fā)現(xiàn)邏輯不是那么簡單。
簡單測試了一下,如下代碼
Map<Object,String> map = new HashMap<>(); Integer b = 123; Long c =123L; map.put(b, b+"int"); System.out.println(b.hashCode() == c.hashCode()); //true System.out.println(map.get(c)); //null map.put(c, c+"long"); // size =2
簡單的總結(jié)了一下問題:
1、HashMap 是把key做hash 然后作為數(shù)組下標(biāo),但是 b 和c 的hashcode 竟然是相同的,為什么 get(c) 為空
2、HashMap 存入c 發(fā)現(xiàn) size=2 ,hashcode相同 為什么 size=2,get(c) =123long
帶著上面兩個問題跟了一遍源碼,問題豁然開朗
1、hashmap在存入的時候,先對key 做一遍 hash,以hash值作為數(shù)組下標(biāo),如果發(fā)現(xiàn) 下標(biāo)已有值,判斷 存的key 跟傳入的key是不是相同,使用 (k = p.key) == key || (key != null && key.equals(k)) ,如果相同覆蓋,而Interger的equals 方法如下:
if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false;
Integer 和 Long 肯定不是一個類型,返回 false,這時候 hashmap 以 hashkey 沖突來處理,存入鏈表,所以 Long 123 和 Integer 123 hashmap會認(rèn)為是 hash沖突
2、hashmap 在 get的時候,也是先做 hash處理,根據(jù)hash值查找對應(yīng)的數(shù)組下標(biāo),如果找到,使用存入時候的判斷條件
(k = first.key) == key || (key != null && key.equals(k)) 如果相等就返回,不相等,查找當(dāng)前值的next有沒有值,有繼續(xù)根據(jù)邏輯判斷,所以 存入Integer 123 根據(jù) Long 123 來獲取返回的 是 NULL
至此,解析完畢。
關(guān)于Map的key、value值的數(shù)據(jù)類型不能為基本類型的原因有哪些就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。