java LRUMap经典实现

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.aviation.utils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *LRU 缓存map
 * @author gaoxingliang
 */
public class LRUMap extends LinkedHashMap {
    
    private int maxSize=20;
    
    public LRUMap(int size){
        super(size,0.75f,true);
        this.maxSize=size;
    };
    
    protected boolean removeEldestEntry(Map.Entry eldest) {
        return (size()>maxSize);
    }
}

这个是偶然在阅读hibernate源码时发现的

LRUMap可以方便的用于缓存实现

代码如下:(只需要继承LinkedHashMap 覆盖一方法即可)


posted on 2013-04-01 19:45  scugxl  阅读(591)  评论(0编辑  收藏  举报

导航