- 基于LinkedHashMap实现LRUCache
public class LRUCache2 extends LinkedHashMap { private int size; private HashMap map; public LRUCache2(int size) { this.size = size; this.map = new LinkedHashMap (size, 0.75f, true) { @Override protected boolean removeEldestEntry(Map.Entry eldest) { return size() > size; } }; } public V getValue(K key) { if (this.map.containsKey(key)) { return this.map.get(key); } else { return null; } } public void set(K key, V value) { this.map.put(key, value); } public void print() { this.map.forEach((k, v) -> System.out.println(k + "\t" + v)); }}
public class LRUCacheDemo { public static void main(String[] args) { LRUCache2 cache = new LRUCache2(3); cache.set(1, "One"); cache.set(2, "Two"); cache.set(3, "Three"); cache.print(); System.out.println("----------------------"); // 尝试获取,提高1和3的使用率 cache.getValue(1); cache.getValue(3); cache.getValue(3); // 容器已经满了,插入4的时候会覆盖最少使用的为2 cache.set(4, "Four"); cache.print(); }}