j**y 发帖数: 462 | 1 An LRU (least recently used)
May I use LinkedHashMap for the question or I have to define my own data
structure ?
Thanks |
g**e 发帖数: 6127 | 2 you can use LinkedHashMap but you need to understand why it can work as LRU
cache
【在 j**y 的大作中提到】 : An LRU (least recently used) : May I use LinkedHashMap for the question or I have to define my own data : structure ? : Thanks
|
j**y 发帖数: 462 | 3 Thanks, seems LinkedHashMap maintain a double linked list and delete/put
the latest access item to the head
LRU
【在 g**e 的大作中提到】 : you can use LinkedHashMap but you need to understand why it can work as LRU : cache
|
w*********t 发帖数: 170 | 4 class LRU{
private final static int MAX_SIZE= 3;
private Map map;
public LRU(){
map = new LinkedHashMap(MAX_SIZE,0.75F,true){
public boolean removeEldestEntry(Map.Entry eldest){
return map.size() > MAX_SIZE;
}
};
}
public void add(K k,V v){
map.put(k,v);
}
public V get(K k){
return map.get(k);
}
public void printLRU(){
for(V v:map.values()){
System.out.print(v+" ");
}
System.out.print("\n");
}
public static void main(String[] args){
LRU lru = new LRU();
lru.add("testing1", "1");
lru.add("testing2", "2");
lru.add("testing3", "3");
lru.add("testing4", "4");
lru.printLRU();
}
} |
O******n 发帖数: 1505 | 5 如果是用C++考这道题,有什么现成的container么?
【在 w*********t 的大作中提到】 : class LRU{ : private final static int MAX_SIZE= 3; : private Map map; : public LRU(){ : map = new LinkedHashMap(MAX_SIZE,0.75F,true){ : public boolean removeEldestEntry(Map.Entry eldest){ : return map.size() > MAX_SIZE; : } : }; : }
|
w*********t 发帖数: 170 | 6 No, unordered_map + list
【在 O******n 的大作中提到】 : 如果是用C++考这道题,有什么现成的container么?
|