当前位置:网站首页>Using LinkedHashMap to realize the caching of an LRU algorithm
Using LinkedHashMap to realize the caching of an LRU algorithm
2022-07-06 11:44:00 【Ride the wind to break the bug】
/**
* @author wdj
* Created on 2021/2/25 14:28
*/
public class LRUcache<K,V> extends LinkedHashMap<K,V> {
// Default cache value
private static final int DEFAULT_NODE_NUM = 11;
// Cache limit
private int capacityLimit;
LRUcache(){
this(DEFAULT_NODE_NUM);
}
public LRUcache(int capaticy){
// In turn : Initial capacity , Load factor , Which sort of access
// The third parameter :false, It means traversal in insertion order ( The default is false)/true, Then it means sorting by access order
super(capaticy,0.75f,true);
this.capacityLimit = capaticy;
}
public V putKV(K key,V val){
return put(key,val);
}
public V getValue(K key){
return get(key);
}
public boolean exists(K key){
return containsKey(key);
}
@Override
protected boolean removeEldestEntry(Map.Entry<K,V> eldest){
// If LinkedHashMap The length of reaches the cache capacity , Scavenging
return size()>capacityLimit;
}
// The test case
public static void main(String[]args){
LRUcache<Object, Object> cache = new LRUcache<>();
for (int i=0;i<11;i++){
cache.putKV(i,i);
}
System.out.println(cache);
cache.getValue(7);
System.out.println(cache);
cache.get(5);
System.out.println(cache);
// Cover the original (10,10), At this time, the cache capacity is still sufficient
cache.putKV(10,9);
System.out.println(cache);
// Add another node
cache.putKV(11,11);
// At this time, the data in the cache has changed , Removed the head node of the linked list
System.out.println(cache);
}
}
边栏推荐
- How to configure flymcu (STM32 serial port download software) is shown in super detail
- PHP - whether the setting error displays -php xxx When PHP executes, there is no code exception prompt
- Database advanced learning notes -- SQL statement
- ES6 promise object
- [BSidesCF_2020]Had_a_bad_day
- Tcp/ip protocol (UDP)
- When using lambda to pass parameters in a loop, the parameters are always the same value
- Julia 1.6 1.7 common problem solving
- 人脸识别 face_recognition
- L2-004 这是二叉搜索树吗? (25 分)
猜你喜欢

Kept VRRP script, preemptive delay, VIP unicast details

第4阶段 Mysql数据库

Connexion sans mot de passe du noeud distribué

Cookie setting three-day secret free login (run tutorial)

Word typesetting (subtotal)

Stage 4 MySQL database

Machine learning notes week02 convolutional neural network

Composition des mots (sous - total)

4、安装部署Spark(Spark on Yarn模式)

【yarn】Yarn container 日志清理
随机推荐
【presto】presto 参数配置优化
Mysql的索引实现之B树和B+树
yarn安装与使用
[Presto] Presto parameter configuration optimization
vs2019 桌面程序快速入门
QT creator custom build process
小L的试卷
Cookie setting three-day secret free login (run tutorial)
Mtcnn face detection
MySQL与c语言连接(vs2019版)
AcWing 179.阶乘分解 题解
Integration test practice (1) theoretical basis
數據庫高級學習筆記--SQL語句
常用正则表达式整理
解决安装Failed building wheel for pillow
L2-006 树的遍历 (25 分)
Word排版(小計)
[NPUCTF2020]ReadlezPHP
Reading BMP file with C language
Nodejs connect mysql