title: How does LinkedHashMap work under the hood?
body: <p>I read about <a href="http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html" rel="nofollow noreferrer">LinkedHashMap</a> and from the description (although very interesting) I couldn't understand how it actually does its job under the hood. As a side note, I know how a <code>HashMap</code> works underneath in Java.<br>
  So I review the source and still can not figure out how it works. Perhaps I am not grasping something fundamental in OOP in this case so bear with me.<br>
  To summarize the part that is confusing to me is the following:<br>
  The <code>LinkedHashMap</code> delegates all the calls to its parent <code>HashMap</code>.<br>
  Internally it overrides the <code>HashMap.Entry</code> to implement the various <code>recordAccess</code> and <code>recordRemoval</code> methods which seem to implement the logic of the <code>LinkedHashMap</code><br>
  But the actual <code>Entries</code> are inside the table of the base class i.e. the <code>HashMap</code> which instantiates a table of <code>HashMap.Entry</code> and not of <code>LinkedHashMap.Entry</code>.<br>
  So I can't figure out how the various <code>recordAccess</code> and <code>recordRemove</code> etc are actually being called.<br>
  So can anyone help me understand what's going on here?<br>
  Am I correct to think that somehow the <code>LinkedHashedMap.Entry</code> is the type of table created by the <code>HashMap</code>? But how?   </p>
  
  <p><strong>UPDATE:</strong><br>
  My question is how do the <code>recordAccess</code> are being called. My experiment on this using a derived version of <code>HashMap</code> failed for the reason of Shengyuan Lu (+1) - My bad there   </p>
  
  <p><strong>UPDATE:</strong><br>
  The following which I tried is the same (I think) as what the <code>LinkedHashMap</code> is doing:  </p>
  
  <pre><code>package delete;  
  
  public class Base&lt;T&gt; {  
  
      Entry&lt;T&gt;[] table;  
      int idx = 0;  
      @SuppressWarnings("unchecked")  
      public Base(){  
          System.out.println("In base");  
          table = new Entry[10];  
      }
  
      public void add(T x){  
          table[idx] = new Entry(x);  
          table[idx].doSomething();  
      }  
  
      static class Entry&lt;T&gt;{  
          T value;  
  
          Entry(T x){  
              this.value = x;  
              System.out.println("Entry::Base");  
          }
  
          void doSomething(){  
              System.out.println("In Entry base, doing something");  
          }  
      }  
  
  }  
  
  
  
  
  public class Derived&lt;T&gt; extends Base&lt;T&gt; {  
  
      static class Entry&lt;T&gt; extends Base.Entry&lt;T&gt;{  
  
          Entry(T x) {  
              super(x);  
              System.out.println("In Entry derived");  
          }  
  
          int val;  
  
          @Override  
          void doSomething() {  
              System.out.println("In Entry derived doing something really smart!");  
          }       
      }  
  
      /**
       * @param args
       */
      public static void main(String[] args) {  
  
          Base&lt;String&gt; b = new Derived&lt;String&gt;();  
          b.add("Test string");  
  
      }  
  
  }  
  </code></pre>
  
  <p>But it prints:  </p>
  
  <pre><code>In base  
  Entry::Base     
  In Entry base, doing something    
  </code></pre>
  
  <p>So the derived <code>Entry</code> is never called.<br>
  Is my example different somehow? I can't understand how this works for <code>LinkedHashMap</code></p>