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 覆盖一方法即可)