惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
博客园 - 司徒正美
博客园_首页
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
I
InfoQ
M
MIT News - Artificial intelligence
T
Tailwind CSS Blog
L
LangChain Blog
Last Week in AI
Last Week in AI
A
About on SuperTechFans
B
Blog
博客园 - 叶小钗
雷峰网
雷峰网
H
Help Net Security
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

Liu Zijian's Blog | 一个技术博客

使用Certbot自动续签HTTPS证书 使用Filebeat采集Nginx日志到ES Python的协程 Python中的异常 Python中的类和对象 Python的函数 Python的数据结构,推导式、迭代器和生成器 Spring AI集成多模态模型 LangChain4j多模态 LangChain Tools工具使用 Python中的模块和包 Python全局环境和虚拟环境(venv) LangChain Prompt提示词工程 LangChain4j Tools工具使用 基于Dify搭建AI智能体应用 LangChain4j RAG检索增强生成 Spring AI实现MCP Server Spring AI集成MCP Client LangChain4j Prompt提示词工程 Spring AI使用知识库增强对话功能 Spring AI实现一个智能客服 Spring AI实现一个简单的对话机器人 实现MinIO数据的每日备份 自己实现一个DNS服务 简单理解AI智能体 大模型和大模型应用 LangChain开篇 LangChain4j开篇 一个解析Excel2007的POI工具类 DataPermissionInterceptor源码解读
最近最少使用算法(LRU)
Liu Zijian · 2022-06-20 · via Liu Zijian's Blog | 一个技术博客

import java.util.HashMap;
import java.util.Map;

/**
 * 双链表加哈希实现最近最少使用算法
 */
public class LRUCache {

    public static void main(String[] args) {
        LRUCache lruCache = new LRUCache(3);
        lruCache.put(1, 1);
        lruCache.put(2, 2);
        lruCache.put(3, 3);
        System.out.println(lruCache.map.keySet());
        lruCache.put(4, 4);
        System.out.println(lruCache.map.keySet());
        lruCache.put(3, 3);
        System.out.println(lruCache.map.keySet());
        lruCache.put(3, 3);
        System.out.println(lruCache.map.keySet());
        lruCache.put(3, 3);
        System.out.println(lruCache.map.keySet());
        lruCache.put(5, 5);
        System.out.println(lruCache.map.keySet());
    }


    private int capacity;

    private Map<Integer, Node<Integer, Integer>> map;

    private DoubleLinkedList<Integer, Integer> linkedList;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        map = new HashMap<>();
        linkedList = new DoubleLinkedList<>();
    }

    public int get(int key) {
        if (map.containsKey(key)) {
            Node<Integer, Integer> node = map.get(key);
            linkedList.removeNode(node);
            linkedList.addHead(node);
            return node.val;
        }
        return -1;
    }

    public void put(int key, int value) {
        //更新
        if (map.containsKey(key)) {
            Node<Integer, Integer> node = map.get(key);
            node.val = value;
            linkedList.removeNode(node);
            linkedList.addHead(node);
        }
        //新增
        else {
            if (map.size() == this.capacity) {
                Node<Integer, Integer> node = linkedList.last();
                linkedList.removeNode(node);
                map.remove(node.key);
            }
            Node<Integer, Integer> node = new Node<>(key, value);
            linkedList.addHead(node);
            map.put(key, node);
        }
    }



}


class Node<K, V> {
    K key;
    V val;
    Node<K, V> prev;
    Node<K, V> next;
    public Node() {

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


class DoubleLinkedList<K, V> {
    Node<K, V> head;
    Node<K, V> tail;

    public DoubleLinkedList() {
        head = new Node<>();
        tail = new Node<>();
        head.next = tail;
        tail.prev = head;
    }

    public void addHead(Node<K, V> node) {
        node.prev = head;
        node.next = head.next;
        head.next.prev = node;
        head.next = node;
    }

    public void removeNode(Node<K, V> node) {
        node.prev.next = node.next;
        node.next.prev = node.prev;
        node.next = null;
        node.prev = null;
    }

    public Node<K, V> last() {
        return tail.prev;
    }



}

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 最近最少使用
 */
public class SimpleLRUCache<K, V> extends LinkedHashMap<K, V> {

    public static void main(String[] args) {
        SimpleLRUCache<Integer, String> lruCache = new SimpleLRUCache<>(3);
        lruCache.put(1, "A");
        lruCache.put(2, "B");
        lruCache.put(3, "C");
        System.out.println(lruCache.keySet());
        lruCache.put(4, "D");
        System.out.println(lruCache.keySet());
        lruCache.put(3, "C");
        System.out.println(lruCache.keySet());
        lruCache.put(3, "C");
        System.out.println(lruCache.keySet());
        lruCache.put(3, "C");
        System.out.println(lruCache.keySet());
        lruCache.put(5, "D");
        System.out.println(lruCache.keySet());
    }




    private int capacity;

    public SimpleLRUCache(int capacity) {
        super(capacity, 0.75f, true);//true访问顺序  false插入顺序
        this.capacity = capacity;
    }

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


}