码迷,mamicode.com
首页 > 其他好文 > 详细

LRU实现

时间:2017-02-16 23:03:33      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:bool   link   lrucache   over   dha   this   string   span   put   

import java.util.LinkedHashMap;
import java.util.Map;
 
/**
 * LRU (Least Recently Used) 
 */
public class LRUCache<K, V> extends LinkedHashMap<K, V> {
    
    private static final long serialVersionUID = 1L;
    //缓存大小
    private int cacheSize;
 
    public LRUCache(int cacheSize) {
        //第三个参数true是关键
        super(10, 0.75f, true);
        this.cacheSize = cacheSize;
    }
 
    /**
     * 缓存是否已满
     */
    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        boolean r = size() > cacheSize;
        if(r){
            System.out.println("清除缓存key:"+eldest.getKey());
        }
        return r;
    }
 
    //测试
    public static void main(String[] args) {
        LRUCache<String, String> cache = new LRUCache<String, String>(5);
        cache.put("1", "1");
        cache.put("2", "2");
        cache.put("3", "3");
        cache.put("4", "4");
        cache.put("5", "5");
 
        System.out.println("初始化:");
        System.out.println(cache.keySet());
        System.out.println("访问3:");
        cache.get("3");
        System.out.println(cache.keySet());
        System.out.println("访问2:");
        cache.get("2");
        System.out.println(cache.keySet());
        System.out.println("增加数据6,7:");
        cache.put("6", "6");
        cache.put("7", "7");
        System.out.println(cache.keySet());
    }
}

 

LRU实现

标签:bool   link   lrucache   over   dha   this   string   span   put   

原文地址:http://www.cnblogs.com/super-d2/p/6407255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!