标签:
01.private transient Entry[] table; // Entry数组
01.private static class Entry<K,V> implements Map.Entry<K,V> { 02. int hash; 03. K key; 04. V value; 05. Entry<K,V> next; // Entry此处表明是个单链表 06. ... 07.}
01.public Hashtable(int initialCapacity, float loadFactor) { 02.... 03.} 04.public Hashtable() { 05. this(11, 0.75f); 06.}
01.public synchronized V put(K key, V value) { 02. // Make sure the value is not null 03. if (value == null) { 04. throw new NullPointerException(); 05. } 06. 07. // Makes sure the key is not already in the hashtable. 08. Entry tab[] = table; 09. int hash = key.hashCode(); 10. int index = (hash & 0x7FFFFFFF) % tab.length; 11. for (Entry<K, V> e = tab[index]; e != null; e = e.next) { 12. if ((e.hash == hash) && e.key.equals(key)) { 13. V old = e.value; 14. e.value = value; 15. return old; 16. } 17. } 18.}
02. if (key == null) 03. return putForNullKey(value); 04. int hash = hash(key.hashCode()); 05. int i = indexFor(hash, table.length); 06. for (Entry<K, V> e = table[i]; e != null; e = e.next) { 07. Object k; 08. if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { 09. V oldValue = e.value; 10. e.value = value; 11. e.recordAccess(this); 12. return oldValue; 13. } 14. } 15. 16. modCount++; 17. addEntry(hash, key, value, i); 18. return null; 19.} 20. 21. 22./** 23. * Applies a supplemental hash function to a given hashCode, which 24. * defends against poor quality hash functions. This is critical 25. * because HashMap uses power-of-two length hash tables, that 26. * otherwise encounter collisions for hashCodes that do not differ 27. * in lower bits. Note: Null keys always map to hash 0, thus index 0. 28. */ 29.static int hash(int h) { 30. // This function ensures that hashCodes that differ only by 31. // constant multiples at each bit position have a bounded 32. // number of collisions (approximately 8 at default load factor). 33. h ^= (h >>> 20) ^ (h >>> 12); 34. return h ^ (h >>> 7) ^ (h >>> 4); 35.} 36. 37./** 38. * Returns index for hash code h. 39. */ 40.static int indexFor(int h, int length) { 41. return h & (length-1); 42.}
01./** the load above which rehashing occurs. */ 02.public static final float DEFAULT_LOAD_FACTOR = 0.5f; 03. 04.protected int setUp( int initialCapacity ) { 05. int capacity; 06. capacity = PrimeFinder.nextPrime( initialCapacity ); 07. computeMaxSize( capacity ); 08. computeNextAutoCompactionAmount( initialCapacity ); 09. return capacity; 10.}
01.public V put(K key, V value) { 02. // insertKey() inserts the key if a slot if found and returns the index 03. int index = insertKey(key); 04. return doPut(value, index); 05.} 06. 07. 08.protected int insertKey(T key) { 09. consumeFreeSlot = false; 10. 11. if (key == null) 12. return insertKeyForNull(); 13. 14. final int hash = hash(key) & 0x7fffffff; 15. int index = hash % _set.length; 16. Object cur = _set[index]; 17. 18. if (cur == FREE) { 19. consumeFreeSlot = true; 20. _set[index] = key; // insert value 21. return index; // empty, all done 22. } 23. 24. if (cur == key || equals(key, cur)) { 25. return -index - 1; // already stored 26. } 27. 28. return insertKeyRehash(key, index, hash, cur); 29.}
01.if (map._entryCount + map._nullCount > (entries.length >> 1)) { // Table more than half empty. 02. map.resizeTable(_isShared); 03.}
01.private final Object put(Object key, Object value, int keyHash, 02. boolean concurrent, boolean noReplace, boolean returnEntry) { 03. final FastMap map = getSubMap(keyHash); 04. final Entry[] entries = map._entries; // Atomic. 05. final int mask = entries.length - 1; 06. int slot = -1; 07. for (int i = keyHash >> map._keyShift;; i++) { 08. Entry entry = entries[i & mask]; 09. if (entry == null) { 10. slot = slot < 0 ? i & mask : slot; 11. break; 12. } else if (entry == Entry.NULL) { 13. slot = slot < 0 ? i & mask : slot; 14. } else if ((key == entry._key) || ((keyHash == entry._keyHash) && (_isDirectKeyComparator ? key.equals(entry._key) 15. : _keyComparator.areEqual(key, entry._key)))) { 16. if (noReplace) { 17. return returnEntry ? entry : entry._value; 18. } 19. Object prevValue = entry._value; 20. entry._value = value; 21. return returnEntry ? entry : prevValue; 22. } 23. } 24. ... 25.}
标签:
原文地址:http://www.cnblogs.com/zhoudi/p/5539516.html