title: Use LinkedHashMap to implement LRU cache
body: <p>I was trying to implement a LRU cache using LinkedHashMap. 
  In the documentation of LinkedHashMap (<a href="http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html">http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html</a>), it says:</p>
  
  <p><strong>Note that insertion order is not affected if a key is re-inserted into the map.</strong> </p>
  
  <p>But when I do the following puts </p>
  
  <pre><code>public class LRUCache&lt;K, V&gt; extends LinkedHashMap&lt;K, V&gt; {
      private int size;
  
      public static void main(String[] args) {
          LRUCache&lt;Integer, Integer&gt; cache = LRUCache.newInstance(2);
          cache.put(1, 1);
          cache.put(2, 2);
          cache.put(1, 1);
          cache.put(3, 3);
  
          System.out.println(cache);
      }
  
      private LRUCache(int size) {
          super(size, 0.75f, true);
          this.size = size;
      }
  
      @Override
      protected boolean removeEldestEntry(Map.Entry&lt;K, V&gt; eldest) {
          return size() &gt; size;
      }
  
      public static &lt;K, V&gt; LRUCache&lt;K, V&gt; newInstance(int size) {
          return new LRUCache&lt;K, V&gt;(size);
      }
  
  }
  </code></pre>
  
  <p>The output is </p>
  
  <pre><code>{1=1, 3=3}
  </code></pre>
  
  <p>Which indicates that the re-inserted did affected the order.
  Does anybody know any explanation?</p>