src/share/classes/java/util/LinkedHashMap.java
Print this page
rev 8940 : 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
Reviewed-by: duke
@@ -26,11 +26,10 @@
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
@@ -61,18 +60,18 @@
*
* <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.
+ * building LRU caches. Invoking the {@code put}, {@code get},
+ * {@code getOrDefault} or {@code replace} methods results in an access to the
+ * corresponding entry (assuming it exists after the invocation completes). 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,12 +440,23 @@
afterNodeAccess(e);
return e.value;
}
/**
- * Removes all of the mappings from this map.
- * The map will be empty after this call returns.
+ * {@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;
}