src/share/classes/java/util/LinkedHashMap.java
Print this page
rev 8940 : 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
Reviewed-by: psandoz
*** 26,36 ****
package java.util;
import java.util.function.Consumer;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
- import java.io.Serializable;
import java.io.IOException;
/**
* <p>Hash table and linked list implementation of the <tt>Map</tt> interface,
* with predictable iteration order. This implementation differs from
--- 26,35 ----
*** 61,78 ****
*
* <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.
*
* <p>The {@link #removeEldestEntry(Map.Entry)} method may be overridden to
* impose a policy for removing stale mappings automatically when new mappings
* are added to the map.
*
--- 60,80 ----
*
* <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 {@code put}, {@code putIfAbsent},
! * {@code get}, {@code computeIfAbsent} or {@code getOrDefault} methods results
! * in an access to the corresponding entry (assuming it exists after the
! * invocation completes). Invoking the {@code replace}, {@code merge},
! * {@code compute} or {@code computeIfPresent} methods results in one or more
! * accesses of the corresponding entry. The {@code putAll} 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.
*
* <p>The {@link #removeEldestEntry(Map.Entry)} method may be overridden to
* impose a policy for removing stale mappings automatically when new mappings
* are added to the map.
*
*** 441,452 ****
afterNodeAccess(e);
return e.value;
}
/**
! * Removes all of the mappings from this map.
! * The map will be empty after this call returns.
*/
public void clear() {
super.clear();
head = tail = null;
}
--- 443,465 ----
afterNodeAccess(e);
return e.value;
}
/**
! * {@inheritDoc}
! */
! public V getOrDefault(Object key, V defaultValue) {
! Node<K,V> e;
! if ((e = getNode(hash(key), key)) == null)
! return defaultValue;
! if (accessOrder)
! afterNodeAccess(e);
! return e.value;
! }
!
! /**
! * {@inheritDoc}
*/
public void clear() {
super.clear();
head = tail = null;
}