package com.zhang.reflection.面试.算法模版.LRU;
import java.util.*;
public class LRU1<K,V> extends LinkedHashMap {
//缓冲容量
private int capacity;
public LRU1(int capacity){
super(16,0.75f,true);
this.capacity = capacity;
}
//重写removeEldestEntry 返回值代表是否需要删除最老的节点的判断
@Override
protected boolean removeEldestEntry(Map.Entry eldest) {
boolean sign = size() > capacity;
if(sign){
System.out.println("要删除地是:"+eldest);
}
return sign;
}
public static void main(String[] args) {
LRU1<Integer,Integer> map = new LRU1(4);
map.put(1,1);
map.put(2,2);
map.put(3,3);
map.put(4,4);
map.put(5,5);
List<Map.Entry<Integer,Integer>> result=new ArrayList<>(map.entrySet());
for(Map.Entry<Integer,Integer> node:result){
System.out.println(node.getValue());
}
}
}