package LRU
;
import java
.util
.*
;
public class LRU extends LinkedHashMap {
private static float LOAD_FACTOR
= 0.75f;
private static int INIT_CAPACITY
= 3;
private float maxCapacity
;
public LRU(int maxCapacity
){
super(INIT_CAPACITY
,LOAD_FACTOR
,true);
this.maxCapacity
= maxCapacity
;
}
@Override
protected boolean removeEldestEntry(Map
.Entry eldest
) {
if (size() > maxCapacity
){
return true;
}
return false;
}
public static void main(String
[] args
) {
LRU cache
= new LRU(3);
cache
.put(1,null
);
cache
.put(2,null
);
cache
.put(3,null
);
cache
.put(1,null
);
cache
.put(4,null
);
for (Object object
: cache
.entrySet()) {
System
.out
.println(object
);
}
}
}
转载请注明原文地址: https://lol.8miu.com/read-8480.html