@Test public void testLruCache() { LRUCache<Integer, Object> lruCache = new LRUCache<>(MAX_CAPACITY, entry -> { evictedEntryList.add(((Map.Entry<Integer, Object>) entry).getKey()); }); for (int i = 0; i < 10; i++) { lruCache.putIfAbsent(i, i); } assertEquals(lruCache.size(), MAX_CAPACITY); assertEquals(evictedEntryList.size(), 5); for (int i = 0; i < 5; i++) { assertNull(lruCache.get(i)); assertNotNull(evictedEntryList.get(evictedEntryList.indexOf(i))); } for (int i = 5; i < 10; i++) { assertNotNull(lruCache.get(i)); } List<Integer> list = new ArrayList<>(Arrays.asList(6, 5, 8, 7, 9)); for (Integer idx : list) { assertNotNull(lruCache.get(idx)); } assertEquals(list, new ArrayList<>(lruCache.keySet())); lruCache.remove(5); lruCache.remove(6); assertNull(lruCache.get(5)); assertNull(lruCache.get(6)); int idx = list.indexOf(5); list.remove(idx); idx = list.indexOf(6); list.remove(idx); assertEquals(list, new ArrayList<>(lruCache.keySet())); lruCache.remove(7); lruCache.remove(8); lruCache.remove(9); assertTrue(lruCache.isEmpty()); }
public LRUCache(int maxCapacity, Consumer<Map.Entry<K, V>> cleanupFunc) { super(maxCapacity, DEFAULT_LOAD_FACTOR, true); this.maxCapacity = maxCapacity; this.cleanupFunc = cleanupFunc; }
LRUCache extends LinkedHashMap<K, V> { public LRUCache(int maxCapacity, Consumer<Map.Entry<K, V>> cleanupFunc) { super(maxCapacity, DEFAULT_LOAD_FACTOR, true); this.maxCapacity = maxCapacity; this.cleanupFunc = cleanupFunc; } }
LRUCache extends LinkedHashMap<K, V> { public LRUCache(int maxCapacity, Consumer<Map.Entry<K, V>> cleanupFunc) { super(maxCapacity, DEFAULT_LOAD_FACTOR, true); this.maxCapacity = maxCapacity; this.cleanupFunc = cleanupFunc; } LRUCache(int maxCapacity, Consumer<Map.Entry<K, V>> cleanupFunc);  LRUCache(int maxCapacity, float loadFactor, Consumer<Map.Entry<K, V>> cleanupFunc); }
LRUCache extends LinkedHashMap<K, V> { public LRUCache(int maxCapacity, Consumer<Map.Entry<K, V>> cleanupFunc) { super(maxCapacity, DEFAULT_LOAD_FACTOR, true); this.maxCapacity = maxCapacity; this.cleanupFunc = cleanupFunc; } LRUCache(int maxCapacity, Consumer<Map.Entry<K, V>> cleanupFunc);  LRUCache(int maxCapacity, float loadFactor, Consumer<Map.Entry<K, V>> cleanupFunc);  }
LRUCache extends LinkedHashMap<K, V> { public LRUCache(int maxCapacity, Consumer<Map.Entry<K, V>> cleanupFunc) { super(maxCapacity, DEFAULT_LOAD_FACTOR, true); this.maxCapacity = maxCapacity; this.cleanupFunc = cleanupFunc; } LRUCache(int maxCapacity, Consumer<Map.Entry<K, V>> cleanupFunc);  LRUCache(int maxCapacity, float loadFactor, Consumer<Map.Entry<K, V>> cleanupFunc);   }