@Test public void lru2() { final LinkedHashMap<Integer, String> tested = new LinkedHashMap<Integer, String>() { @Override protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) { return size() > 1000; } }; for (int i = 0; i < 1000; ++i) { tested.put(i, Integer.toString(i)); } Assert.assertEquals(Integer.toString(0), tested.get(0)); for (int i = 1000; i < 1999; ++i) { tested.put(i, Integer.toString(i)); } Assert.assertEquals(Integer.toString(0), tested.get(0)); tested.put(2000, Integer.toString(2000)); Assert.assertNull(tested.get(1000)); }
@Override public V put(final K key, final V value) { final Entry<K, V>[] table = this.table; final int hash = key.hashCode(); final int index = HashUtil.indexFor(hash, table.length, mask); for (Entry<K, V> e = table[index]; e != null; e = e.hashNext) { final K entryKey; if ((entryKey = e.key) == key || entryKey.equals(key)) { moveToTop(e); return e.setValue(value); } } final Entry<K, V> e = new Entry<>(key, value); e.hashNext = table[index]; table[index] = e; final Entry<K, V> top = this.top; e.next = top; if (top != null) { top.previous = e; } else { back = e; } this.top = e; _size += 1; if (removeEldestEntry(back)) { remove(eldestKey()); } else if (_size > capacity) { rehash(HashUtil.nextCapacity(capacity)); } return null; }
LinkedHashMap extends AbstractHashMap<K, V> { @Override public V put(final K key, final V value) { final Entry<K, V>[] table = this.table; final int hash = key.hashCode(); final int index = HashUtil.indexFor(hash, table.length, mask); for (Entry<K, V> e = table[index]; e != null; e = e.hashNext) { final K entryKey; if ((entryKey = e.key) == key || entryKey.equals(key)) { moveToTop(e); return e.setValue(value); } } final Entry<K, V> e = new Entry<>(key, value); e.hashNext = table[index]; table[index] = e; final Entry<K, V> top = this.top; e.next = top; if (top != null) { top.previous = e; } else { back = e; } this.top = e; _size += 1; if (removeEldestEntry(back)) { remove(eldestKey()); } else if (_size > capacity) { rehash(HashUtil.nextCapacity(capacity)); } return null; } }
LinkedHashMap extends AbstractHashMap<K, V> { @Override public V put(final K key, final V value) { final Entry<K, V>[] table = this.table; final int hash = key.hashCode(); final int index = HashUtil.indexFor(hash, table.length, mask); for (Entry<K, V> e = table[index]; e != null; e = e.hashNext) { final K entryKey; if ((entryKey = e.key) == key || entryKey.equals(key)) { moveToTop(e); return e.setValue(value); } } final Entry<K, V> e = new Entry<>(key, value); e.hashNext = table[index]; table[index] = e; final Entry<K, V> top = this.top; e.next = top; if (top != null) { top.previous = e; } else { back = e; } this.top = e; _size += 1; if (removeEldestEntry(back)) { remove(eldestKey()); } else if (_size > capacity) { rehash(HashUtil.nextCapacity(capacity)); } return null; } LinkedHashMap();  LinkedHashMap(int capacity);  LinkedHashMap(int capacity, float loadFactor); }
LinkedHashMap extends AbstractHashMap<K, V> { @Override public V put(final K key, final V value) { final Entry<K, V>[] table = this.table; final int hash = key.hashCode(); final int index = HashUtil.indexFor(hash, table.length, mask); for (Entry<K, V> e = table[index]; e != null; e = e.hashNext) { final K entryKey; if ((entryKey = e.key) == key || entryKey.equals(key)) { moveToTop(e); return e.setValue(value); } } final Entry<K, V> e = new Entry<>(key, value); e.hashNext = table[index]; table[index] = e; final Entry<K, V> top = this.top; e.next = top; if (top != null) { top.previous = e; } else { back = e; } this.top = e; _size += 1; if (removeEldestEntry(back)) { remove(eldestKey()); } else if (_size > capacity) { rehash(HashUtil.nextCapacity(capacity)); } return null; } LinkedHashMap();  LinkedHashMap(int capacity);  LinkedHashMap(int capacity, float loadFactor); @Override V put(final K key, final V value); @Override V remove(final Object key); V removeEldest(); }
LinkedHashMap extends AbstractHashMap<K, V> { @Override public V put(final K key, final V value) { final Entry<K, V>[] table = this.table; final int hash = key.hashCode(); final int index = HashUtil.indexFor(hash, table.length, mask); for (Entry<K, V> e = table[index]; e != null; e = e.hashNext) { final K entryKey; if ((entryKey = e.key) == key || entryKey.equals(key)) { moveToTop(e); return e.setValue(value); } } final Entry<K, V> e = new Entry<>(key, value); e.hashNext = table[index]; table[index] = e; final Entry<K, V> top = this.top; e.next = top; if (top != null) { top.previous = e; } else { back = e; } this.top = e; _size += 1; if (removeEldestEntry(back)) { remove(eldestKey()); } else if (_size > capacity) { rehash(HashUtil.nextCapacity(capacity)); } return null; } LinkedHashMap();  LinkedHashMap(int capacity);  LinkedHashMap(int capacity, float loadFactor); @Override V put(final K key, final V value); @Override V remove(final Object key); V removeEldest();  }