LinkedHashMap的get()方法除了返回元素之外还可以把被访问的元素放到链表的底端,这样一来每次顶端的元素就是remove的元素。
构造函数如下:
public LinkedHashMap (int initialCapacity, float loadFactor, boolean accessOrder);
initialCapacity 初始容量
loadFactor 加载因子,一般是 0.75f
accessOrder false基于插入顺序,true 基于访问顺序(get一个元素后,这个元素被加到最后,使用了LRU 最近最少被使用的调度算法)
import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.locks.ReentrantLock; /** * 该类是线程安全的带有容量限制的LinkedHashMap * 超容量的元素采用LRU方式删除 * 线程安全仅限于已经覆盖的几个方法,若有其他需要可自行添加 * 迭代操作不保证数据的一致性,即使remove方法是线程安全的也会抛ConcurrentModificationException */ public class LinkedMapLRU<K, V> extends LinkedHashMap<K, V> { private static final long serialVersionUID = -7911712053305433954L; private int capacity; private final ReentrantLock lock = new ReentrantLock(); public LinkedMapLRU(int capacity) { super(capacity, 0.75f, true); this.capacity = capacity; } @Override public V put(K key, V value) { try { lock.lock(); return super.put(key, value); } finally { lock.unlock(); } } @Override public V get(Object key) { try { lock.lock(); return super.get(key); } finally { lock.unlock(); } } @Override public V remove(Object key) { try { lock.lock(); return super.remove(key); } finally { lock.unlock(); } } @Override public boolean removeEldestEntry(Map.Entry<K,V> eldest) { if(size() > capacity) {
System.out.println(eldest.getKey() +":" + eldest.getValue()); return true; } return false; } public void setMaxSize(int size) { this.capacity = size; } }
测试类:
LinkedMapLRU<Integer, Integer> LRU = new LinkedMapLRU<Integer, Integer>(3); LRU.put(3, 1); LRU.put(6, 2); LRU.put(9, 3); LRU.get(3); //key=3移到顶部 LRU.put(12, 4); LRU.put(15, 5); for(Map.Entry<Integer, Integer> entry : LRU.entrySet()) { System.out.println(entry.getKey() + " " +entry.getValue()); }
测试结果:
6:2
9:3
3 1
12 4
15 5