AbstractMap:
數據結構:
Entry<K,V> 是 Map接口內部的一個接口,在具體的實現類中會被實現成不同靜態內部類,他們有不同的的鍵值對結構.
Set<K> keySet();
Collection<V> values();
transient volatile Set<K> keySet = null;
transient volatile AbstractCollection<V> values = null;
keySet,values被設置為第一次指向的對象.以后使用該對象通過public Set<K> keySet()和public Collection<V> values()方法獲得,
從中也知道了鍵在集合中是以Set形式存在,值是以Collection形式存在,Entry<K,V>也是以Set形式存在,當然它們本身都是有各自的類型。
抽象方法:
1 public abstract Set<Entry<K,V>> entrySet(); 2 //下面的兩個方法都是依靠了這個抽象方法進行的編程。 3 private Iterator<Entry<K,V>> i = entrySet().iterator();//返回的是實現類中內部類HashIterator這個對象。 4 public Set<K> keySet() { 5 if (keySet == null) { 6 keySet = new AbstractSet<K>() { 7 public Iterator<K> iterator() { 8 return new Iterator<K>() { 9 10 private Iterator<Entry<K,V>> i = entrySet().iterator(); 11 12 public boolean hasNext() { 13 return i.hasNext(); 14 } 15 16 public K next() { 17 return i.next().getKey(); 18 } 19 20 public void remove() { 21 i.remove(); 22 } 23 24 }; 25 } 26 27 public int size() { 28 return AbstractMap.this.size(); 29 } 30 31 public boolean isEmpty() { 32 return AbstractMap.this.isEmpty(); 33 } 34 35 public void clear() { 36 AbstractMap.this.clear(); 37 } 38 39 public boolean contains(Object k) { 40 return AbstractMap.this.containsKey(k); 41 } 42 }; 43 } 44 return keySet; 45 } 46 public Collection<V> values() { 47 if (values == null) { 48 values = new AbstractCollection<V>() { 49 public Iterator<V> iterator() { 50 return new Iterator<V>() { 51 private Iterator<Entry<K,V>> i = entrySet().iterator(); 52 53 public boolean hasNext() { 54 return i.hasNext(); 55 } 56 57 public V next() { 58 return i.next().getValue(); 59 } 60 61 public void remove() { 62 i.remove(); 63 } 64 }; 65 } 66 67 public int size() { 68 return AbstractMap.this.size(); 69 } 70 71 public boolean isEmpty() { 72 return AbstractMap.this.isEmpty(); 73 } 74 75 public void clear() { 76 AbstractMap.this.clear(); 77 } 78 79 public boolean contains(Object v) { 80 return AbstractMap.this.containsValue(v); 81 } 82 83 84 }; 85 } 86 return values; 87 }
方法:
1 public int size(); 2 public boolean isEmpty(); 3 public boolean containsValue(Object value); 4 public boolean containsKey(); 5 public abstract Set<Entry<K,V>> entrySet(); 6 //------------------------------------ 7 public V get(Object key); 8 //存入key返回的是對應的值。 9 //----------------------------------- 10 public V put(K key, V value); 11 //必須要重寫不然直接拋出UnsupportedOperationException()異常。 12 //---------------------------------- 13 public V remove(Object key); 14 public void putAll; 15 public void clear(); 16 //------------------------------------------------- 17 public boolean equals(Object o); 18 //判斷兩個map是否相等,原理是把a中的key放到b的get()取得的value和a的value進行比較,一項不滿足就返回false; 19 //------------------------------------------------- 20 public int hashCode(); 21 //把每個元素的hashCode()加起來返回:h += i.next().hashCode(); 22 //--------------------------------------- 23 public String toString(); 24 clone(); 25 //以上方法大多面向抽象的編程,很多函數里都有entrySet().iterator();
HashMap:
構造方法:
1 public HashMap() { 2 this.loadFactor =DEFAULT_LOAD_FACTOR;// all otherfields defaulted 3 } 4 public HashMap(intinitialCapacity) { 5 this(initialCapacity,DEFAULT_LOAD_FACTOR); 6 } 7 8 public HashMap(intinitialCapacity,floatloadFactor) { 9 10 } 11 public HashMap(Map<?extends K, ?extends V>m) { 12 13 }
1. capacity,表示的是HashMap中桶的數量,初始化容量initCapacity為16,第一次擴容會擴到64,之后每次擴容都是之前容量的2倍,所以容量每次都是2的次冪;
2. loadFactor,裝載因子,衡量HashMap一個滿的程度,初始化為0.75,實時裝載因子是size/capacity;
3. threshold,HashMap擴容的一個標准,每當size大於這個標准時就會進行擴容操作,threshold等於capacity*loadFactor;
數據結構:
static final Entry<?,?>[] EMPTY_TABLE = {}; transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
集合特性:
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(); } }
迭代器:
1 private abstract class HashIterator<E> implements Iterator<E> { 2 Entry<K,V> next; // next entry to return 3 int expectedModCount; // For fast-fail 4 int index; // current slot 5 Entry<K,V> current; // current entry 6 7 HashIterator() { 8 expectedModCount = modCount; 9 if (size > 0) { // advance to first entry 10 Entry[] t = table; 11 while (index < t.length && (next = t[index++]) == null) 12 ; 13 } 14 } 15 16 public final boolean hasNext() { 17 return next != null; 18 } 19 20 final Entry<K,V> nextEntry() { 21 if (modCount != expectedModCount) 22 throw new ConcurrentModificationException(); 23 Entry<K,V> e = next; 24 if (e == null) 25 throw new NoSuchElementException(); 26 27 if ((next = e.next) == null) { 28 Entry[] t = table; 29 while (index < t.length && (next = t[index++]) == null) 30 ; 31 } 32 current = e; 33 return e; 34 } 35 36 public void remove() { 37 if (current == null) 38 throw new IllegalStateException(); 39 if (modCount != expectedModCount) 40 throw new ConcurrentModificationException(); 41 Object k = current.key; 42 current = null; 43 HashMap.this.removeEntryForKey(k); 44 expectedModCount = modCount; 45 } 46 }
HashIterator<Map.Entry<K,V>>是HashMap的內部類,其為抽象類,它沒有實現next()方法,但是卻添加了具有next()功能的函數nextEntry()。
while (index < t.length && (next = t[index++]) == null);
HashMap迭代的精髓就這句話。同樣說明了HashMap不能保證元素的順序。
HashMap的迭代器實現分成3種:
1 Iterator<V> newValueIterator() { 2 return new ValueIterator(); 3 } 4 private final class ValueIterator extends HashIterator<V> { 5 public V next() { 6 return nextEntry().value; 7 } 8 } 9 10 Iterator<K> newKeyIterator() { 11 return new KeyIterator(); 12 } 13 private final class KeyIterator extends HashIterator<K> { 14 public K next() { 15 return nextEntry().getKey(); 16 } 17 } 18 19 Iterator<Map.Entry<K,V>> newEntryIterator() { 20 return new EntryIterator(); 21 } 22 private final class EntryIterator extends HashIterator<Map.Entry<K,V>> { 23 public Map.Entry<K,V> next() { 24 return nextEntry(); 25 } 26 }
Map元素的迭代其實是其內部接口Entry<K,V>的實現的迭代,所以要迭代Map需要使用entrySet()方法取得Set<Entry<K,V>>類型的對象然后取得該對象的迭代器。
三個內部類中Values KeySet EntrySet。反而是EntrySet的迭代速度最快。

1 package test; 2 3 import java.util.Collection; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.Map; 7 import java.util.Map.Entry; 8 9 public class MapTest { 10 11 public static <K> void main(String[] args) { 12 13 Map<String,String> hashMap = new HashMap<String, String>(); 14 15 for(int i=0;i<100000;i++) 16 hashMap.put(i+"", i+"v"); 17 18 long time = System.currentTimeMillis(); 19 20 time = System.currentTimeMillis(); 21 System.out.println("==============方式2:通過遍歷values()遍歷HashMap的value"); 22 Collection<String> values = hashMap.values(); 23 for(Iterator<String> valIt = values.iterator();valIt.hasNext();){ 24 valIt.next(); 25 } 26 System.out.println("用時:"+(System.currentTimeMillis() - time)); 27 28 System.out.println("==============方式1:通過遍歷keySet()遍歷HashMap的value"); 29 Iterator<String> it = hashMap.keySet().iterator(); 30 while(it.hasNext()){ 31 it.next(); 32 //System.out.println(hashMap.get(it.next())); 33 } 34 System.out.println("用時:"+(System.currentTimeMillis() - time)); 35 36 time = System.currentTimeMillis(); 37 System.out.println("==============方式3:通過entrySet().iterator()遍歷HashMap的key和映射的value"); 38 Iterator<Entry<String, String>> entryIt = hashMap.entrySet().iterator(); 39 while(entryIt.hasNext()){ 40 entryIt.next().getKey(); 41 entryIt.next().getValue(); 42 //System.out.println("key:"+entry.getKey()+" value:"+entry.getValue()); 43 } 44 System.out.println("用時:"+(System.currentTimeMillis() - time)); 45 } 46 47 }
函數:
final int hash(Object k); static int indexFor(int h, int length);
見JAVA API HashMap hash()函數解析.
private V getForNullKey(); private V getForNullKey() { if (size == 0) { return null; } for (Entry<K,V> e = table[0]; e != null; e = e.next) { if (e.key == null) return e.value; } return null; }
說明當key是null的時候會調用該函數,可以看到默認遍歷的是table[0]的位置,默認key==null的hashCode為0.
1 public V put(K key, V value); 2 public V put(K key, V value) { 3 if (table == EMPTY_TABLE) { 4 inflateTable(threshold); 5 } 6 if (key == null) 7 return putForNullKey(value); 8 int hash = hash(key); 9 int i = indexFor(hash, table.length); 10 //Key的位置已經有舊的值了 則把新值給舊值 並返回舊值 11 for (Entry<K,V> e = table[i]; e != null; e = e.next) { 12 Object k; 13 if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { 14 V oldValue = e.value; 15 e.value = value; 16 e.recordAccess(this); 17 return oldValue; 18 } 19 } 20 21 modCount++; 22 //如果Key是新的則調用addEntry插入新的Key和value。 23 addEntry(hash, key, value, i); 24 return null; 25 }
put函數保證了EntrySet集合中元素的唯一性。
((k = e.key) == key || key.equals(k))可見即使對象不同只要兩者的值相同就行,前提是重寫了toString().
從上面代碼我們同樣可以看出,不同的key可能散列出形同的hashCode,不同的hashCode可能定位到相同的table[i],所以在比較的時候要要同時判斷hashCode和key
如果遍歷完鏈表還沒有找到進入if語句的元素,說明可能發生了上面不同的key定位到了相同的table[i]的位置,所以就把這個新元素添加到表頭。
1 void createEntry(int hash, K key, V value, int bucketIndex) { 2 Entry<K,V> e = table[bucketIndex]; 3 table[bucketIndex] = new Entry<>(hash, key, value, e); 4 size++; 5 }
當中的形式參數e被賦值給了next,意識就是新的頭節點指向了舊的頭節點。
Entry(int h, K k, V v, Entry<K,V> n) { value = v; next = n; key = k; hash = h; }
在這里String對象是特殊的,在hash()函數中有。
if (0 != h && k instanceof String) { return sun.misc.Hashing.stringHash32((String) k); }
即使是不同的String對象只要值是相同的就認為是相同的key。