java实现简单的LUR算法
public class LRUCache<k,v> extends LinkedHashMap<k,v> {private final int CACHE_SIZE;public LRUCache(int cacheSize){super((int)Math.ceil(cacheSize/0.75)+1,0.75f,true);// 这块就是设置一个hashMap的初始值,同时最后一个true只的是让 LinkedHashMap 按照访问顺序来进行排序,最近访问放在头,最老访问的放在尾巴CACHE_SIZE = cacheSize;}@Overrideprotected boolean removeEldestEntry(Map.Entry eldest) {return size() >CACHE_SIZE;// 这个意思就是说当map 中的数量大于指定缓存个数的时候,就自动删除最老的数据}
}
/*** Constructs an empty <tt>LinkedHashMap</tt> instance with the* specified initial capacity, load factor and ordering mode.** @param initialCapacity the initial capacity* @param loadFactor the load factor* @param accessOrder the ordering mode - <tt>true</tt> for* access-order, <tt>false</tt> for insertion-order* @throws IllegalArgumentException if the initial capacity is negative* or the load factor is nonpositive*/public LinkedHashMap(int initialCapacity,float loadFactor,boolean accessOrder) {super(initialCapacity, loadFactor);this.accessOrder = accessOrder;}
/*** Returns <tt>true</tt> if this map should remove its eldest entry.* This method is invoked by <tt>put</tt> and <tt>putAll</tt> after* inserting a new entry into the map. It provides the implementor* with the opportunity to remove the eldest entry each time a new one* is added. This is useful if the map represents a cache: it allows* the map to reduce memory consumption by deleting stale entries.** <p>Sample use: this override will allow the map to grow up to 100* entries and then delete the eldest entry each time a new entry is* added, maintaining a steady state of 100 entries.* <pre>* private static final int MAX_ENTRIES = 100;** protected boolean removeEldestEntry(Map.Entry eldest) {* return size() > MAX_ENTRIES;* }* </pre>** <p>This method typically does not modify the map in any way,* instead allowing the map to modify itself as directed by its* return value. It <i>is</i> permitted for this method to modify* the map directly, but if it does so, it <i>must</i> return* <tt>false</tt> (indicating that the map should not attempt any* further modification). The effects of returning <tt>true</tt>* after modifying the map from within this method are unspecified.** <p>This implementation merely returns <tt>false</tt> (so that this* map acts like a normal map - the eldest element is never removed).** @param eldest The least recently inserted entry in the map, or if* this is an access-ordered map, the least recently accessed* entry. This is the entry that will be removed it this* method returns <tt>true</tt>. If the map was empty prior* to the <tt>put</tt> or <tt>putAll</tt> invocation resulting* in this invocation, this will be the entry that was just* inserted; in other words, if the map contains a single* entry, the eldest entry is also the newest.* @return <tt>true</tt> if the eldest entry should be removed* from the map; <tt>false</tt> if it should be retained.*/protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {return false;}
示例二:
import java.util.LinkedHashMap;
import java.util.Map;public class LRU extends LinkedHashMap {int capacity;public LRU(int capacity) {super(16, 0.75f, true);this.capacity = capacity;}@Overrideprotected boolean removeEldestEntry(Map.Entry eldest) {return size() > capacity;}public static void main(String[] args) {LRU lru = new LRU(5);lru.put("1", 1);lru.put("2", 2);lru.put("3", 3);lru.put("4", 4);lru.get("1");lru.put("5", 5);lru.put("6", 6);System.out.println(lru.size() + " | " + lru);}
}
感谢: 中华石杉
java实现简单的LUR算法相关推荐
- 简单模拟Lur 算法
import java.util.LinkedHashMap; import java.util.Map;//模拟Lur 算法 public class redisLUR<K,V> ext ...
- JAVA中简单的MD5算法——MD5Utils
文章目录 MD5信息摘要算法 Java中简单的MD5加密类MD5Utils 今日推歌 MD5信息摘要算法 MD5(Message Digest) 它是一种哈希算法,不是一种加密算法,任何长度的任意内容 ...
- JAVA 判断简单密码算法_十道简单算法题二【Java实现】
前言 清明不小心就拖了两天没更了-- 这是十道算法题的第二篇了-上一篇回顾:十道简单算法题 最近在回顾以前使用C写过的数据结构和算法的东西,发现自己的算法和数据结构是真的薄弱,现在用Java改写一下, ...
- java的简单算法题_[2]十道算法题【Java实现】
前言 清明不小心就拖了两天没更了-- 这是十道算法题的第二篇了-上一篇回顾:十道简单算法题 最近在回顾以前使用C写过的数据结构和算法的东西,发现自己的算法和数据结构是真的薄弱,现在用Java改写一下, ...
- 蚁群算法画图java_[转载]简单蚁群算法 + JAVA实现蚁群算法
一 引言 蚁群算法(ant colony optimization,ACO),又称蚂蚁算法,是一种用来在图中寻找优化路径的机率型技术.它由Marco Dorigo于1992年在他的博士论文中引入,其灵 ...
- 蚁群算法java实现_简单蚁群算法 + JAVA实现蚁群算法
一 引言 蚁群算法(ant colony optimization,ACO),又称蚂蚁算法,是一种用来在图中寻找优化路径的机率型技术.它由Marco Dorigo于1992年在他的博士论文中引入,其灵 ...
- C语言、Java学习笔记(三)---几种简单的排序算法
假期已经过了一半,整个人都变得颓废了许多.今天没有出去玩,就学了几个简单的排序算法,以求安慰自己,好歹也是在假期里学习过了.(瘫- C 这里一次性给出三种排序方法的代码,分别是冒泡排序,选择排序和归并 ...
- 2016CCF、CSP 第二题铁路购票系统的简单座位分配算法(JAVA版)
问题描述 请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配. 假设一节车厢有20排.每一排5个座位.为方便起见,我们用1到100来给所有的座位编号,第一排是1到5号,第二排是6到10 ...
- 简单选择排序算法 (JAVA)
算法实现思想: 选择排序一共排序了n-1轮. for (int i = 0; i < arr.length - 1; i++) {} 定义变量min来记录每轮中需要比较序列中的最小数,变量min ...
最新文章
- 全球安全行业融资收购简报(2016年2月)
- 如何做好网站开发项目需求分析(转)
- 三年python面试题_300道Python面试题
- Redis基础知识点总结
- 通向架构师的道路(第六天)之漫谈基于数据库的权限系统的设计
- PostgreSQL 多重含义数组检索与条件过滤 (标签1:属性, 标签n:属性) - 包括UPSERT操作如何修改数组、追加数组元素
- c语言 除法小数点怎么写,高精度除法小数点位数
- linux 文件系统 dfs,分布式文件系统fastDFS 机器硬件要求
- html得到上传文件类型后缀,js获取上传文件后缀名(附js提交form表单)
- c#基础(一)之内存管理
- 《Ray Tracing in One Weekend》——Chapter 6: Antialiasing
- yolov5的wts权重转成tensorrt的engine权重一定要注意的问题:版本匹配(有什么问题可以私信我)
- 值得关注的开源软件推荐
- c语言输入相应的成绩评定信息,C语言上机练习题记答案.doc
- 3.微信小程序--快速开发UI界面
- localhost和127.0.0.1的区别
- Life feelings--13--青春不毕业,那些心里念念叨叨难以忘怀的记忆
- 38家公司上榜2020大中华区最佳职场;中国新生代企业家调研白皮书正式发布 | 美通企业日报...
- 【键盘】jQuery+CSS3模拟键盘事件(精)
- 深度剖析家用洗地机的方案设计
热门文章
- Mysql 条件查询(like、between、and、in······)
- 软件系统设计-17-架构评估
- 二维数组名做参数传递问题
- Apollo星火计划学习笔记——Control 专项讲解(LQR)
- 最长子序列(LCS, LIS, LCIS)
- JS中正则表达式常用语法总结
- python win32print api_python学习笔记(win32print API介绍)
- 运算(与运算)和|运算(或运算)
- 离散数学 群,环和域
- 什么是javaweb开发?