基础数据机构之WeakHashMap源码分析
2017-12-15 15:51
351 查看
1.WeakHashMap 在HashMap实现上,在HashMap上增加Entry的SoftReference软引用功能
源码分析
Entry实体类继承WeakReference
插入数据方式和HashMap插入数据方法一致,只是多了对WeakReference的null判断逻辑
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for this key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated.
* @param value value to be associated with the specified key.
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key, V value) {
K k = (K) maskNull(key); //如果该key为null,那么直接使用NULL_KEY作为填充
int h = HashMap.hash(k.hashCode());
Entry[] 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,V>(k, value, queue, h, e);
if (++size >= threshold)
resize(tab.length * 2);
return null;
}
源码分析
Entry实体类继承WeakReference
/** * The entries in this hash table extend WeakReference, using its main ref * field as the key. */ private static class Entry<K,V> extends WeakReference<K> implements Map.Entry<K,V> {//继承WeakReference弱引用 private V value; private final int hash; private Entry<K,V> next; /** * Creates new entry. */ Entry(K key, V value,ReferenceQueue<K> queue,int hash, Entry<K,V> next) { super(key, queue); this.value = value; this.hash = hash; this.next = next; }
public K getKey() { return WeakHashMap.<K>unmaskNull(get()); //get()方法,获取该key的时候,判断是否被回收掉,如果被回收掉返回null }}
插入数据方式和HashMap插入数据方法一致,只是多了对WeakReference的null判断逻辑
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for this key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated.
* @param value value to be associated with the specified key.
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key, V value) {
K k = (K) maskNull(key); //如果该key为null,那么直接使用NULL_KEY作为填充
int h = HashMap.hash(k.hashCode());
Entry[] 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,V>(k, value, queue, h, e);
if (++size >= threshold)
resize(tab.length * 2);
return null;
}
相关文章推荐
- 基础数据机构之ArrayDeque队列源码分析
- 基础数据机构之Stack栈源码分析
- Java集合框架之Map--IdentityHashMap,WeakHashMap,EnumMap源码分析
- 【Java】Java基础知识之HashMap源码分析(Java8)
- nginx源码分析2———基础数据结构二(链表和双向链表)
- java核心基础--jdk源码分析学习--基本数据类型
- 基础数据结构之TreeMap源码分析
- MySQL系列:innodb源码分析之基础数据结构
- Java-WeakHashMap源码分析及示例
- 数据结构基础:哈希表(HashMap)原理分析
- Map子类重要源码分析对比&HashMap&HashTable&TreeMap&IdentityHashMap&WeakHashMap
- 【深入Java基础】HashMap源码分析(二)
- [Java数据结构]从源码分析HashMap
- java基础提高篇--集合源码分析--jdk1.8 HashMap源码
- android系统源码分析——binder基础数据结构
- Java基础——HashMap源码分析
- nginx源码分析2———基础数据结构五(ngx_hash_wildcard_t)
- nginx源码分析2———基础数据结构三(内存池)
- java核心基础--jdk源码分析学习--HashMap
- 基础数据结构之LinkedHashMap源码分析