Java实现LRU缓存

LRU算法会淘汰最久未使用的元素
插入和查询时间复杂度都要求O(1),因此使用 链表+HashMap实现

/**
 * 使用 链表+HashMap 实现LRU
 */
public class LRUCache {
    private ListNode head; // 头结点占位, 避免判空操作
    private ListNode tail; // 尾节点占位, 避免判空操作
    private Map<Integer, ListNode> cache;
    private int capacity; // 容量
    private int size; // 存储元素个数

    /**
     * 初始化容量为 capacity 的LRU缓存
     */
    public LRUCache(int capacity) {
        this.capacity = capacity;
        this.head = new ListNode();
        this.tail = new ListNode();
        this.head.setNext(this.tail);
        this.cache = new HashMap<>();
    }

    /**
     * 查询元素, 如果不存在, 返回-1
     */
    public int get(int key) {
        ListNode node = cache.get(key);
        if (node == null) return -1; // 没有目标元素
        moveToHead(node); // 这个节点被访问了, 移到队头
        return node.val;
    }

    /**
     * 插入元素
     */
    public void put(int key, int value) {
        ListNode node = cache.get(key);
        if (node != null) { // 存在, 更新值, 移到队头
            node.val = value;
            moveToHead(node);
            return;
        }
        node = new ListNode(key, value); // 不存在, 创建, 放到队头
        this.head.append(node);
        cache.put(key, node);
        size++;
        eliminate(); // 淘汰元素
    }

    private void eliminate() {
        if (size > capacity) {
            size--;
            ListNode remove = tail.pre;
            remove.remove(); // 淘汰末尾元素
            cache.remove(remove.key);
        }
    }

    private void moveToHead(ListNode node) {
        node.remove(); // 从链表中移除
        this.head.append(node); // 添加到队头
    }

    private static class ListNode {
        public int key;
        public int val;
        public ListNode next;
        public ListNode pre;

        public ListNode() {
        }

        public ListNode(int key, int val) {
            this.key = key;
            this.val = val;
        }

        public void append(ListNode element) {
            ListNode next = this.next;
            this.setNext(element);
            element.setNext(next);
        }

        public void remove() {
            if (pre != null) {
                pre.setNext(next);
                this.pre = null;
            }
            this.next = null;
        }

        private void setNext(ListNode next) {
            this.next = next;
            if (next != null) {
                next.pre = this;
            }
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            ListNode cur = this;
            while (cur != null) {
                sb.append(cur.key).append(":").append(cur.val).append(", ");
                cur = cur.next;
            }
            if (sb.length() > 0) {
                return sb.substring(0, sb.length() - 2);
            }
            return "";
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值