深入JDK源代碼之HashMap實現

[color=red][b] 以下是JDK1.6中文版的對HashMap的具體介紹:[/b] [/color]
基於哈希表的 Map 接口的實現。此實現提供所有可選的映射操作,並允許使用 null 值和 null 鍵。(除了非同步和允許使用 null 之外,HashMap 類與 Hashtable 大致相同。)此類不保證映射的順序,特別是它不保證該順序恆久不變。
此實現假定哈希函數將元素適當地分佈在各桶之間,可爲基本操作(get 和 put)提供穩定的性能。迭代 collection 視圖所需的時間與 HashMap 實例的“容量”(桶的數量)及其大小(鍵-值映射關係數)成比例。所以,如果迭代性能很重要,則不要將初始容量設置得太高(或將加載因子設置得太低)。
HashMap 的實例有兩個參數影響其性能:初始容量 和加載因子。容量 是哈希表中桶的數量,初始容量只是哈希表在創建時的容量。加載因子 是哈希表在其容量自動增加之前可以達到多滿的一種尺度。當哈希表中的條目數超出了加載因子與當前容量的乘積時,則要對該哈希表進行 rehash 操作(即重建內部數據結構),從而哈希表將具有大約兩倍的桶數。
通常,默認加載因子 (0.75) 在時間和空間成本上尋求一種折衷。加載因子過高雖然減少了空間開銷,但同時也增加了查詢成本(在大多數 HashMap 類的操作中,包括 get 和 put 操作,都反映了這一點)。在設置初始容量時應該考慮到映射中所需的條目數及其加載因子,以便最大限度地減少 rehash 操作次數。如果初始容量大於最大條目數除以加載因子,則不會發生 rehash 操作。
如果很多映射關係要存儲在 HashMap 實例中,則相對於按需執行自動的 rehash 操作以增大表的容量來說,使用足夠大的初始容量創建它將使得映射關係能更有效地存儲。

[color=red][b]下面是HashMap中的方法:[/b][/color]
void clear() 
從此映射中移除所有映射關係。
Object clone()
返回此 HashMap 實例的淺表副本:並不複製鍵和值本身。
boolean containsKey(Object key)
如果此映射包含對於指定鍵的映射關係,則返回 true。
boolean containsValue(Object value)
如果此映射將一個或多個鍵映射到指定值,則返回 true。
Set<Map.Entry<K,V>> entrySet()
返回此映射所包含的映射關係的 Set 視圖。
V get(Object key)
返回指定鍵所映射的值;如果對於該鍵來說,此映射不包含任何映射關係,則返回 null。
boolean isEmpty()
如果此映射不包含鍵-值映射關係,則返回 true。
Set<K> keySet()
返回此映射中所包含的鍵的 Set 視圖。
V put(K key, V value)
在此映射中關聯指定值與指定鍵。
void putAll(Map<? extends K,? extends V> m)
將指定映射的所有映射關係複製到此映射中,這些映射關係將替換此映射目前針對指定映射中所有鍵的所有映射關係。
V remove(Object key)
從此映射中移除指定鍵的映射關係(如果存在)。
int size()
返回此映射中的鍵-值映射關係數。
Collection<V> values()
返回此映射所包含的值的 Collection 視圖。

[color=red][b]下面深入HashMap源代碼,詳解它的具體實現:
[/b][/color]
[color=red][b]一、HashMap的數據結構[/b][/color]: HashMap用了一個名字爲table的Entry類型數組;數組中的每一項又是一個Entry鏈表。
[img]http://dl.iteye.com/upload/attachment/523988/5242acdf-5c7d-3ea2-8adc-19da1360da94.jpeg[/img]

 // 默認的初始化大小
static final int DEFAULT_INITIAL_CAPACITY = 16;
// 最大的容量
static final int MAXIMUM_CAPACITY = 1 << 30;
// 負載因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
// 儲存key-value鍵值對的數組,一個鍵值對對象映射一個Entry對象
transient Entry[] table;
// 鍵值對的數目
transient int size;
// 調整HashMap大小門檻,該變量包含了HashMap能容納的key-value對的極限,它的值等於HashMap的容量乘以負載因子
int threshold;
// 加載因子
final float loadFactor;
// HashMap結構修改次數,防止在遍歷時,有其他的線程在進行修改
transient volatile int modCount;
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: "
+ initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: "
+ loadFactor);

// Find a power of 2 >= initialCapacity
int capacity = 1;
// 使得capacity 的大小爲2的冪,至於爲什麼,請看下面
while (capacity < initialCapacity)
capacity <<= 1;

this.loadFactor = loadFactor;
threshold = (int) (capacity * loadFactor);
table = new Entry[capacity];
init();
}

