JAVA HASHMAP議圻尖蛍裂
扮寂:2011-04-19 鴬人坩 Christmas
珊頁栖屁悶心匯和HashMap議潤更杏. 泌和夕侭幣(夕短鮫挫),圭崇旗燕Hash涌,蓉夕旗燕 涌坪議圷殆,壓宸戦祥頁Key-value斤侭怏撹Map.Entry斤?.
泌惚嗤謹倖圷沫瓜Hash痕方協了欺揖匯倖涌坪,厘断各岻葎hash喝融,涌坪議圷殆怏撹汽? 全燕.斑厘断心匯和hashMap JDK坿鷹(咀鐙嫌購狼,評茅阻何蛍旗鷹嚥廣瞥,湖佶辛參臥心 JDK1.6坿鷹):
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
{
static final int DEFAULT_INITIAL_CAPACITY = 16;
static final int MAXIMUM_CAPACITY = 1 << 30;
static final float DEFAULT_LOAD_FACTOR = 0.75f;
transient Entry[] table;
transient int size;
int threshold;
final float loadFactor;
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;
while (capacity < initialCapacity)
capacity <<= 1;
this.loadFactor = loadFactor;
threshold = (int)(capacity * loadFactor);
table = new Entry[capacity];
init();
}
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;
}
private V getForNullKey() {
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 ==
|