java系列-LinkedHashMap怎么实现LRU

 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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值