LRU Cache Design using LinkedHashMap This is a follow-on qestion from: LRU cache design

Revised Implementation

package lrucache;

import java.util.LinkedHashMap;


public class LRUCacheV2&lt;K, V&gt; extends LinkedHashMap&lt;K, V&gt;{

    private static final int MAX_ENTRIES = 3;
         /**
     * super class constructor arguments are
     * cache size = MAX_ENTRIES
     * load factor = 0.75F
     * access order = true
     */
    public LRUCacheV2(){
        super(MAX_ENTRIES,0.75F,true);
    }

    @Override
    protected boolean removeEldestEntry(java.util.Map.Entry&lt;K, V&gt; entry) {
        return size() &gt; MAX_ENTRIES;
    }

    public static void main(String[] args) {
        LRUCacheV2&lt;String, Integer&gt; cache = new LRUCacheV2&lt;String, Integer&gt;();
        cache.put("abc", 1);
        cache.put("def", 2);
        cache.put("ghi", 3);
        cache.put("jkl", 4);
        cache.put("mno", 5);
        cache.put("abc", 1);
        System.out.println(cache);
    }
}


Output

{jkl=4, mno=5, abc=1}
Your code looks fine, depending on the use case in which it is applied. A few people have mentioned "composition over inheritance". This is a good point, but it is not directly practical in this situation. You need inheritance in order to get access to the functionality you use in the LinkedHashMap. But, there is an approach where you use inheritance to access the functionality, and composition to expose it.

If you assume the direct approach you have taken is useful for accessing additional functionality from the LinkedHashMap, then your code is good. The variables are well named, the code is consistent (there's a glitch on the one comment line...), and it's all fine.

Whether the hybrid approach is useful to you depends on whether your use case requires just a smallish subset of the the LinkedHashMap functionality. For example, if you needed to access the entries on the content as Map.Entry instances, or if you need to do more complicated iterations, then you start to lose the benefit of the composition.

In this case, the first approach you have using straight inheritance has a place. Th secnd approach requires wrapping the functionality you need from the private inner cache.

This second approach requires tailoring your LRU cache to its specific use cases. At the moment, you have a very general LRU cache. On the other hand, you are only using the put method (and presumably the get()). You can create a class with just these methods (and a selection of other ones that make sense) and gave an internal inherited instance for access to the LinkedHashMap. Consider the following:

public final LRUCacheVC&lt;K,V&gt;() {

    // use inheritence to access LinkedHashMap functionality.
    private static class AccessOrderCache&lt;T,U&gt; extends LinkedHashMap&lt;T,U&gt; {
        private final int capacity;
        AccessOrderCache(final int capacity) {
            super(capacity, 0.75f, true);
            this.capacity = capacity;
        }
        @Override
        protected boolean removeEldestEntry(java.util.Map.Entry&lt;T, U&gt; entry) {
            return size() &gt; MAX_ENTRIES;
        }
    }


    // Use composition to access the Inherited Cache.    
    private final AccessOrderCache&lt;K,V&gt; cache;

    public LRUCacheVC(final int capacity) {
        cache = new AccessOrderCache&lt;&gt;(capacity);
    }


    public V get(K key) {
        return cache.get(key);
    }

    public V put(K key, V value) {
        return cache.put(key, value)
    }

    @Override
    public int hashCode() {
        return cache.hashCode();
    }

    @Override
    public boolean equals(Object other) {
        return other != null 
            &amp;&amp; (other instanceof LRUCacheVC)
            &amp;&amp; cache.equals(((LRUCacheVC)other).cache);
    }

    @Override
    public String toString() {
        return cache.toString();
    }
}


Note how you use inheritence to access the LinkedHashMap concepts, then use composition to incorporate that new class in to your code.

You only expose that limited functionality that you want (get and put), as well as a few other standard methods that make life easier.