Search code examples
javalinkedhashmap

What is the point of the Map.Entry parameter to LinkedHashMap#removeEldestEntry?


The documentation describes this method using the following example:

private static final int MAX_ENTRIES = 100;

protected boolean removeEldestEntry(Map.Entry eldest) {
   return size() > MAX_ENTRIES;
}

In this example, the method does not use the parameter, eldest. What is the point of this parameter to LinkedHashMap#removeEldestEntry?


Solution

  • The code you quote is just an example of how to override that method. The actual implementation is:

    protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
        return false;
    }
    

    It is meant to be overridden, and some implementations can examine the eldest entry to decide whether or not it should be removed.