java实现LRU算法,附代码

LRU算法简介:

原理是根据页面的最近使用情况来决定哪些页面应该被置换出去,以便为新的页面腾出空间,是一种常见的页面置换算法,用于操作系统中的页面缓存管理。

代码部分:

整体结构

 LRUCache.java

import java.util.*;


class LRUCache<K, V> {
    private final int capacity;
    private final Map<K, Node<K, V>> cache;
    private final Node<K, V> head;
    private final Node<K, V> tail;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        cache = new HashMap<>();
        head = new Node<>(null, null);
        tail = new Node<>(null, null);
        head.next = tail;
        tail.prev = head;
    }

    public V get(K key) {
        Node<K, V> node = cache.get(key);

        if (node == null) {
            // 缓存中不存在该键,返回null
            return null;
        }

        // 移动访问过的节点到链表头部,表示最近被使用过
        moveToHead(node);
        return node.value;
    }

    public void put(K key, V value) {
        Node<K, V> node = cache.get(key);

        if (node == null) {
            // 缓存中不存在该键,需要插入新节点

            if (cache.size() >= capacity) {
                // 缓存已满,移除最久未使用的节点(尾节点的前一个节点)

                Node<K, V> evictNode = tail.prev;
                removeNode(evictNode);
                cache.remove(evictNode.key);
            }

            // 创建新节点并放入缓存
            node = new Node<>(key, value);
            cache.put(key, node);
        } else {
            // 缓存中存在该键,更新节点的值,并将其移动到链表头部
            node.value = value;
            moveToHead(node);
        }

        // 将节点插入链表头部
        addToHead(node);
    }

    private void moveToHead(Node<K, V> node) {
        // 从链表中移除节点
        removeNode(node);
        // 将节点插入链表头部
        addToHead(node);
    }

    private void removeNode(Node<K, V> node) {
        // 从链表中移除节点
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }

    private void addToHead(Node<K, V> node) {
        // 将节点插入链表头部
        node.prev = head;
        node.next = head.next;
        head.next.prev = node;
        head.next = node;
    }

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

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

Main.java

public class Main {
    public static void main(String[] args) {
        LRUCache<Integer, String> cache = new LRUCache<>(3);
        cache.put(1, "one");
        cache.put(2, "two");
        cache.put(3, "three");
        System.out.println(cache.get(1)); // 输出: one
        cache.put(4, "four");
        System.out.println(cache.get(2)); // 输出: null
        System.out.println(cache.get(1));
        System.out.println(cache.get(3));
        System.out.println(cache.get(4));

    }
}

运行结果

过程:

首先创建LRUCache对象,容量为3。接着,将key为1,2,3的键值对数据以头插方式插入链表。当输出key为1的数据时,相当于访问,所以key为1的数据提前到链表头部,变成了1->3->2,插入key为4的数据时,挤出2,变为4->1->3。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值