LRU 缓存实现

使用hashmap加双向链表

package com.fantaike;

import sun.applet.Main;

import java.util.HashMap;
import java.util.Map;

public class LRUcache {
    class Node {
        Node pre;
        Node next;
        Integer key;
        Integer val;
        Node(Integer k, Integer v) {
            key = k;
            val = v;
        }
    }
    Map<Integer, Node> map = new HashMap<Integer, Node>();
    // The head (eldest) of the doubly linked list.
    Node head;
    // The tail (youngest) of the doubly linked list.
    Node tail;
    int cap;
    public LRUcache(int capacity) {
        cap = capacity;
        head = new Node(null, null);
        tail = new Node(null, null);
        head.next = tail;
        tail.pre = head;
    }
    public int get(int key) {
        Node n = map.get(key);
        if(n!=null) {
            n.pre.next = n.next;
            n.next.pre = n.pre;
            appendTail(n);
            return n.val;
        }
        return -1;
    }
    public void set(int key, int value) {
        Node n = map.get(key);
        // existed
        if(n!=null) {
            n.val = value;
            map.put(key, n);
            n.pre.next = n.next;
            n.next.pre = n.pre;
            appendTail(n);
            return;
        }
        // else {
        if(map.size() == cap) {
            //达到链表的长度,从链表的头部删除元素
            Node tmp = head.next;
            head.next = head.next.next;
            head.next.pre = head;
            map.remove(tmp.key);
        }
        n = new Node(key, value);
        // youngest node append tail
        //向链表的尾部添加元素
        appendTail(n);
        map.put(key, n);
    }
    private void appendTail(Node n) {
        n.next = tail;
        n.pre = tail.pre;
        tail.pre.next = n;
        tail.pre = n;
    }

    public static void main(String[] args) {
        LRUcache lrUcache = new LRUcache(3);
        lrUcache.set(1,1);
        lrUcache.set(2,2);
        lrUcache.set(3,3);
        lrUcache.set(4,4);
        System.out.println(lrUcache.get(1));
        System.out.println(lrUcache.get(2));
    }
}

使用linkedhashmap实现LRU 缓存

package com.fantaike;

import sun.applet.Main;

import java.util.Map;
import java.util.logging.Level;

public class LRUcache2 {
    private int capacity;
    private Map<Integer, Integer> cache;
    public LRUcache2(int capacity) {
        this.capacity = capacity;
        this.cache = new java.util.LinkedHashMap<Integer, Integer> (capacity, 0.75f, true) {
            // 定义put后的移除规则,大于容量就删除eldest
            protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
                return size() > capacity;
            }
        };
    }
    public int get(int key) {
        if (cache.containsKey(key)) {
            return cache.get(key);
        } else
            return -1;
    }
    public void set(int key, int value) {
        cache.put(key, value);
    }

    public static void main(String[] args) {
        LRUcache2 lrUcache2=new LRUcache2(3);
        lrUcache2.set(1,1);
        lrUcache2.set(2,2);
        lrUcache2.set(3,3);
        lrUcache2.set(4,4);
        System.out.println(lrUcache2.get(1));
        System.out.println(lrUcache2.get(3));
    }
}

参考文献

https://www.cnblogs.com/dolphin0520/p/3741519.html

http://yikun.github.io/2015/04/03/%E5%A6%82%E4%BD%95%E8%AE%BE%E8%AE%A1%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AALRU-Cache%EF%BC%9F/

猜你喜欢

转载自blog.csdn.net/u011243684/article/details/87096771