Java 实现双链表 造LRU轮子

Java 实现双链表 造LRU轮子

  • 实现双链表
  • 造LRU

LRU(Least Recently Used)是缓存淘汰策略,这个可以联想到早年间,CPU和内存的容量特别小,要想管理好有限的空间,我们就需要执行内存清理。清理什么呢?当然是那些暂时不需要的,LRU就是一种,它负责清理出最近最没有被使用的。
其底层是LinkedHashMap, 双向哈希链表,有兴趣可以去读一下源码。
实现需求:

  1. 要实现put 和 get 的复杂度均为O(1),这样的情况必然需要哈希来解决。
  2. 如果要保证知道哪个元素久,最好是维护一个序列
  3. 每次访问一个key,需要更改其为最近使用,所以你的数据结构需要保证数据可以随便插入和删除。

此处我就根据大牛的思路,我们一起实现一个双向循环链表,再造LRU的轮子:

实现双链表

class Node{
    public int key, val;
    public Node next, prev;
    public Node(int k, int v){
        this.key = k;
        this.val = v;
    }
}
class doublelist {
    private Node head, tail;
    private int size;

    public doublelist(){
        head = new Node(0,0);
        tail = new Node(0,0);
        head.next = tail;
        tail.prev = head;
        size = 0;
    }

    public void addLast(Node x){
        x.prev = tail.prev;
        x.next = tail;
        tail.prev.next = x;
        tail.prev = x;
        size++;
    }

    public void remove(Node x){
        x.prev.next = x.next;
        x.next.prev = x.prev;
        size--;
    }

    public Node removeFirst(){
        if(head.next == tail){
            return null;
        }
        Node first = head.next;
        remove(first);
        return first;
    }

    public int size(){
        return size;
    }
}

造LRU

class LRUCache{
    private HashMap<Integer, Node> map;
    private doublelist cache;
    private int cap;

    public LRUCache(int cap){
        this.cap = cap;
        map = new HashMap<>();
        cache = new doublelist();
    }

    private void makeRecently(int key){
        Node x = map.get(key);
        cache.remove(x);
        cache.addLast(x);
    }

    private void addRecently(int key, int val){
        Node x = new Node(key, val);
        cache.addLast(x);
        map.put(key, x);
    }

    private void deleteKey(int key){
        Node x = map.get(key);
        cache.remove(x);
        map.remove(key);
    }

    private void removeLeastRecently(){
        Node deletedNode = cache.removeFirst();
        int deletedKey = deletedNode.key;
        map.remove(deletedKey);
    }

    public int get(int key){
        if(!map.containsKey(key)){
            return -1;
        }
        makeRecently(key);
        return map.get(key).val;
    }

    public void put(int key, int val){
        if(map.containsKey(key)){
            deleteKey(key);
            addRecently(key, val);
            return;
        }

        if(cap == cache.size()){
            removeLeastRecently();;
        }

        addRecently(key, val);
    }
}

底层大概是这样,我们如果直接使用LinkedHashMap:

class LRUCache {
    private int capacity;
    LinkedHashMap<Integer, Integer> cache = new LinkedHashMap<>();
    public LRUCache(int capacity) {
        this.capacity = capacity;
    }
    
    public int get(int key) {
        if(!cache.containsKey(key)){
            return -1;
        }
        makeRecently(key);
        return cache.get(key);
    }
    
    public void put(int key, int value) {
        if(cache.containsKey(key)){
            cache.put(key, value);
            makeRecently(key);
            return;
        }
        if(cache.size() >= this.capacity){
            int oldestKey = cache.keySet().iterator().next();
            cache.remove(oldestKey);
        }
        cache.put(key, value);
    }

    private void makeRecently(int key){
        int val = cache.get(key);
        cache.remove(key);
        cache.put(key, val);
    }
}

你可能感兴趣的:(java,链表,数据结构)