一 特性
- 存储的内容也是键值对,可以为null
- 当其中某个键不再使用的时候,会被GC回收掉,也就是其映射关系不能影响垃圾回收器的丢弃
- 通过WeakReference 和ReferenceQueue 实现的,队列中会保存被GC回收的弱键
- 非同步的,但是可以使用Collections.synchronizedMap来创造同步的
-
二 属性
/*** The default initial capacity -- MUST be a power of two. 默认大小*/private static final int DEFAULT_INITIAL_CAPACITY = 16;/*** The maximum capacity, used if a higher value is implicitly specified* by either of the constructors with arguments.* MUST be a power of two <= 1<<30.*/private static final int MAXIMUM_CAPACITY = 1 << 30;/*** The load factor used when none specified in constructor. 负载因子*/private static final float DEFAULT_LOAD_FACTOR = 0.75f;/*** The table, resized as necessary. Length MUST Always be a power of two. 2的整数幂的大小的数组*/Entry<K,V>[] table;/*** The number of key-value mappings contained in this weak hash map. 实际存储的大小*/private int size;/*** The next size value at which to resize (capacity * load factor). 当容量到达这个值的时候会进行扩容*/private int threshold;/*** The load factor for the hash table.*/private final float loadFactor;/*** Reference queue for cleared WeakEntries*/private final ReferenceQueue<Object> queue = new ReferenceQueue<>();/*** The number of times this WeakHashMap has been structurally modified.* Structural modifications are those that change the number of* mappings in the map or otherwise modify its internal structure* (e.g., rehash). This field is used to make iterators on* Collection-views of the map fail-fast.** @see ConcurrentModificationException*/int modCount;
三 重要方法
hash
- 会获取键的hashCode 然后通过一些操作获得对
- indexFor
- 获得给定hashcode的位置 h & (length-1) 经过处理的hash码 按位与 长度减1
resize
-
四 添加
public V put(K key, V value) {Object k = maskNull(key);int h = hash(k);Entry<K,V>[] tab = getTable();//获得对应的位置int i = indexFor(h, tab.length);//循环对应位置的链表,如果已经存在则替换for (Entry<K,V> e = tab[i]; e != null; e = e.next) {if (h == e.hash && eq(k, e.get())) {V oldValue = e.value;if (value != oldValue)e.value = value;return oldValue;}}modCount++;Entry<K,V> e = tab[i];//这里注意是采用的头插法tab[i] = new Entry<>(k, value, queue, h, e);if (++size >= threshold)resize(tab.length * 2);return null;}
五 删除
public V remove(Object key) {Object k = maskNull(key);int h = hash(k);Entry<K,V>[] tab = getTable();int i = indexFor(h, tab.length);Entry<K,V> prev = tab[i];Entry<K,V> e = prev;while (e != null) {Entry<K,V> next = e.next;if (h == e.hash && eq(k, e.get())) {modCount++;size--;if (prev == e)tab[i] = next;elseprev.next = next;return e.value;}prev = e;e = next;}return null;}
六 获取
public V get(Object key) {Object k = maskNull(key);int h = hash(k);Entry<K,V>[] tab = getTable();int index = indexFor(h, tab.length);Entry<K,V> e = tab[index];while (e != null) {if (e.hash == h && eq(k, e.get()))return e.value;e = e.next;}return null;}
七 hash 扰乱函数
final int hash(Object k) {int h = k.hashCode();// 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).// 先无符号右移20位 然后在异或 无符号右移12位 在于hash进行异或h ^= (h >>> 20) ^ (h >>> 12);//再无符号右移 7 和4return h ^ (h >>> 7) ^ (h >>> 4);}
八 resize扩容
```java //put 调用的时候传参是 数组的2倍大小
// 注意这个在删除元素的时候没有进行调用 void resize(int newCapacity) { Entry[] oldTable = getTable(); int oldCapacity = oldTable.length; if (oldCapacity == MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE;return;
}
Entry
[] newTable = newTable(newCapacity); transfer(oldTable, newTable); table = newTable; /*
* If ignoring null elements and processing ref queue caused massive* shrinkage, then restore old table. This should be rare, but avoids* unbounded expansion of garbage-filled tables.*/
if (size >= threshold / 2) {
threshold = (int)(newCapacity * loadFactor);
} else {
expungeStaleEntries();transfer(newTable, oldTable);table = oldTable;
} }
-
private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest) {for (int j = 0; j < src.length; ++j) {Entry<K,V> e = src[j];src[j] = null;while (e != null) {Entry<K,V> next = e.next;Object key = e.get();if (key == null) {e.next = null; // Help GCe.value = null; // " "size--;} else {//注意这里会使用新的长度为每个元素重新计算hash槽int i = indexFor(e.hash, dest.length);e.next = dest[i];dest[i] = e;}e = next;}}}
```