下面是用於包裝key-value映射關係的Entry,它是HashMap的靜態內部類:
static class Entry<K, V> implements Map.Entry<K, V> {
final K key;
V value;
Entry<K, V> next;
final int hash;

/**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry<K, V> n) {
value = v;
next = n;
key = k;
hash = h;
}

public final K getKey() {
return key;
}

public final V getValue() {
return value;
}

public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}

public final boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry e = (Map.Entry) o;
Object k1 = getKey();
Object k2 = e.getKey();
if (k1 == k2 || (k1 != null && k1.equals(k2))) {
Object v1 = getValue();
Object v2 = e.getValue();
if (v1 == v2 || (v1 != null && v1.equals(v2)))
return true;
}
return false;
}

public final int hashCode() {
return (key == null ? 0 : key.hashCode())
^ (value == null ? 0 : value.hashCode());
}

public final String toString() {
return getKey() + "=" + getValue();
}

/**
* This method is invoked whenever the value in an entry is overwritten
* by an invocation of put(k,v) for a key k that's already in the
* HashMap.
*/
void recordAccess(HashMap<K, V> m) {
}

/**
* This method is invoked whenever the entry is removed from the table.
*/
void recordRemoval(HashMap<K, V> m) {
}
}

[color=red][b]二、下面我們看看HashMap的put和get及remove方法源碼,就知道爲什麼說它數據結構是鏈表和數組了[/b][/color]
  
// 根據key獲取value
public V get(Object key) {
if (key == null)
return getForNullKey();
//根據key的hashCode值計算它的hash碼
int hash = hash(key.hashCode());
//直接取出table數組中指定索引處的值
for (Entry<K, V> e = table[indexFor(hash, table.length)];
e != null;
//搜索該Entry鏈的下一個Entry
e = e.next) {
Object k;
//如果該Entry的key與被搜索key相同
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
}
return null;
}

private V getForNullKey() {
//key爲null,hash碼爲0,也就是說key爲null的Entry位於table[0]的Entry鏈上
for (Entry<K, V> e = table[0]; e != null; e = e.next) {
if (e.key == null)
return e.value;
}
return null;
}
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
//根據key的hashCode值計算它的hash碼
int hash = hash(key.hashCode());
//搜索指定hash值對應table中的索引值
int i = indexFor(hash, table.length);
for (Entry<K, V> e = table[i]; e != null; e = e.next) {
Object k;
//如果找到指定key與需要放入的key相等(hash值相同,通過equals比較返回true)
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
//新的值覆蓋舊值
e.value = value;
//這個方法是個空方法,可能是表示個標記,字面意思是表示記錄訪問
e.recordAccess(this);
//返回舊值
return oldValue;
}
}

modCount++;
//如果i處索引處的Entry爲null,表示此處還沒有Entry
//將key、value添加到i索引處
addEntry(hash, key, value, i);
return null;
}

//key=null的鍵值對,默認存放table[0]的Entry鏈
private V putForNullKey(V value) {
for (Entry<K, V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
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);
}
//根據鍵值移除key-value映射對象
public V remove(Object key) {
Entry<K, V> e = removeEntryForKey(key);
return (e == null ? null : e.value);
}

final Entry<K, V> removeEntryForKey(Object key) {
int hash = (key == null) ? 0 : hash(key.hashCode());
int i = indexFor(hash, table.length);
Entry<K, V> prev = table[i];
Entry<K, V> e = prev;

while (e != null) {
Entry<K, V> next = e.next;
Object k;
if (e.hash == hash
&& ((k = e.key) == key || (key != null && key.equals(k)))) {
modCount++;
size--;
if (prev == e)
table[i] = next;
else
prev.next = next;
//空方法,表示移除記錄
e.recordRemoval(this);
return e;
}
prev = e;
e = next;
}

return e;
}


[color=red][b]三、HashMap的hash算法和size大小調整,爲什麼說HashMap此類不保證映射的順序,特別是它不保證該順序恆久不變[/b][/color]請看以下分解:
  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);
}

/**
* Returns index for hash code h.
*/
// 根據hash碼求的數組小標並返回,當length爲2的冪時,h & (length-1)等價於h%(length-1),這裏也就是爲什麼前面說table的長度必須是2的冪
static int indexFor(int h, int length) {
return h & (length - 1);
}
// 調整大小
void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}

Entry[] newTable = new Entry[newCapacity];
transfer(newTable);
table = newTable;
threshold = (int) (newCapacity * loadFactor);
}

/**
* Transfers all entries from current table to newTable.
*/
void transfer(Entry[] newTable) {
Entry[] src = table;
int newCapacity = newTable.length;
for (int j = 0; j < src.length; j++) {
Entry<K, V> e = src[j];
if (e != null) {
src[j] = null;
do {
//注意這裏哈,HashMap不保證順序恆久不變
//在這裏可以找到答案
Entry<K, V> next = e.next;
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
} while (e != null);
}
}
}

