avatar
j*y
1
An LRU (least recently used)
May I use LinkedHashMap for the question or I have to define my own data
structure ?
Thanks
avatar
g*e
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

avatar
j*y
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

avatar
w*t
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();

}
}
avatar
O*n
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;
: }
: };
: }

avatar
w*t
6
No, unordered_map + list

【在 O******n 的大作中提到】
: 如果是用C++考这道题,有什么现成的container么?
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。