专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

LRU算法的精简实现(基于Java)

LRU(Least recently used,最近最少使用)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”。

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
    static class LRULinkedHashMap<K,V> extends LinkedHashMap<K,V> {
        //定义缓存的容量
        private int capacity;
        //带参数的构造器
        LRULinkedHashMap(int capacity){
            //如果accessOrder为true的话,则会把访问过的元素放在链表后面,放置顺序是访问的顺序
            //如果accessOrder为flase的话,则按插入顺序来遍历
            super(16,0.75f,true);
            //传入指定的缓存最大容量
            this.capacity=capacity;
        }
        //实现LRU的关键方法,如果map里面的元素个数大于了缓存最大容量,则删除链表的顶端元素
        @Override
        public boolean removeEldestEntry(Map.Entry<K, V> eldest){
            return size()>capacity;
        }
    }
    //test
    public static void main(String[] args) {
        LRULinkedHashMap<String, Integer> testCache = new LRULinkedHashMap<>(3);
        testCache.put("A", 1);
        testCache.put("B", 2);
        testCache.put("C", 3);
        System.out.println(testCache.get("B"));
        System.out.println(testCache.get("A"));
        testCache.put("D", 4);
        System.out.println(testCache.get("D"));
        System.out.println(testCache.get("C"));
    }
}

API的使用:

1、 首先是LinkedHashMap的构造器:

//如果accessOrder为true的话,则会把访问过的元素放在链表后面,放置顺序是访问的顺序
public LinkedHashMap(int initialCapacity,
                     float loadFactor,
                     boolean accessOrder) {
    super(initialCapacity, loadFactor);
    this.accessOrder = accessOrder;
}

1、 重写removeEldestEntry方法

//removeEldestEntry方法会在afterNodeInsertion中调用
//在每次put操作末尾会调用afterNodeInsertion方法。可以利用此方法删除链表的顶端元素。
void afterNodeInsertion(boolean evict) { // possibly remove eldest
    LinkedHashMap.Entry<K,V> first;
    if (evict && (first = head) != null && removeEldestEntry(first)) {
        K key = first.key;
        removeNode(hash(key), key, null, false, true);
    }
}

文章永久链接:https://tech.souyunku.com/48630

未经允许不得转载:搜云库技术团队 » LRU算法的精简实现(基于Java)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们