使用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
相关推荐
这个demo主要讲解了LinkedHashmap的使用,希望可以帮助需要的同学.
如何用LinkedHashMap实现LRU? 如何用TreeMap实现一致性hash? ConcurrentHashMap是如何在保证并发安全的同时提高性能? ConcurrentHashMap是如何让多线程同时参与扩容? LinkedBlockingQueue、DelayQueue是如何实现...
主要介绍了Java使用LinkedHashMap进行分数排序的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
基于JavaLinkedHashMap实现的 LRU 算法 algorithm.consistentHashing ConsistentHash: 一致性Hash算法 algorithm.cap algorithm.subset 给一个set打印出所有子集 jdk jdk 知识 jdk.autoboxing 自动装箱拆箱 jdk....
LinkedHashMap源代码,Java中Map的一种实现子类。
分成三级对图片进行缓存,第一级采用LinkedHashMap(LRU算法),第二季采用ConcurrentHashMap线程安全控制
这是关于Java学习的主要针对LinkedHashMap的实现原理
Set是使用LinkedHashMap在Go(Golang)中简单的Set数据结构实现。 该库允许您获取一组int64或string而没有重复的项目。 用法 package main import ( "fmt" "github.com/StudioSol/set" ) func main () { ...
ava基础 基础知识 ...Java集合详解5:深入理解LinkedHashMap和LRU缓存 Java集合详解6:TreeMap和红黑树 Java集合详解7:HashSet,TreeSet与LinkedHashSet Java集合详解8:Java集合类细节精讲 JavaWeb
(当然也可以用hashbrown )用法将ritelinked添加到Cargo.toml :ritelinked =" x.y.z"写一些这样的代码:letmut lru_cache= LinkedHashMap::new ();let key="key" .to_owned ();let _cached_val= lru_cache .raw_...
LinkedHashMap是Java中的一种特殊类型的HashMap,它保留了插入顺序,即按照元素插入的先后顺序进行排序
java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
Android提供了LRUCache类,可以方便的使用它来实现LRU算法的缓存。Java提供了LinkedHashMap,可以用该类很方便的实现LRU算法,Java的LRULinkedHashMap是直接继承了LinkedHashMap,进行了极少的改动后可以实现LRU...
HashMap,HashTable,LinkedHashMap,TreeMap的区别
深入Java集合学习系列(四): LinkedHashMap的实现原理
要注意一点的是LinkedHashMap是可以实现LRU缓存策略的,前提是你需要将LinkedHashMap中的accessorder属性设置为true。 因此你基本可以认为LinkedHashMap是LinkedList和HashMap的一个组合。 LinkedHashMap简介 ...
Android提供了LRUCache类,可以方便的使用它来实现LRU算法的缓存。Java提供了LinkedHashMap,可以用该类很方便的实现LRU算法,Java的LRULinkedHashMap就是直接继承了LinkedHashMap,进行了极少的改动后就可以实现LRU...
java12-fundamentals-cache-implementations-workshop 参考 前言 本次研讨会的目标 理解 LRU 缓存的概念 理解 LFU 缓存的概念 实现 LRU 和 LFU 缓存 看看守卫在列表实现中是如何有用的 工作坊: lfu.workshop , lru....
RiteLinked-类似HashMap的容器,以用户可控制的顺序保存其键值对 提供了LinkedHashMap和... let mut lru_cache = LinkedHashMap :: new (); let key = "key" . to_owned (); let _cached_val = lru_cache .