[color=red][b]四、HashMap與Set的關係[/b][/color],Set代表一種集合元素無序、集合元素不可重複的集合。如果只考察HashMap中的key,不難發現集合中的key有一個特徵:所有的key不能重複,key之間無序。具備了Set的特徵,所有的key集合起來組成一個Set集合。同理所有的Entry集合起來,也是一個Set集合。而value是可以重複的,不能組成一個Set集合,在HashMap源代碼中提供了values()方法把value集合起來組成Collection集合。
 private abstract class HashIterator<E> implements Iterator<E> {
Entry<K, V> next; // next entry to return
int expectedModCount; // For fast-fail
int index; // current slot
Entry<K, V> current; // current entry

HashIterator() {
expectedModCount = modCount;
if (size > 0) { // advance to first entry
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
}

public final boolean hasNext() {
return next != null;
}

final Entry<K, V> nextEntry() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Entry<K, V> e = next;
if (e == null)
throw new NoSuchElementException();

if ((next = e.next) == null) {
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
current = e;
return e;
}

public void remove() {
if (current == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Object k = current.key;
current = null;
HashMap.this.removeEntryForKey(k);
expectedModCount = modCount;
}

}
private final class ValueIterator extends HashIterator<V> {
public V next() {
return nextEntry().value;
}
}

private final class KeyIterator extends HashIterator<K> {
public K next() {
return nextEntry().getKey();
}
}

private final class EntryIterator extends HashIterator<Map.Entry<K, V>> {
public Map.Entry<K, V> next() {
return nextEntry();
}
}
Iterator<K> newKeyIterator() {
return new KeyIterator();
}
Iterator<V> newValueIterator() {
return new ValueIterator();
}
Iterator<Map.Entry<K, V>> newEntryIterator() {
return new EntryIterator();
}
// Views

private transient Set<Map.Entry<K, V>> entrySet = null;
//把所有的key集合成Set集合
public Set<K> keySet() {
Set<K> ks = keySet;
return (ks != null ? ks : (keySet = new KeySet()));
}

private final class KeySet extends AbstractSet<K> {
public Iterator<K> iterator() {
return newKeyIterator();
}

public int size() {
return size;
}

public boolean contains(Object o) {
return containsKey(o);
}

public boolean remove(Object o) {
return HashMap.this.removeEntryForKey(o) != null;
}

public void clear() {
HashMap.this.clear();
}
}
//把所有的values集合成Collection集合
public Collection<V> values() {
Collection<V> vs = values;
return (vs != null ? vs : (values = new Values()));
}

private final class Values extends AbstractCollection<V> {
public Iterator<V> iterator() {
return newValueIterator();
}
public int size() {
return size;
}
public boolean contains(Object o) {
return containsValue(o);
}

public void clear() {
HashMap.this.clear();
}
}
//把所有的Entry對象集合成Set集合
public Set<Map.Entry<K, V>> entrySet() {
return entrySet0();
}

private Set<Map.Entry<K, V>> entrySet0() {
Set<Map.Entry<K, V>> es = entrySet;
return es != null ? es : (entrySet = new EntrySet());
}

private final class EntrySet extends AbstractSet<Map.Entry<K, V>> {
public Iterator<Map.Entry<K, V>> iterator() {
return newEntryIterator();
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<K, V> e = (Map.Entry<K, V>) o;
Entry<K, V> candidate = getEntry(e.getKey());
return candidate != null && candidate.equals(e);
}
public boolean remove(Object o) {
return removeMapping(o) != null;
}
public int size() {
return size;
}
public void clear() {
HashMap.this.clear();
}
}

[color=red][b]五、fail-fast策略(速錯)[/b][/color]HashMap不是線程安全的,因此如果在使用迭代器的過程中有其他線程修改了map,那麼將拋ConcurrentModificationException,這就是所謂fail-fast策略(速錯),這一策略在源碼中的實現是通過modCount域,modCount顧名思義就是修改次數,對HashMap內容的修改都將增加這個值,那麼在迭代器初始化過程中會將這個值賦給迭代器的expectedModCount。[color=red]在迭代過程中,判斷modCount跟expectedModCount是否相等,如果不相等就表示已經有其他線程修改了[/color]
private abstract class HashIterator<E> implements Iterator<E> {
Entry<K, V> next; // next entry to return
int expectedModCount; // For fast-fail
int index; // current slot
Entry<K, V> current; // current entry

HashIterator() {
expectedModCount = modCount;
if (size > 0) { // advance to first entry
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
}

public final boolean hasNext() {
return next != null;
}

final Entry<K, V> nextEntry() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Entry<K, V> e = next;
if (e == null)
throw new NoSuchElementException();

if ((next = e.next) == null) {
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
current = e;
return e;
}

public void remove() {
if (current == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Object k = current.key;
current = null;
HashMap.this.removeEntryForKey(k);
expectedModCount = modCount;
}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章