使用 LinkedHashMap 实现 LRU 算法
2017-07-20 15:38
633 查看
LRU是Least Recently Used 的缩写,翻译过来就是“最近最少使用”,LRU缓存就是使用这种原理实现,简单的说就是缓存一定量的数据,当超过设定的阈值时就把一些过期的数据删除掉,比如我们缓存10000条数据,当数据小于10000时可以随意添加,当超过10000时就需要把新的数据添加进来,同时要把过期数据删除,以确保我们最大缓存10000条。
LinkedHashMap自身已经实现了LRU功能,请看如下的LinkedHashMap.java源码
现在我们要做的就是两件事:
- 设置accessOrder为true
- 重写removeEldestEntry方法,让它在一定条件下返回true,这样当LinkedHashMap内容填满时,新来的值会挤掉LRU的值。
输出结果如下:
null
a1
LinkedHashMap自身已经实现了LRU功能,请看如下的LinkedHashMap.java源码
/** * The iteration ordering method for this linked hash map: <tt>true</tt> * for access-order, <tt>false</tt> for insertion-order. * * @serial */ final boolean accessOrder;
/** * Returns <tt>true</tt> if this map should remove its eldest entry. * This method is invoked by <tt>put</tt> and <tt>putAll</tt> after * inserting a new entry into the map. It provides the implementor * with the opportunity to remove the eldest entry each time a new one * is added. This is useful if the map represents a cache: it allows * the map to reduce memory consumption by deleting stale entries. * * <p>Sample use: this override will allow the map to grow up to 100 * entries and then delete the eldest entry each time a new entry is * added, maintaining a steady state of 100 entries. * <pre> * private static final int MAX_ENTRIES = 100; * * protected boolean removeEldestEntry(Map.Entry eldest) { * return size() > MAX_ENTRIES; * } * </pre> * * <p>This method typically does not modify the map in any way, * instead allowing the map to modify itself as directed by its * return value. It <i>is</i> permitted for this method to modify * the map directly, but if it does so, it <i>must</i> return * <tt>false</tt> (indicating that the map should not attempt any * further modification). The effects of returning <tt>true</tt> * after modifying the map from within this method are unspecified. * * <p>This implementation merely returns <tt>false</tt> (so that this * map acts like a normal map - the eldest element is never removed). * * @param eldest The least recently inserted entry in the map, or if * this is an access-ordered map, the least recently accessed * entry. This is the entry that will be removed it this * method returns <tt>true</tt>. If the map was empty prior * to the <tt>put</tt> or <tt>putAll</tt> invocation resulting * in this invocation, this will be the entry that was just * inserted; in other words, if the map contains a single * entry, the eldest entry is also the newest. * @return <tt>true</tt> if the eldest entry should be removed * from the map; <tt>false</tt> if it should be retained. */ protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { return false; }
现在我们要做的就是两件事:
- 设置accessOrder为true
- 重写removeEldestEntry方法,让它在一定条件下返回true,这样当LinkedHashMap内容填满时,新来的值会挤掉LRU的值。
public class Main { static public class LRULinkedHashMap<K, V> extends LinkedHashMap<K, V> { private int capacity; LRULinkedHashMap(int capacity){ super(16, 0.75f, true); this.capacity = capacity; } @Override public boolean removeEldestEntry(Map.Entry<K, V> eldest){ return size() > capacity; } } public static void main(String[] args) { LRULinkedHashMap<String, String> map = new LRULinkedHashMap<>(4); map.put("rcx1", "a1"); //queue: rcx1 map.put("rcx2", "a2"); //queue: rcx2-rcx1 map.put("rcx3", "a3"); //queue: rcx3-rcx2-rcx1 //map.get("rcx1"); //queue will be changed to: rcx1-rcx3-rcx2 map.put("rcx4", "a4"); //queue: rcx4-rcx3-rcx2-rcx1 map.put("rcx5", "a5"); //queue: rcx5-rcx4-rcx3-rcx2, rcx1 will be removed since the capacity is 4. System.out.println(map.get("rcx1")); LinkedHashMap<String, String> map2 = new LinkedHashMap<>(4); map2.put("rcx1", "a1"); map2.put("rcx2", "a2"); map2.put("rcx3", "a3"); map2.put("rcx4", "a4"); map2.put("rcx5", "a5"); System.out.println(map2.get("rcx1")); } }
输出结果如下:
null
a1
相关文章推荐
- LRU算法的实现使用linkedHashMap方法,以及使用双链表+hashtable方法
- 使用LinkedHashMap来实现一个使用LRU(Least Recently Used)算法的cache
- Android使用LinkedHashMap实现一个LRU算法的内存缓存
- 使用 Java.util.LinkedHashMap 实现 LRU、FIFO 算法
- 近期最久未使用页面淘汰算法———LRU算法(java实现)
- leetcode LRU Cache(高级缓存的最近最少使用算法实现)
- 转:LinkedHashMap使用(可以用来实现LRU缓存)
- 使用LinkedHashMap实现LRU缓存
- 基于LinkedHashMap实现LRU缓存调度算法原理
- 最近最久未使用页面淘汰算法———LRU算法(java实现)
- LinkedHashMap实现一个固定容量的基于最近最少使用算法的缓存类
- 利用LinkedHashMap实现LRU算法缓存
- java基础解析系列(四)---LinkedHashMap的原理及LRU算法的实现
- 用LinkedHashMap实现的LRU算法
- 最近最久未使用页面淘汰算法———LRU算法(java实现)
- LinkedHashMap 和 LRU算法实现
- java基础解析系列(四)---LinkedHashMap的原理及LRU算法的实现
- LinkedHashMap的实现原理(LRU算法)
- 剖析LRU算法及LinkedHashMap源码实现机制
- java基础解析系列(四)---LinkedHashMap的原理及LRU算法的实现