LRUCache算法

设计一种数据结构实现LRU算法,查询以及插入操作时间复杂度为O(1),并且满足最近最少使用原则。如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小。当限定的空间已存满数据时,应当把最久没有被访问到的数据淘汰。

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

The cache is initialized with a positive capacity.

Follow up:
Could you do both operations in O(1) time complexity?

基本实现思路:

利用HashMap以及双端队列来实现,利用Map来保存key与Node节点的对应关系,实现O(1)查询操作;

利用双端队列来保存Node节点链表,当查询命中时,将Node节点移动到链表头部;当插入时,如果容量未满,则直接插入到链表头部,如果容量已满则删除链表最后元素,将新元素插入到链表头部,这样链表头部一直保存最新的Node节点,尾部保存最老的Node节点。

使用HashMap以及自定义双向链表来实现

//双向链表+HashMap来实现
//如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小。当限定的空间已存满数据时,应当把最久没有被访问到的数据淘汰。
//查询数据时,如果命中将数据移动到链表头部
//添加数据时,如果容量未满直接插入到链表头部,如果容量已满则删除尾部元素,插入到链表头部
//当存在热点数据时,LRU的效率很好,但偶发性的、周期性的批量操作会导致LRU命中率急剧下降,缓存污染情况比较严重。
public class LRUCache {
	
	private Node head;
	private Node tail;
	private HashMap<Integer, Node> map;
	private int capacity;
	
	class Node{
		int key;
		int value;
		Node prev, next;
		Node(int key, int value){
			this.key = key;
			this.value = value;
		}
	}
	
	public LRUCache(int capacity){
		map = new HashMap<>();
		this.capacity = capacity;
		head = new Node(0, 0);
		tail = new Node(0, 0);
		head.next = tail;
		tail.prev = head;
	}
	
	public int get(int key){
		//如果key不存在返回-1
		if(!map.containsKey(key)) return -1;
		//如果key存在,则将节点删除,然后重新插入到链表头部
		Node n = map.get(key);
		remove(n);
		addToHead(n);
		return n.value;
	}
	
	public void put(int key, int value){
		Node n = new Node(key, value);
		//如果key不存在
		if(!map.containsKey(key)){
			//如果容量已满,则将链表尾部元素删除  与查询操作将元素置于头部相呼应
			if(map.size() == capacity){
				map.remove(tail.prev.key);
				remove(tail.prev);
			}
		}else{
			remove(map.get(key));
		}
		//保存Node,将Node移动到链表头部
		map.put(key, n);
		addToHead(n);
	}

	//将Node移动到链表头部
	private void addToHead(Node n) {
		Node hnext = head.next;
		n.next = hnext;
		hnext.prev = n;
		head.next = n;
		n.prev = head;
	}

	//删除某个Node
	private void remove(Node n) {
		n.prev.next = n.next;
		n.next.prev = n.prev;
	}

}

初始化维持一个head以及tail指针

当插入元素时如果发现该元素已经存在需要将Node删除,并且将Node插入到链表头部;

当查询命中时,也需要将Node删除移动到链表头部;

从链表中删除一个Node节点

将一个Node节点插入到链表头部

使用JDK LinkedHashMap来实现

//JDK LinkedHashMap来实现
public class LRUCache2 extends LinkedHashMap<Integer, Integer>{

	private int cacheSize;
	
	public LRUCache2(int capacity){
		super(capacity, 0.75f, true);
		this.cacheSize = capacity;
	}
	
	public int get(int key){
		return super.getOrDefault(key, -1);
	}
	
	public void put(int key, int value){
		super.put(key, value);
	}
	
	@Override
	protected boolean removeEldestEntry(java.util.Map.Entry<Integer, Integer> eldest) {
		return this.size() > cacheSize;
	}
}

LinkedHashMap继承HashMap

public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>

双向链表的核心结构体Entry继承HashMap Node数据结构   在Node基础之上添加before以及after指针

static class Entry<K,V> extends HashMap.Node<K,V> {
        Entry<K,V> before, after;
        Entry(int hash, K key, V value, Node<K,V> next) {
            super(hash, key, value, next);
        }
    }
