当前位置:网站首页>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);
}
}
边栏推荐
- Kept VRRP script, preemptive delay, VIP unicast details
- 使用lambda在循环中传参时,参数总为同一个值
- [蓝桥杯2017初赛]包子凑数
- 天梯赛练习集题解LV1(all)
- [AGC009D]Uninity
- AcWing 1298. Solution to Cao Chong's pig raising problem
- [mrctf2020] dolls
- L2-004 is this a binary search tree? (25 points)
- Learn winpwn (3) -- sEH from scratch
- [Blue Bridge Cup 2017 preliminary] buns make up
猜你喜欢
Request object and response object analysis
人脸识别 face_recognition
Pytoch Foundation
Stage 4 MySQL database
QT creator support platform
[Flink] Flink learning
vs2019 桌面程序快速入门
Composition des mots (sous - total)
02 staff information management after the actual project
MySQL and C language connection (vs2019 version)
随机推荐
小L的试卷
Rhcsa certification exam exercise (configured on the first host)
Solution to the practice set of ladder race LV1 (all)
L2-001 紧急救援 (25 分)
Case analysis of data inconsistency caused by Pt OSC table change
SQL time injection
Codeforces Round #753 (Div. 3)
數據庫高級學習筆記--SQL語句
保姆级出题教程
误删Path变量解决
When using lambda to pass parameters in a loop, the parameters are always the same value
Nanny level problem setting tutorial
Vs2019 desktop app quick start
2019 Tencent summer intern formal written examination
[蓝桥杯2020初赛] 平面切分
{一周总结}带你走进js知识的海洋
ImportError: libmysqlclient. so. 20: Cannot open shared object file: no such file or directory solution
Reading BMP file with C language
About string immutability
wangeditor富文本引用、表格使用问题