Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8029795

LinkedHashMap fails to update access order correctly




        LinkedHashMap does not update LRU order for iteration of entries if last accessed order is declared in the constructor.


            public V getOrDefault(Object key, V defaultValue) {
                Node<K,V> e;
                if ((e = getNode(hash(key), key)) == null)
                    return defaultValue;
                if (accessOrder)
                return e.value;

        Other Map default methods for LinkedHashMap behave correctly (see HashMap and the method afterNodeAccess).

        It can be inferred from the implementation specification of default methods that entry accesses should be generated, since they are composed from put/get, but it is not obvious or guaranteed so the documentation should be updated to clarify the behaviour of methods other than put/get:

         * <p>A special {@link #LinkedHashMap(int,float,boolean) constructor} is
         * provided to create a linked hash map whose order of iteration is the order
         * in which its entries were last accessed, from least-recently accessed to
         * most-recently (<i>access-order</i>). This kind of map is well-suited to
         * building LRU caches. Invoking the <tt>put</tt> or <tt>get</tt> method
         * results in an access to the corresponding entry (assuming it exists after
         * the invocation completes). The <tt>putAll</tt> method generates one entry
         * access for each mapping in the specified map, in the order that key-value
         * mappings are provided by the specified map's entry set iterator. <i>No
         * other methods generate entry accesses.</i> In particular, operations on
         * collection-views do <i>not</i> affect the order of iteration of the backing
         * map.


            Issue Links



                • Assignee:
                  mduigou Mike Duigou
                  psandoz Paul Sandoz
                • Votes:
                  0 Vote for this issue
                  7 Start watching this issue


                  • Created: