LRUCache

  1. import java.util.LinkedHashMap;  
  2. import java.util.concurrent.locks.Lock;  
  3. import java.util.concurrent.locks.ReentrantLock;  
  4.   
  5. public class LRUCache<K, V> extends LinkedHashMap<K, V> {  
  6.   
  7.     private static final long serialVersionUID = -656812640814484468L;  
  8.   
  9.     /** 默认最大容量 */  
  10.     private static final int DEFAULT_MAX_CAPACITY = 300;  
  11.   
  12.     private static final int INITIALCAPACITY = 16;  
  13.   
  14.     /** 负载因子 */  
  15.     private static final float DEFAULT_LOAD_FACTOR = 0.75f;  
  16.   
  17.     private final Lock lock = new ReentrantLock();  
  18.   
  19.     private volatile int maxCapacity;  
  20.   
  21.     /** 
  22.      * <br> 
  23.      * ------------------------------<br> 
  24.      */  
  25.     public LRUCache() {  
  26.         this(DEFAULT_MAX_CAPACITY);  
  27.     }  
  28.   
  29.     public LRUCache(int maxCapacity) {  
  30.         super(INITIALCAPACITY, DEFAULT_LOAD_FACTOR, Boolean.TRUE);  
  31.         this.maxCapacity = maxCapacity;  
  32.     }  
  33.   
  34.     @Override  
  35.     protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {  
  36.         return size() > maxCapacity;  
  37.     }  
  38.   
  39.     @Override  
  40.     public V get(Object key) {  
  41.         try {  
  42.             lock.lock();  
  43.             return super.get(key);  
  44.         } finally {  
  45.             lock.unlock();  
  46.         }  
  47.     }  
  48.   
  49.     public V put(K key, V value) {  
  50.         try {  
  51.             lock.lock();  
  52.             return super.put(key, value);  
  53.         } finally {  
  54.             lock.unlock();  
  55.         }  
  56.     }  
  57.   
  58.     @Override  
  59.     public V remove(Object key) {  
  60.         try {  
  61.             lock.lock();  
  62.             return super.remove(key);  
  63.         } finally {  
  64.             lock.unlock();  
  65.         }  
  66.     }  
  67.   
  68.     @Override  
  69.     public boolean containsKey(Object key) {  
  70.         try {  
  71.             lock.lock();  
  72.             return super.containsKey(key);  
  73.         } finally {  
  74.             lock.unlock();  
  75.         }  
  76.     }  
  77.   
  78.     @Override  
  79.     public void clear() {  
  80.         try {  
  81.             lock.lock();  
  82.             super.clear();  
  83.         } finally {  
  84.             lock.unlock();  
  85.         }  
  86.     }  
  87.   
  88.     @Override  
  89.     public int size() {  
  90.         try {  
  91.             lock.lock();  
  92.             return super.size();  
  93.         } finally {  
  94.             lock.unlock();  
  95.         }  
  96.     }  
  97.   
  98.     /** 
  99.      * 获得maxCapacity 
  100.      * @return the maxCapacity 
  101.      */  
  102.     public int getMaxCapacity() {  
  103.         return maxCapacity;  
  104.     }  
  105.   
  106.     /** 
  107.      * 设置maxCapacity 
  108.      * @param maxCapacity the maxCapacity to set 
  109.      */  
  110.     public void setMaxCapacity(int maxCapacity) {  
  111.         this.maxCapacity = maxCapacity;  
  112.     }  
  113. }  
import java.util.LinkedHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LRUCache<K, V> extends LinkedHashMap<K, V> {

	private static final long serialVersionUID = -656812640814484468L;

	/** 默认最大容量 */
	private static final int DEFAULT_MAX_CAPACITY = 300;

	private static final int INITIALCAPACITY = 16;

	/** 负载因子 */
	private static final float DEFAULT_LOAD_FACTOR = 0.75f;

	private final Lock lock = new ReentrantLock();

	private volatile int maxCapacity;

	/**
	 * <br>
	 * ------------------------------<br>
	 */
	public LRUCache() {
		this(DEFAULT_MAX_CAPACITY);
	}

	public LRUCache(int maxCapacity) {
		super(INITIALCAPACITY, DEFAULT_LOAD_FACTOR, Boolean.TRUE);
		this.maxCapacity = maxCapacity;
	}

	@Override
	protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {
		return size() > maxCapacity;
	}

	@Override
	public V get(Object key) {
		try {
			lock.lock();
			return super.get(key);
		} finally {
			lock.unlock();
		}
	}

	public V put(K key, V value) {
		try {
			lock.lock();
			return super.put(key, value);
		} finally {
			lock.unlock();
		}
	}

	@Override
	public V remove(Object key) {
		try {
			lock.lock();
			return super.remove(key);
		} finally {
			lock.unlock();
		}
	}

	@Override
	public boolean containsKey(Object key) {
		try {
			lock.lock();
			return super.containsKey(key);
		} finally {
			lock.unlock();
		}
	}

	@Override
	public void clear() {
		try {
			lock.lock();
			super.clear();
		} finally {
			lock.unlock();
		}
	}

	@Override
	public int size() {
		try {
			lock.lock();
			return super.size();
		} finally {
			lock.unlock();
		}
	}

	/**
	 * 获得maxCapacity
	 * @return the maxCapacity
	 */
	public int getMaxCapacity() {
		return maxCapacity;
	}

	/**
	 * 设置maxCapacity
	 * @param maxCapacity the maxCapacity to set
	 */
	public void setMaxCapacity(int maxCapacity) {
		this.maxCapacity = maxCapacity;
	}
}
  1. public class Main {  
  2.       
  3.     public static void main(String[] args) {  
  4.         LRUCache<String, String> lruCache = new LRUCache<String, String>(2);  
  5.         lruCache.put("1""1");  
  6.         lruCache.put("2""2");  
  7.         System.out.println(lruCache);  
  8.         lruCache.put("3""3");  
  9.         System.out.println(lruCache);  
  10.     }  
  11. }  
public class Main {
	
	public static void main(String[] args) {
		LRUCache<String, String> lruCache = new LRUCache<String, String>(2);
		lruCache.put("1", "1");
		lruCache.put("2", "2");
		System.out.println(lruCache);
		lruCache.put("3", "3");
		System.out.println(lruCache);
	}
}

{1=1, 2=2}
{2=2, 3=3}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值