HashMap 是通過key_value當成一個整體進行處理,通過key的hashCoder反回一個int數(shù),然后還會調(diào)用一個hash方法。來確定存儲位置。
HashMap 存儲java對像,其實并沒有真正將 Java 對象放入 Set 集合中,而是多個引用變量所組成的集合,這些引用變量指向?qū)嶋H的 Java 對象。
HashMap 存儲.以如下代碼為例:
HashMap<String,String> ma = new HashMap<String,String>();
ma.put("a", "aaa");
ma.put("b", "bbb");
HashMap 采用一種所謂的“Hash 算法”來決定每個元素的存儲位置。
在上面代碼中系統(tǒng)會調(diào)用a的hashCode方法來確定來決定該元素的存儲位置。以下是部分源碼
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
hash方法對應(yīng)在下面
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
大家可以看下上面的Entry,我理解成其實就是一個key_value對。然后通過hash(key.hashCode())這樣確定位置。系統(tǒng)這樣才能存儲在那里。
關(guān)鍵是int i = indexFor(hash, table.length);我理解是這個對找到相對應(yīng)的索引塊。加快速度。包括get的時候也會調(diào)用這個方法,確實索引塊的位置.
來看看具體算法
static int indexFor(int h, int length) {
return h & (length-1);
}
h是hash的一個值,length中這個table的長度。假設(shè) h=5,length=16, 那么 h & length - 1 將得到 5,這個length為什么我會假設(shè)是16,大家可以看看源碼
也就知道static final int DEFAULT_INITIAL_CAPACITY = 16; 初始化的時候就是16。這樣目的在于計算得到的索引值總是位于 table 數(shù)組的索引之內(nèi)
根據(jù)上面 put 方法的源代碼可以看出,當程序試圖將一個 key-value 對放入 HashMap 中時,
程序首先根據(jù)該 key 的 hashCode() 返回值決定該 Entry 的存儲位置:如果兩個 Entry 的 key 的 hashCode() 返回值相同,
那它們的存儲位置相同。如果這兩個 Entry 的 key 通過 equals 比較返回 true,
新添加 Entry 的 value 將覆蓋集合中原有 Entry 的 value,但 key 不會覆蓋。如果這兩個 Entry 的 key 通過 equals 比較返回 false
,新添加的 Entry 將與集合中原有 Entry 形成 Entry 鏈,而且新添加的 Entry 位于 Entry 鏈的頭部——具體說明繼續(xù)看 addEntry() 方法的說明。
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
大家看看這里就知道了, 就是把添加的 Entry 對象放入 table 數(shù)組的 bucketIndex 索引處——如果 bucketIndex 索引處已經(jīng)有了一個 Entry 對象,那新添加的 Entry 對象指向原有的 Entry 對象(產(chǎn)生一個 Entry 鏈),
如果 bucketIndex 索引處沒有 Entry 對象,也會新放入的 Entry 對象指向 null,也就是沒有產(chǎn)生 Entry 鏈。
如果 Map 中的 key-value 對的數(shù)量超過了極限,就會度擴充到 2 倍。
HashMap的取值
public V get(Object key) {
if (key == null)
return getForNullKey();
int hash = 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.equals(k)))
return e.value;
}
return null;
}
其實也是先要找到對應(yīng)的位置,HashMap是用equals判斷當前鍵是否和表中的鍵相同。所以在這里要說明如果要用自己的類做為HashMap的鍵,必須同時重載wqual()和hashCode()
posted on 2010-09-01 11:39
青菜貓(孫宇) 閱讀(2230)
評論(1) 編輯 收藏 所屬分類:
java