HashMap+双向链表手写LRU缓存算法/页面置换算法

Posted roni-i

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HashMap+双向链表手写LRU缓存算法/页面置换算法相关的知识,希望对你有一定的参考价值。

import java.util.Hashtable;
//https://zhuanlan.zhihu.com/p/34133067
class DLinkedList {
    String key; //
    int value;    //
    DLinkedList pre; //双向链表前驱
    DLinkedList next; //双向链表后继
}
public class LRUCache {
    private Hashtable<String,DLinkedList> cache = new Hashtable<String,DLinkedList>(); 
    private int count;
    private int capacity;
    private DLinkedList head, tail;
    public LRUCache(int capacity) {
        this.count = 0;
        this.capacity = capacity;
        head = new DLinkedList();
        head.pre = null;
        tail = new DLinkedList();
        tail.next = null;
        
        head.next = tail;
        tail.pre = head;
    }
    public int get(String key) {
        DLinkedList node = cache.get(key);
        if(node == null) return -1;
        this.moveToHead(node);
        return node.value;
    }
    public void set(String key,int value) {
        DLinkedList node = cache.get(key);
        if(node == null) {
            DLinkedList newNode = new DLinkedList();
            newNode.key = key;
            newNode.value = value;
            this.cache.put(key, newNode);
            this.addNode(newNode);
            ++count;
        
            if(count>capacity) {
                DLinkedList tail = this.popTail();
                this.cache.remove(tail.key);
                --count;
            }
        }
        else {
            node.value = value;
            this.moveToHead(node);
        }
    }
    private void addNode(DLinkedList node) {
        node.pre = head;
        node.next = head.next;
        head.next.pre = node;
        head.next = node;
    }
    private void removeNode(DLinkedList node) {
        DLinkedList pre = node.pre;
        DLinkedList next = node.next;
        pre.next = next;
        next.pre = pre;
    }
    private void moveToHead(DLinkedList node) {
        this.removeNode(node);
        this.addNode(node);
    }
    private DLinkedList popTail() {
        DLinkedList res = tail.pre;
        this.removeNode(res);
        return res;
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        DLinkedList node = head;
        while(node != null){
            sb.append(String.format("%s:%s ", node.key,node.value));
            node = node.next;
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        LRUCache lru = new LRUCache(3);
        lru.set("1", 7);
        System.out.println(lru.toString());
        lru.set("2", 0);
        System.out.println(lru.toString());
        lru.set("3", 1);
        System.out.println(lru.toString());
        lru.set("4", 2);
        System.out.println(lru.toString());
        lru.get("2");
        System.out.println(lru.toString());
        lru.set("5", 3);
        System.out.println(lru.toString());
        lru.get("2");
        System.out.println(lru.toString());
        lru.set("6", 4);
        System.out.println(lru.toString());
        /*
         0ull:0 1:7 null:0 
        null:0 2:0 1:7 null:0 
        null:0 3:1 2:0 1:7 null:0 
        null:0 4:2 3:1 2:0 null:0 
        null:0 2:0 4:2 3:1 null:0 
        null:0 5:3 2:0 4:2 null:0 
        null:0 2:0 5:3 4:2 null:0 
        null:0 6:4 2:0 5:3 null:0  
         */
    }
}

 

以上是关于HashMap+双向链表手写LRU缓存算法/页面置换算法的主要内容,如果未能解决你的问题,请参考以下文章

Java手写原生的LRU算法

java编写LRU算法

LRU算法的实现

从源码出发,分析LRU缓存淘汰策略的实现!

LRU 缓存淘汰算法

算法: 实现LRU缓存,读取写入O实现