为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
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://www.cnblogs.com/keeya/p/9602691.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消