lru cache java

http://www.acmerblog.com/leetcode-lru-cache-lru-5745.html

acm之家的讲解是在是好,丰富

import java.util.LinkedHashMap;
public class LRUCache {
     private int capacity;
     private LinkedHashMap<Integer,Integer> map=null;
    
    public LRUCache(int capacity) {
        this.capacity=capacity;
        map=new LinkedHashMap<Integer,Integer>(16,0.75f,true){
            protected boolean removeEldestEntry(java.util.Map.Entry<Integer,Integer> eldest) {  
        
            return size()>LRUCache.this.capacity;
        

            
        }
        
    };
    }
    
    public int get(int key) {
        if(map.get(key)==null)
        {
            return -1;
        }
        return map.get(key);
        
    }
    
    public void set(int key, int value) {
        map.put(key,value);
        
        
    }
}

  

 

lru cache java,布布扣,bubuko.com

lru cache java

上一篇:Python实例31[批量对目录下文件重命名]


下一篇:python实例31[列出目录下所有的文件到txt]