LRU-最近最久未使用算法

3 篇文章 0 订阅
import java.util.HashMap;
import java.util.Map;

/**
 * @author caisebei
 */
public class LRUCache {


    private Node head;

    private Node end;

    private int limit;

    private Map<String,Node> hashMap;

    public LRUCache(int limit) {
        this.limit = limit;
        this.hashMap = new HashMap<>();
    }

    public Node get(String key) {
        Node node = hashMap.get(key);
        if(node == null) {
            return null;
        }
        refreshNode(node);
        return node;
    }

    public void put(String key, String value) {
        Node node = hashMap.get(key);
        if(node == null) {
            if(hashMap.size() >= limit) {
                String oldKey = removeNode(head);
                hashMap.remove(oldKey);
            }

            node = new Node(key, value);
            addNode(node);
            hashMap.put(key, node);
        } else {
            node.value = value;
            refreshNode(node);
        }
    }

    public void remove(String key) {
        Node node = hashMap.get(key);
        removeNode(node);
        hashMap.remove(key);
    }
    
    
    /**
     *  
     * 添加节点
     **/
    private void addNode(Node node) {
        if(end != null) {
            end.next = node;
            node.pre = end;
        }
        end = node;
        if(head == null) {
            head = node;
        }
    }

    
    
    /**
     * 刷新被访问的节点位置
     **/
    private void refreshNode(Node node) {
        
        if(node == end) {
            return ;
        }
        
        removeNode(node);
        addNode(node);
    }

    
    /**
     * 删除节点
     * @return java.lang.String
     **/
    private String removeNode(Node node) {
        if(node == head && node == end) {  //移除唯一节点
            head = null;
            end = null;
        }else if (node == end) {  //移除尾节点
            end = end.pre;
            end.next = null;
        }else if(node == head) { // 移除头节点
            head = head.next;
            head.pre = null;
        }else {                  // 移除中间节点
            node.pre.next = node.next;
            node.next.pre = node.pre;
        }
        return node.key;
    }


    public static void main(String[] args) {
        LRUCache lruCache = new LRUCache(5);
        lruCache.put("001", "用户1");
        lruCache.put("002", "用户2");
        lruCache.put("003", "用户3");
        lruCache.put("004", "用户4");
        lruCache.put("005", "用户5");
        lruCache.get("002");
        lruCache.put("006", "用户6");
    }
    


    class Node {

        public Node pre;
        public Node next;
        public String key;
        public String value;

        Node(String key, String value) {
            this.key = key;
            this.value = value;
        }
    }

    
}

from: 《漫画算法》1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值