1.定义变量accessOrder
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V> {
final boolean accessOrder;
public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) {
super(initialCapacity, loadFactor);
this.accessOrder = accessOrder;
}
}
2.最近访问的节点移动到链表末尾
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V> {
void afterNodeAccess(Node<K,V> e) { // move node to last
LinkedHashMapEntry<K,V> last;
if (accessOrder && (last = tail) != e) { //e不是tail
LinkedHashMapEntry<K,V> p =
(LinkedHashMapEntry<K,V>)e, b = p.before, a = p.after;
p.after = null;
if (b == null){ //原来p是head,p被移到最后,那head就变成p.after
head = a;
} else{
b.after = a; //b p a 中间p被移走,b和a需要串起来
}
if (a != null){ //b p a 中间p被移走,b和a需要串起来
a.before = b;
} else{ //原来a为null,说明p为tail(这个场景不是不会走进来吗?)
last = b;
}
if (last == null){ //last为null说明链表只有一个元素,更新head
head = p;
}else {
p.before = last; //last p
last.after = p;
}
tail = p; //更新尾
++modCount;
}
}
}
3.LruCache
3.1.LruCache.put
public class LruCache<T, Y> {
@Nullable
public synchronized Y put(@NonNull T key, @Nullable Y item) {
final int itemSize = getSize(item);
if (itemSize >= maxSize) {
onItemEvicted(key, item);
return null;
}
if (item != null) {
currentSize += itemSize;
}
@Nullable Entry<Y> old = cache.put(key, item == null ? null
: new Entry<>(item, itemSize));
if (old != null) {
currentSize -= old.size;
if (!old.value.equals(item)) {
onItemEvicted(key, old.value);
}
}
evict(); //看放入之后,大小是否超过
return old != null ? old.value : null;
}
}
3.2.LruCache.evict
public class LruCache<T, Y> {
private void evict() {
trimToSize(maxSize);
}
}
3.3.LruCache.trimToSize
循环移除,直到size小于maxSize,每次移除链表头元素
public class LruCache<T, Y> {
protected synchronized void trimToSize(long size) {
Map.Entry<T, Entry<Y>> last;
Iterator<Map.Entry<T, Entry<Y>>> cacheIterator;
while (currentSize > size) {
cacheIterator = cache.entrySet().iterator(); //LinkedHashMap遍历
last = cacheIterator.next(); //获取的是链表的第一个元素
final Entry<Y> toRemove = last.getValue();
currentSize -= toRemove.size; //更新移除后的size
final T key = last.getKey();
cacheIterator.remove(); //将元素移除
onItemEvicted(key, toRemove.value);
}
}
}
3.4.LinkedHashIterator.remove
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>{
abstract class LinkedHashIterator {
public final void remove() {
Node<K,V> p = current;
if (p == null){
throw new IllegalStateException();
}
if (modCount != expectedModCount){
throw new ConcurrentModificationException();
}
current = null;
K key = p.key;
removeNode(hash(key), key, null, false, false); //HashMap的移除方法
expectedModCount = modCount;
}
}
}