JAVA版简单LRU算法

手写简单版LRU算法

public class LRU<K,V> {

    private int capacity;

    private HashMap<K,Node<K,V>> map;

    private Node head;

    private Node tail;

    public LRU(int capacity) {
        this.capacity = capacity;
        map = new HashMap<K,Node<K,V>>();
        this.head = this.tail = new Node();
        this.head.next = this.tail;
        this.tail.pre = this.head;
    }

    public void put(K key,V value){
        Node<K,V> node = new Node<K,V>(key,value);
        if(!map.containsKey(key)){
            node.pre = this.head;
            node.next = this.head.next;
            this.head.next = node;
            node.next.pre = node;
            if(map.size() == capacity){
                delNode(getLastNode());
            }
            map.put(key,node);
        }else{
            moveToHead(map.get(key));
            map.get(key).setValue(value);
        }
    }

    public V get(K key){
        if(null != map.get(key)){
            moveToHead(map.get(key));
            return map.get(key).value;
        }
        return null;
    }

    private void moveToHead(Node<K,V> node){
        delNode(node);
        node.pre = this.head;
        node.next = this.head.next;
        this.head.next = node;
        node.next.pre = node;
        map.put(node.key,node);
    }

    private Node getLastNode(){
        return tail.pre;
    }

    private void delNode(Node<K,V> node){
        node.pre.next = node.next;
        node.next.pre = node.pre;
        map.remove(node.key);
    }

    @Override
    public String toString() {
        String result = "";
        Node node = tail;
        while (node.pre != head){
            node = node.pre;
            result += node.key.toString();
        }
        return result;
    }

    static class Node<K,V>{
        private K key;
        private V value;
        private Node pre;
        private Node next;

        public Node() {
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }

        public Node getPre() {
            return pre;
        }

        public void setPre(Node pre) {
            this.pre = pre;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }

    public static void main(String[] args) {
        LRU<Integer,Integer> lru = new LRU<>(4);
        lru.put(1,1);
        lru.put(2,2);
        lru.put(3,3);
        System.out.println(lru.toString());
        lru.put(4,4);
        System.out.println(lru.toString());
        lru.put(5,5);
        System.out.println(lru.toString());
        System.out.println(lru.get(4));
        System.out.println(lru.toString());
        System.out.println(lru.get(6));
    }
}

基于LinkedHashMap实现的LRU算法

public class LinkHashMapLRU<K,V> extends LinkedHashMap<K,V> {

    private int capacity;

    public LinkHashMapLRU(int capacity) {
        super(capacity,0.75f,true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size() > capacity;
    }

    public static void main(String[] args) {
        LinkHashMapLRU<Integer,Integer> linkHashMapLRU = new LinkHashMapLRU<Integer,Integer>(3);
        linkHashMapLRU.put(1,1);
        linkHashMapLRU.put(2,2);
        linkHashMapLRU.put(3,3);
        System.out.println(linkHashMapLRU.keySet().toString());
        linkHashMapLRU.put(4,4);
                System.out.println(linkHashMapLRU.keySet().toString());
        linkHashMapLRU.put(3,3);
        linkHashMapLRU.put(3,3);
        linkHashMapLRU.put(3,3);
        System.out.println(linkHashMapLRU.keySet().toString());
        linkHashMapLRU.put(5,5);
        System.out.println(linkHashMapLRU.keySet().toString());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值