使用LinkedHashMap构建LRU的Cache
来源:互联网 发布:java程序员委培班 编辑:程序博客网 时间:2024/05/21 16:22
使用LinkedHashMap构建LRU的Cache
package tags;import java.util.LinkedHashMap;public class LRUCache<K,V> extends LinkedHashMap<K,V> { /** 最大数据存储容量 */ private static final int LRU_MAX_CAPACITY = 1024; /** 存储数据容量 */ private int capacity; public LRUCache() { super(); } public LRUCache(int initialCapacity, float loadFactor,Boolean useCache) { super(initialCapacity, loadFactor, useCache); capacity = LRU_MAX_CAPACITY; } public LRUCache(int initialCapacity, float loadFactor,Boolean useCache, int lruCapacity) { super(initialCapacity, loadFactor, true); this.capacity = lruCapacity; } @Override protected boolean removeEldestEntry(Map.Entry<K,V> eldest) { System.out.println(eldest.getKey() + "=" + eldest.getValue()); return size () > LRUCache.this.capacity; } public static void main(String[] args) { LRUCache<String, String> map = new LRUCache<String, String>(16, 0.75f,true); map.put("a", "a"); //a a map.put("b", "b"); //a a b map.put("c", "c"); //a a b c map.put("a", "a2"); // b c a map.put("d", "d"); //b b c a d map.put("a", "a3"); // b c d a map.put("b", "b2"); // c d a b map.put("f", "f"); //c c d a b f map.put("g", "g"); //c c d a b f g map.get("d"); //c a b f g d for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getValue() + ", "); } System.out.println(); /*map.get("a"); //c b f g d a for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getValue() + ", "); } System.out.println(); map.get("c"); //b f g d a c for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getValue() + ", "); } System.out.println(); map.get("b"); //f g d a c b for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getValue() + ", "); } System.out.println(); map.put("h", "h"); //f f g d a c b h for (Entry<String, String> entry : map.entrySet()) { System.out.print(entry.getValue() + ", "); } System.out.println(); */ }}
结果:
a=a
a=a
a=a
b=b
c=c
c=c
c, a3, b2, f, g, d,
c, b2, f, g, d, a3,
b2, f, g, d, a3, c,
f, g, d, a3, c, b2,
f=f
f, g, d, a3, c, b2, h,
LinkedHashMap : http://uule.iteye.com/admin/blogs/1522291
0 0
- 使用LinkedHashMap构建LRU的Cache
- 使用LinkedHashMap构建LRU的Cache
- 用LinkedHashMap实现的简单LRU Cache
- LinkedHashMap as LRU cache
- 使用LinkedHashMap实现的LRU算法
- LRU Cache 暨LinkedHashMap源码阅读
- Java集合5:LinkedHashMap与LRU cache
- 使用LinkedHashMap实现LRU缓存
- 使用 LinkedHashMap 实现 LRU 算法
- Android使用LinkedHashMap实现一个LRU算法的内存缓存
- LRU cache的实现
- LRU Cache的实现
- LRU Cache的实现
- LRU Cache的实现
- An LRU cache class based on java.util.LinkedHashMap
- LRU Cache:最近最久未使用
- LRU (redis 的LRU 加自己实现的linkedHashMap)
- LRU Cache 最近最少使用缓存的设计
- Intent和PendingIntent的区别
- Java Object and Class基本
- Log4j使用
- Android中pendingIntent的深入理解
- 题目1038:Sum of Factorials
- 使用LinkedHashMap构建LRU的Cache
- Android LayoutInflater详解
- 谈layout_gravity和gravity的用法
- shell脚本数组报错Syntax error: "(" unexpected
- Spring AOP实现(以log管理为例)
- 数据结构总结
- android Handler post sendMessage
- annot refer to a non-final variable * inside an inner class defined in a different method"错误解析
- 权限控制和session验证的实现