Java Collection Tutorial - Java LinkedHashMap .removeEldestEntry ( Map .Entry < K , V > eldest)








Syntax

LinkedHashMap.removeEldestEntry(Map.Entry < K , V > eldest) has the following syntax.

protected boolean removeEldestEntry(Map.Entry <K , V> eldest)

Example

In the following code shows how to use LinkedHashMap.removeEldestEntry(Map.Entry < K , V > eldest) method.

/*w  w  w . j  a  v  a  2s  . c om*/
import java.util.*;

public class Main {

   private static final int MAX_ENTRIES = 5;

   public static void main(String[] args) {
      LinkedHashMap  lhm = new LinkedHashMap(MAX_ENTRIES + 1, .75F, false) {

         protected boolean removeEldestEntry(Map.Entry  eldest) {
            return size() >  MAX_ENTRIES;
         }
      };
      lhm.put(0, "H");
      lhm.put(1, "E");
      lhm.put(2, "L");
      lhm.put(3, "L");
      lhm.put(4, "O");

      System.out.println(lhm);

   }
}

The code above generates the following result.