LRUCache extends LinkedHashMap<K, V> { public LRUCache(int cacheSize) { super((int) Math.ceil(cacheSize / DEFAULT_LOAD_FACTOR) + 1, DEFAULT_LOAD_FACTOR, true); if (cacheSize < 0) { throw new IllegalArgumentException("cacheSize is negative."); } this.cacheSize = cacheSize; } LRUCache(int cacheSize);   }
@Test public void testLRUCache() throws Exception { assertEquals("Should be initial empty.", 0, cache.size()); assertEquals("Should be initial empty.", true, cache.isEmpty()); for (int i = 0 ; i < 100; i++) { cache.put(String.valueOf(i), i); assertEquals("Should be the right size.", i+1, cache.size()); } for (int i = 100 ; i < 20000000; i++) { cache.put(String.valueOf(i), i); } assertEquals("Should keep latest element.", 20000000 - 1, cache.get(String.valueOf(20000000-1))); assertFalse("Should not keep the obsolete element.", cache.containsKey(String.valueOf(20000000 - 101))); }