1、開始 public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable 繼承了抽象類AbstractMap,實現了Map接口,Cloneable接口(可克隆),Serializable接口(可序列化) 2、屬性 //默認初始化容量爲16,容量必須是2的n次冪 static final int DEFAULT_INITIAL_CAPACITY = 16; //最大容量爲2的20次冪,再大就是Integer.MAX_VALUE static final int MAXIMUM_CAPACITY = 1 << 30; //默認加載因子爲0.75 static final float DEFAULT_LOAD_FACTOR = 0.75f; //Entry數組,其就是鏈表散列的數據結構,即數組+鏈表 transient Entry<K,V>[] table; //已存儲元素的數量 transient int size; //擴容的臨界值,只要存儲元素的數量大於該臨界值,就會自動擴容,其中threshold=capacity*load_factor int threshold; //加載因子 final float loadFactor; //更改次數 transient int modCount; 3、存儲數據結構Entry static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; Entry<K,V> next; int hash; 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(); } //當向HashMap添加元素時調用該方法 void recordAccess(HashMap<K,V> m) { } //當從HashMap中刪除元素時調用該方法 void recordRemoval(HashMap<K,V> m) { } } 4、構造器 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); //獲取容量大小,使之是2的n次冪 // Find a power of 2 >= initialCapacity int capacity = 1; while (capacity < initialCapacity) capacity <<= 1; //賦值 //加載因子 this.loadFactor = loadFactor; //擴容的臨界值 threshold = (int)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1); //存儲元素的數組 table = new Entry[capacity]; //用於元素計算Hash值,定位元素在數組中的位置 useAltHashing = sun.misc.VM.isBooted() && (capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD); //初始化時的一些其餘操做 init(); } //指定初始容量 public HashMap(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR); } //使用默認容量和默認加載因子 public HashMap() { this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); } //使用現有元素,和默認加載因子 public HashMap(Map<? extends K, ? extends V> m) { this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1, DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR); putAllForCreate(m); } 5、添加 public V put(K key, V value) { //當key爲null時,存儲在數組的第0個位置 if (key == null) return putForNullKey(value); //計算HashCode值 int hash = hash(key); //定位在數組中的位置,即肯定該元素所在的鏈表 int i = indexFor(hash, table.length); for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; //對比hash值以及Key是否相等 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位置的鏈表中新增一節點 addEntry(hash, key, value, i); return null; } private V putForNullKey(V value) { //key爲null,其必在HashMap的第0個數組中 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++; //新增,hash=0,bucketIndex=0 addEntry(0, null, value, 0); return null; } void addEntry(int hash, K key, V value, int bucketIndex) { //當前元素數量已經達到擴容臨界點,則進行擴容 if ((size >= threshold) && (null != table[bucketIndex])) { //擴容,爲原來的2倍 resize(2 * table.length); //從新計算當前key的hash值, hash = (null != key) ? hash(key) : 0; //以及在數組中的位置 bucketIndex = indexFor(hash, table.length); } //爲鏈表添加一新節點 createEntry(hash, key, value, bucketIndex); } void createEntry(int hash, K key, V value, int bucketIndex) { //使用前插法,即新插入的元素一定在鏈表的頭部 Entry<K,V> e = table[bucketIndex]; table[bucketIndex] = new Entry<>(hash, key, value, e); //元素數量加1 size++; } 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]; boolean oldAltHashing = useAltHashing; useAltHashing |= sun.misc.VM.isBooted() && (newCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD); //是否進行hash值重算 boolean rehash = oldAltHashing ^ useAltHashing; //數組元素遷移到新數組中 transfer(newTable, rehash); //最後將新數組引用賦給HashMap table = newTable; //從新計算擴容臨界值 threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1); } void transfer(Entry[] newTable, boolean rehash) { //新數組長度 int newCapacity = newTable.length; for (Entry<K,V> e : table) { //遍歷數組中的每個元素,即每個鏈表 while(null != e) { //將每個鏈表遷移到新數組中 //記錄當前節點的下一個節點,用於下次遷移 Entry<K,V> next = e.next; if (rehash) { //是否對當前節點的hash重計算 e.hash = null == e.key ? 0 : hash(e.key); } //從新定位該元素在新數組中的位置 int i = indexFor(e.hash, newCapacity); //將新數組中該位置的鏈表元素都放在該元素後面,使用的是前插法 e.next = newTable[i]; //將鏈表掛在新數組中 newTable[i] = e; //繼續下一個節點的遷移 e = next; } } } 6、刪除 public V remove(Object key) { //獲取須要刪除的key對應的元素 Entry<K,V> e = removeEntryForKey(key); //返回該元素上的值 return (e == null ? null : e.value); } final Entry<K,V> removeEntryForKey(Object key) { //計算該key的hash int hash = (key == null) ? 0 : hash(key); //定位該key的在數組中的位置 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)))) { //若當前節點是所要刪除的節點,即hash相等和key也相等 //更改次數 modCount++; //元素數量減1 size--; if (prev == e) //當要刪除的節點是鏈表的頭結點時,則只須要將當前節點的下一個節點做爲該鏈表的頭結點,便可 table[i] = next; else //當刪除的節點不是鏈表的頭結點時,則只須要當前節點的前一個節點的下一個是當前節點的下一個節點,便可 prev.next = next; //刪除記錄 e.recordRemoval(this); //返回刪除的節點 return e; } prev = e; e = next; } //返回要刪除的節點 return e; } 7、訪問和查找 //根據key查找元素 public V get(Object key) { if (key == null) //當key爲null時,從數組的第0個位置查找 return getForNullKey(); //根據key查找 Entry<K,V> entry = getEntry(key); return null == entry ? null : entry.getValue(); } private V getForNullKey() { //從數組的第0個位置的鏈表頭部開始查找 for (Entry<K,V> e = table[0]; e != null; e = e.next) { //當存在一個key爲null的節點時 if (e.key == null) //返回該節點中的值 return e.value; } return null; } //是否包含鍵 public boolean containsKey(Object key) { return getEntry(key) != null; } final Entry<K,V> getEntry(Object key) { //計算該key對應的hash int hash = (key == null) ? 0 : hash(key); //定位在數組中的位置,即找到某張鏈表,以後從該鏈表的頭部開始遍歷 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)))) //當hash相等,key也想等時,返回該節點 return e; } return null; } //是否包含值 public boolean containsValue(Object value) { if (value == null) //當值爲null時 return containsNullValue(); Entry[] tab = table; //遍歷當前數組,以及每個鏈表 for (int i = 0; i < tab.length ; i++) for (Entry e = tab[i] ; e != null ; e = e.next) if (value.equals(e.value)) return true; return false; } private boolean containsNullValue() { //值爲null Entry[] tab = table; //遍歷當前數組,以及每個鏈表 for (int i = 0; i < tab.length ; i++) for (Entry e = tab[i] ; e != null ; e = e.next) if (e.value == null) return true; return false; } 8、迭代器 //用於HashMap的迭代器抽象類 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(); } } //Entry迭代器類 private final class EntryIterator extends HashIterator<Map.Entry<K,V>> { public Map.Entry<K,V> next() { return nextEntry(); } } //獲取鍵迭代器對象 // Subclass overrides these to alter behavior of views' iterator() method Iterator<K> newKeyIterator() { return new KeyIterator(); } //獲取值迭代器對象 Iterator<V> newValueIterator() { return new ValueIterator(); } //獲取Entry迭代器類對象 Iterator<Map.Entry<K,V>> newEntryIterator() { return new EntryIterator(); } private transient Set<Map.Entry<K,V>> entrySet = null; transient volatile Set<K> keySet = null; transient volatile Collection<V> values = null; //獲取HashMap中的全部鍵對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(); } //Set大小 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(); } } //獲取HashMap中的全部值對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(); } //Collection大小 public int size() { return size; } //是否包含 public boolean contains(Object o) { return containsValue(o); } //清空 public void clear() { HashMap.this.clear(); } } //獲取HashMap中的全部鍵值對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; } //Set大小 public int size() { return size; } //清空 public void clear() { HashMap.this.clear(); } } 9、清空全部元素 public void clear() { modCount++; Entry[] tab = table; for (int i = 0; i < tab.length; i++) tab[i] = null; size = 0; } 參考資料: http://www.cnblogs.com/tstd/p/5055286.html http://tengj.top/2016/04/15/javajh3hashmap/