static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;
}

LinkedHashMap其内部维持head以及tail指针  头部存在最老的元素,尾部存放最年轻的元素,即新插入的元素是放在尾部的。

    /**
     * The head (eldest) of the doubly linked list.
     */
    transient LinkedHashMap.Entry<K,V> head;

    /**
     * The tail (youngest) of the doubly linked list.
     */
    transient LinkedHashMap.Entry<K,V> tail;

属性accessOrder控制元素的迭代访问顺序,true  访问顺序   false 插入顺序

 /**
     * The iteration ordering method for this linked hash map: <tt>true</tt>
     * for access-order, <tt>false</tt> for insertion-order.
     *
     * @serial
     */
    final boolean accessOrder;

查询方法    

public V getOrDefault(Object key, V defaultValue) {
       Node<K,V> e;
       if ((e = getNode(hash(key), key)) == null)
           return defaultValue;
       if (accessOrder)
           afterNodeAccess(e);
       return e.value;
   }

调用hashmap的getNode方法  计算keyhash值,找到数组索引,根据数组位置是单链表以及红黑树来检索元素。

final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
            if ((e = first.next) != null) {
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }

由于此处accessOrder=true,调用afterNodeAccess方法,实现将新访问的node移动到链表尾部,首先实现将node p删除,然后添加到链表尾部。

void afterNodeAccess(Node<K,V> e) { // move node to last
        LinkedHashMap.Entry<K,V> last;
        if (accessOrder && (last = tail) != e) {
            LinkedHashMap.Entry<K,V> p =
                (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
            p.after = null;
            if (b == null)
                head = a;
            else
                b.after = a;
            if (a != null)
                a.before = b;
            else
                last = b;
            if (last == null)
                head = p;
            else {
                p.before = last;
                last.after = p;
            }
            tail = p;
            ++modCount;
        }
    }

插入方法

public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

hashmap的putVal方法

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

首先执行hashmap的插入操作,如果待插入的key已经存在,则更新value值,返回旧value值,并且执行afterNodeAccess操作,实现将最近被访问的元素置于链表尾部;

插入结束之后会执行afterNodeInsertion方法,此处evict=true

void afterNodeInsertion(boolean evict) { // possibly remove eldest
        LinkedHashMap.Entry<K,V> first;
        if (evict && (first = head) != null && removeEldestEntry(first)) {
            K key = first.key;
            removeNode(hash(key), key, null, false, true);
        }
    }

由于removeEldestEntry方法已经重写  当元素数量超过容量,则需要执行removeNode操作。

@Override
	protected boolean removeEldestEntry(java.util.Map.Entry<Integer, Integer> eldest) {
		return this.size() > cacheSize;
	}
final Node<K,V> removeNode(int hash, Object key, Object value,
                               boolean matchValue, boolean movable) {
        Node<K,V>[] tab; Node<K,V> p; int n, index;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (p = tab[index = (n - 1) & hash]) != null) {
            Node<K,V> node = null, e; K k; V v;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                node = p;
            else if ((e = p.next) != null) {
                if (p instanceof TreeNode)
                    node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
                else {
                    do {
                        if (e.hash == hash &&
                            ((k = e.key) == key ||
                             (key != null && key.equals(k)))) {
                            node = e;
                            break;
                        }
                        p = e;
                    } while ((e = e.next) != null);
                }
            }
            if (node != null && (!matchValue || (v = node.value) == value ||
                                 (value != null && value.equals(v)))) {
                if (node instanceof TreeNode)
                    ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
                else if (node == p)
                    tab[index] = node.next;
                else
                    p.next = node.next;
                ++modCount;
                --size;
                afterNodeRemoval(node);
                return node;
            }
        }
        return null;
    }

将node节点删除之后,将node p从双向链表中删除,删除时从头部删除

void afterNodeRemoval(Node<K,V> e) { // unlink
        LinkedHashMap.Entry<K,V> p =
            (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
        p.before = p.after = null;
        if (b == null)
            head = a;
        else
            b.after = a;
        if (a == null)
            tail = b;
        else
            a.before = b;
    }

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值