java实现一个LRU缓存算法。

//LRU(Least Recently Used)缓存算法是一种常见的缓存淘汰策略,
// 它的基本思想是保留最近被访问过的数据,淘汰最久未被访问的数据。下面是一个使用Java实现的简单LRU缓存算法:
import java.util.LinkedHashMap;
import java.util.Map;
public class Test_A29<K,V> extends LinkedHashMap<K,V> {
    private final int capacity; // 声明LRU缓存的容量
    public Test_A29(int capacity){
    super(capacity,0.75f,true);// 初始化LinkedHashMap,设置容量、负载因子和访问顺序为true
    this.capacity=capacity;//初始化缓存容量
    }
    // 重写removeEldestEntry方法,实现LRU缓存的淘汰策略
    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return size() > capacity; // 判断是否需要移除最老的条目
    }
    // main方法,程序入口点
    public static void main(String[] args) {
        Test_A29<Integer, String> cache = new Test_A29<>(2); // 创建Test_A29对象,容量为2

        cache.put(1, "One"); // 将键值对(1, "One")放入缓存
        cache.put(2, "Two"); // 将键值对(2, "Two")放入缓存

        System.out.println(cache.get(1)); // 获取键1对应的值,并打印
        System.out.println(cache.get(2)); // 获取键2对应的值,并打印

        cache.put(3, "Three"); // 将键值对(3, "Three")放入缓存,触发LRU淘汰最老条目

        System.out.println(cache.get(1)); // 获取键1对应的值(已淘汰),打印null
        System.out.println(cache.get(2)); // 获取键2对应的值,仍在缓存中,打印"Two"
        System.out.println(cache.get(3)); // 获取键3对应的值,打印"Three"
    }
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用Java实现Lru算法的示例代码。 Lru算法的核心是维护一个有序的缓存,每次访问缓存时,将访问的缓存项移到最前面,并删除最后一个缓存项。在Java中,我们可以使用双向链表和哈希表来实现这个有序缓存。 以下是一个简单的Java实现: ```java import java.util.HashMap; import java.util.Map; public class LruCache<K, V> { class Node { K key; V value; Node prev; Node next; Node(K key, V value) { this.key = key; this.value = value; } } private final int capacity; private final Map<K, Node> map = new HashMap<>(); private Node head; private Node tail; public LruCache(int capacity) { this.capacity = capacity; } public V get(K key) { Node node = map.get(key); if (node != null) { remove(node); add(node); return node.value; } return null; } public void put(K key, V value) { Node node = map.get(key); if (node != null) { node.value = value; remove(node); add(node); } else { if (map.size() == capacity) { map.remove(tail.key); remove(tail); } node = new Node(key, value); add(node); map.put(key, node); } } private void add(Node node) { if (head == null) { head = node; tail = node; } else { node.next = head; head.prev = node; head = node; } } private void remove(Node node) { if (node == head) { head = node.next; if (head != null) { head.prev = null; } } else if (node == tail) { tail = node.prev; if (tail != null) { tail.next = null; } } else { node.prev.next = node.next; node.next.prev = node.prev; } } } ``` 这个LruCache类使用了双向链表和哈希表来维护一个有序缓存,每次访问缓存时,将访问的缓存项移到最前面,并删除最后一个缓存项。在put方法中,如果缓存中已经存在该缓存项,则更新缓存项的值,并将其移到最前面,否则,如果缓存已满,则删除最后一个缓存项,并将新的缓存项添加到最前面。 示例代码: ```java LruCache<String, Integer> cache = new LruCache<>(2); cache.put("a", 1); cache.put("b", 2); System.out.println(cache.get("a")); // 1 cache.put("c", 3); System.out.println(cache.get("b")); // null cache.put("d", 4); System.out.println(cache.get("a")); // null System.out.println(cache.get("c")); // 3 System.out.println(cache.get("d")); // 4 ``` 在上面的示例中,我们创建了一个容量为2的LruCache,向其中添加了三个缓存项,当缓存项数量超过容量时,会自动删除最近最少使用的缓存项,所以最终缓存中只剩下了"c"和"d"。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值