< prev index next >

src/java.base/share/classes/java/util/LinkedHashMap.java

Print this page




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.util.function.Consumer;
  29 import java.util.function.BiConsumer;
  30 import java.util.function.BiFunction;
  31 import java.io.IOException;
  32 
  33 /**
  34  * <p>Hash table and linked list implementation of the <tt>Map</tt> interface,
  35  * with predictable iteration order.  This implementation differs from
  36  * <tt>HashMap</tt> in that it maintains a doubly-linked list running through
  37  * all of its entries.  This linked list defines the iteration ordering,
  38  * which is normally the order in which keys were inserted into the map
  39  * (<i>insertion-order</i>).  Note that insertion order is not affected
  40  * if a key is <i>re-inserted</i> into the map.  (A key <tt>k</tt> is
  41  * reinserted into a map <tt>m</tt> if <tt>m.put(k, v)</tt> is invoked when
  42  * <tt>m.containsKey(k)</tt> would return <tt>true</tt> immediately prior to
  43  * the invocation.)
  44  *
  45  * <p>This implementation spares its clients from the unspecified, generally
  46  * chaotic ordering provided by {@link HashMap} (and {@link Hashtable}),
  47  * without incurring the increased cost associated with {@link TreeMap}.  It
  48  * can be used to produce a copy of a map that has the same order as the
  49  * original, regardless of the original map's implementation:
  50  * <pre>
  51  *     void foo(Map m) {
  52  *         Map copy = new LinkedHashMap(m);
  53  *         ...
  54  *     }
  55  * </pre>
  56  * This technique is particularly useful if a module takes a map on input,
  57  * copies it, and later returns results whose order is determined by that of
  58  * the copy.  (Clients generally appreciate having things returned in the same
  59  * order they were presented.)
  60  *
  61  * <p>A special {@link #LinkedHashMap(int,float,boolean) constructor} is
  62  * provided to create a linked hash map whose order of iteration is the order
  63  * in which its entries were last accessed, from least-recently accessed to
  64  * most-recently (<i>access-order</i>).  This kind of map is well-suited to
  65  * building LRU caches.  Invoking the {@code put}, {@code putIfAbsent},
  66  * {@code get}, {@code getOrDefault}, {@code compute}, {@code computeIfAbsent},
  67  * {@code computeIfPresent}, or {@code merge} methods results
  68  * in an access to the corresponding entry (assuming it exists after the
  69  * invocation completes). The {@code replace} methods only result in an access
  70  * of the entry if the value is replaced.  The {@code putAll} method generates one
  71  * entry access for each mapping in the specified map, in the order that
  72  * key-value mappings are provided by the specified map's entry set iterator.
  73  * <i>No other methods generate entry accesses.</i>  In particular, operations
  74  * on collection-views do <i>not</i> affect the order of iteration of the
  75  * backing map.
  76  *
  77  * <p>The {@link #removeEldestEntry(Map.Entry)} method may be overridden to
  78  * impose a policy for removing stale mappings automatically when new mappings
  79  * are added to the map.
  80  *
  81  * <p>This class provides all of the optional <tt>Map</tt> operations, and
  82  * permits null elements.  Like <tt>HashMap</tt>, it provides constant-time
  83  * performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
  84  * <tt>remove</tt>), assuming the hash function disperses elements
  85  * properly among the buckets.  Performance is likely to be just slightly
  86  * below that of <tt>HashMap</tt>, due to the added expense of maintaining the
  87  * linked list, with one exception: Iteration over the collection-views
  88  * of a <tt>LinkedHashMap</tt> requires time proportional to the <i>size</i>
  89  * of the map, regardless of its capacity.  Iteration over a <tt>HashMap</tt>
  90  * is likely to be more expensive, requiring time proportional to its
  91  * <i>capacity</i>.
  92  *
  93  * <p>A linked hash map has two parameters that affect its performance:
  94  * <i>initial capacity</i> and <i>load factor</i>.  They are defined precisely
  95  * as for <tt>HashMap</tt>.  Note, however, that the penalty for choosing an
  96  * excessively high value for initial capacity is less severe for this class
  97  * than for <tt>HashMap</tt>, as iteration times for this class are unaffected
  98  * by capacity.
  99  *
 100  * <p><strong>Note that this implementation is not synchronized.</strong>
 101  * If multiple threads access a linked hash map concurrently, and at least
 102  * one of the threads modifies the map structurally, it <em>must</em> be
 103  * synchronized externally.  This is typically accomplished by
 104  * synchronizing on some object that naturally encapsulates the map.
 105  *
 106  * If no such object exists, the map should be "wrapped" using the
 107  * {@link Collections#synchronizedMap Collections.synchronizedMap}
 108  * method.  This is best done at creation time, to prevent accidental
 109  * unsynchronized access to the map:<pre>
 110  *   Map m = Collections.synchronizedMap(new LinkedHashMap(...));</pre>
 111  *
 112  * A structural modification is any operation that adds or deletes one or more
 113  * mappings or, in the case of access-ordered linked hash maps, affects
 114  * iteration order.  In insertion-ordered linked hash maps, merely changing
 115  * the value associated with a key that is already contained in the map is not
 116  * a structural modification.  <strong>In access-ordered linked hash maps,
 117  * merely querying the map with <tt>get</tt> is a structural modification.
 118  * </strong>)
 119  *
 120  * <p>The iterators returned by the <tt>iterator</tt> method of the collections
 121  * returned by all of this class's collection view methods are
 122  * <em>fail-fast</em>: if the map is structurally modified at any time after
 123  * the iterator is created, in any way except through the iterator's own
 124  * <tt>remove</tt> method, the iterator will throw a {@link
 125  * ConcurrentModificationException}.  Thus, in the face of concurrent
 126  * modification, the iterator fails quickly and cleanly, rather than risking
 127  * arbitrary, non-deterministic behavior at an undetermined time in the future.
 128  *
 129  * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
 130  * as it is, generally speaking, impossible to make any hard guarantees in the
 131  * presence of unsynchronized concurrent modification.  Fail-fast iterators
 132  * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
 133  * Therefore, it would be wrong to write a program that depended on this
 134  * exception for its correctness:   <i>the fail-fast behavior of iterators
 135  * should be used only to detect bugs.</i>
 136  *
 137  * <p>The spliterators returned by the spliterator method of the collections
 138  * returned by all of this class's collection view methods are
 139  * <em><a href="Spliterator.html#binding">late-binding</a></em>,
 140  * <em>fail-fast</em>, and additionally report {@link Spliterator#ORDERED}.
 141  *
 142  * <p>This class is a member of the
 143  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
 144  * Java Collections Framework</a>.
 145  *
 146  * @implNote
 147  * The spliterators returned by the spliterator method of the collections
 148  * returned by all of this class's collection view methods are created from
 149  * the iterators of the corresponding collections.
 150  *
 151  * @param <K> the type of keys maintained by this map
 152  * @param <V> the type of mapped values


 192     static class Entry<K,V> extends HashMap.Node<K,V> {
 193         Entry<K,V> before, after;
 194         Entry(int hash, K key, V value, Node<K,V> next) {
 195             super(hash, key, value, next);
 196         }
 197     }
 198 
 199     private static final long serialVersionUID = 3801124242820219131L;
 200 
 201     /**
 202      * The head (eldest) of the doubly linked list.
 203      */
 204     transient LinkedHashMap.Entry<K,V> head;
 205 
 206     /**
 207      * The tail (youngest) of the doubly linked list.
 208      */
 209     transient LinkedHashMap.Entry<K,V> tail;
 210 
 211     /**
 212      * The iteration ordering method for this linked hash map: <tt>true</tt>
 213      * for access-order, <tt>false</tt> for insertion-order.
 214      *
 215      * @serial
 216      */
 217     final boolean accessOrder;
 218 
 219     // internal utilities
 220 
 221     // link at the end of list
 222     private void linkNodeLast(LinkedHashMap.Entry<K,V> p) {
 223         LinkedHashMap.Entry<K,V> last = tail;
 224         tail = p;
 225         if (last == null)
 226             head = p;
 227         else {
 228             p.before = last;
 229             last.after = p;
 230         }
 231     }
 232 
 233     // apply src's links to dst


 318                 last = b;
 319             if (last == null)
 320                 head = p;
 321             else {
 322                 p.before = last;
 323                 last.after = p;
 324             }
 325             tail = p;
 326             ++modCount;
 327         }
 328     }
 329 
 330     void internalWriteEntries(java.io.ObjectOutputStream s) throws IOException {
 331         for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
 332             s.writeObject(e.key);
 333             s.writeObject(e.value);
 334         }
 335     }
 336 
 337     /**
 338      * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
 339      * with the specified initial capacity and load factor.
 340      *
 341      * @param  initialCapacity the initial capacity
 342      * @param  loadFactor      the load factor
 343      * @throws IllegalArgumentException if the initial capacity is negative
 344      *         or the load factor is nonpositive
 345      */
 346     public LinkedHashMap(int initialCapacity, float loadFactor) {
 347         super(initialCapacity, loadFactor);
 348         accessOrder = false;
 349     }
 350 
 351     /**
 352      * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
 353      * with the specified initial capacity and a default load factor (0.75).
 354      *
 355      * @param  initialCapacity the initial capacity
 356      * @throws IllegalArgumentException if the initial capacity is negative
 357      */
 358     public LinkedHashMap(int initialCapacity) {
 359         super(initialCapacity);
 360         accessOrder = false;
 361     }
 362 
 363     /**
 364      * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
 365      * with the default initial capacity (16) and load factor (0.75).
 366      */
 367     public LinkedHashMap() {
 368         super();
 369         accessOrder = false;
 370     }
 371 
 372     /**
 373      * Constructs an insertion-ordered <tt>LinkedHashMap</tt> instance with
 374      * the same mappings as the specified map.  The <tt>LinkedHashMap</tt>
 375      * instance is created with a default load factor (0.75) and an initial
 376      * capacity sufficient to hold the mappings in the specified map.
 377      *
 378      * @param  m the map whose mappings are to be placed in this map
 379      * @throws NullPointerException if the specified map is null
 380      */
 381     public LinkedHashMap(Map<? extends K, ? extends V> m) {
 382         super();
 383         accessOrder = false;
 384         putMapEntries(m, false);
 385     }
 386 
 387     /**
 388      * Constructs an empty <tt>LinkedHashMap</tt> instance with the
 389      * specified initial capacity, load factor and ordering mode.
 390      *
 391      * @param  initialCapacity the initial capacity
 392      * @param  loadFactor      the load factor
 393      * @param  accessOrder     the ordering mode - <tt>true</tt> for
 394      *         access-order, <tt>false</tt> for insertion-order
 395      * @throws IllegalArgumentException if the initial capacity is negative
 396      *         or the load factor is nonpositive
 397      */
 398     public LinkedHashMap(int initialCapacity,
 399                          float loadFactor,
 400                          boolean accessOrder) {
 401         super(initialCapacity, loadFactor);
 402         this.accessOrder = accessOrder;
 403     }
 404 
 405 
 406     /**
 407      * Returns <tt>true</tt> if this map maps one or more keys to the
 408      * specified value.
 409      *
 410      * @param value value whose presence in this map is to be tested
 411      * @return <tt>true</tt> if this map maps one or more keys to the
 412      *         specified value
 413      */
 414     public boolean containsValue(Object value) {
 415         for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
 416             V v = e.value;
 417             if (v == value || (value != null && value.equals(v)))
 418                 return true;
 419         }
 420         return false;
 421     }
 422 
 423     /**
 424      * Returns the value to which the specified key is mapped,
 425      * or {@code null} if this map contains no mapping for the key.
 426      *
 427      * <p>More formally, if this map contains a mapping from a key
 428      * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
 429      * key.equals(k))}, then this method returns {@code v}; otherwise
 430      * it returns {@code null}.  (There can be at most one such mapping.)
 431      *


 448      * {@inheritDoc}
 449      */
 450     public V getOrDefault(Object key, V defaultValue) {
 451        Node<K,V> e;
 452        if ((e = getNode(hash(key), key)) == null)
 453            return defaultValue;
 454        if (accessOrder)
 455            afterNodeAccess(e);
 456        return e.value;
 457    }
 458 
 459     /**
 460      * {@inheritDoc}
 461      */
 462     public void clear() {
 463         super.clear();
 464         head = tail = null;
 465     }
 466 
 467     /**
 468      * Returns <tt>true</tt> if this map should remove its eldest entry.
 469      * This method is invoked by <tt>put</tt> and <tt>putAll</tt> after
 470      * inserting a new entry into the map.  It provides the implementor
 471      * with the opportunity to remove the eldest entry each time a new one
 472      * is added.  This is useful if the map represents a cache: it allows
 473      * the map to reduce memory consumption by deleting stale entries.
 474      *
 475      * <p>Sample use: this override will allow the map to grow up to 100
 476      * entries and then delete the eldest entry each time a new entry is
 477      * added, maintaining a steady state of 100 entries.
 478      * <pre>
 479      *     private static final int MAX_ENTRIES = 100;
 480      *
 481      *     protected boolean removeEldestEntry(Map.Entry eldest) {
 482      *        return size() &gt; MAX_ENTRIES;
 483      *     }
 484      * </pre>
 485      *
 486      * <p>This method typically does not modify the map in any way,
 487      * instead allowing the map to modify itself as directed by its
 488      * return value.  It <i>is</i> permitted for this method to modify
 489      * the map directly, but if it does so, it <i>must</i> return
 490      * <tt>false</tt> (indicating that the map should not attempt any
 491      * further modification).  The effects of returning <tt>true</tt>
 492      * after modifying the map from within this method are unspecified.
 493      *
 494      * <p>This implementation merely returns <tt>false</tt> (so that this
 495      * map acts like a normal map - the eldest element is never removed).
 496      *
 497      * @param    eldest The least recently inserted entry in the map, or if
 498      *           this is an access-ordered map, the least recently accessed
 499      *           entry.  This is the entry that will be removed it this
 500      *           method returns <tt>true</tt>.  If the map was empty prior
 501      *           to the <tt>put</tt> or <tt>putAll</tt> invocation resulting
 502      *           in this invocation, this will be the entry that was just
 503      *           inserted; in other words, if the map contains a single
 504      *           entry, the eldest entry is also the newest.
 505      * @return   <tt>true</tt> if the eldest entry should be removed
 506      *           from the map; <tt>false</tt> if it should be retained.
 507      */
 508     protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
 509         return false;
 510     }
 511 
 512     /**
 513      * Returns a {@link Set} view of the keys contained in this map.
 514      * The set is backed by the map, so changes to the map are
 515      * reflected in the set, and vice-versa.  If the map is modified
 516      * while an iteration over the set is in progress (except through
 517      * the iterator's own <tt>remove</tt> operation), the results of
 518      * the iteration are undefined.  The set supports element removal,
 519      * which removes the corresponding mapping from the map, via the
 520      * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
 521      * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
 522      * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
 523      * operations.
 524      * Its {@link Spliterator} typically provides faster sequential
 525      * performance but much poorer parallel performance than that of
 526      * {@code HashMap}.
 527      *
 528      * @return a set view of the keys contained in this map
 529      */
 530     public Set<K> keySet() {
 531         Set<K> ks;
 532         return (ks = keySet) == null ? (keySet = new LinkedKeySet()) : ks;
 533     }
 534 
 535     final class LinkedKeySet extends AbstractSet<K> {
 536         public final int size()                 { return size; }
 537         public final void clear()               { LinkedHashMap.this.clear(); }
 538         public final Iterator<K> iterator() {
 539             return new LinkedKeyIterator();
 540         }
 541         public final boolean contains(Object o) { return containsKey(o); }
 542         public final boolean remove(Object key) {


 546             return Spliterators.spliterator(this, Spliterator.SIZED |
 547                                             Spliterator.ORDERED |
 548                                             Spliterator.DISTINCT);
 549         }
 550         public final void forEach(Consumer<? super K> action) {
 551             if (action == null)
 552                 throw new NullPointerException();
 553             int mc = modCount;
 554             for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
 555                 action.accept(e.key);
 556             if (modCount != mc)
 557                 throw new ConcurrentModificationException();
 558         }
 559     }
 560 
 561     /**
 562      * Returns a {@link Collection} view of the values contained in this map.
 563      * The collection is backed by the map, so changes to the map are
 564      * reflected in the collection, and vice-versa.  If the map is
 565      * modified while an iteration over the collection is in progress
 566      * (except through the iterator's own <tt>remove</tt> operation),
 567      * the results of the iteration are undefined.  The collection
 568      * supports element removal, which removes the corresponding
 569      * mapping from the map, via the <tt>Iterator.remove</tt>,
 570      * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
 571      * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
 572      * support the <tt>add</tt> or <tt>addAll</tt> operations.
 573      * Its {@link Spliterator} typically provides faster sequential
 574      * performance but much poorer parallel performance than that of
 575      * {@code HashMap}.
 576      *
 577      * @return a view of the values contained in this map
 578      */
 579     public Collection<V> values() {
 580         Collection<V> vs;
 581         return (vs = values) == null ? (values = new LinkedValues()) : vs;
 582     }
 583 
 584     final class LinkedValues extends AbstractCollection<V> {
 585         public final int size()                 { return size; }
 586         public final void clear()               { LinkedHashMap.this.clear(); }
 587         public final Iterator<V> iterator() {
 588             return new LinkedValueIterator();
 589         }
 590         public final boolean contains(Object o) { return containsValue(o); }
 591         public final Spliterator<V> spliterator() {
 592             return Spliterators.spliterator(this, Spliterator.SIZED |
 593                                             Spliterator.ORDERED);
 594         }
 595         public final void forEach(Consumer<? super V> action) {
 596             if (action == null)
 597                 throw new NullPointerException();
 598             int mc = modCount;
 599             for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
 600                 action.accept(e.value);
 601             if (modCount != mc)
 602                 throw new ConcurrentModificationException();
 603         }
 604     }
 605 
 606     /**
 607      * Returns a {@link Set} view of the mappings contained in this map.
 608      * The set is backed by the map, so changes to the map are
 609      * reflected in the set, and vice-versa.  If the map is modified
 610      * while an iteration over the set is in progress (except through
 611      * the iterator's own <tt>remove</tt> operation, or through the
 612      * <tt>setValue</tt> operation on a map entry returned by the
 613      * iterator) the results of the iteration are undefined.  The set
 614      * supports element removal, which removes the corresponding
 615      * mapping from the map, via the <tt>Iterator.remove</tt>,
 616      * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
 617      * <tt>clear</tt> operations.  It does not support the
 618      * <tt>add</tt> or <tt>addAll</tt> operations.
 619      * Its {@link Spliterator} typically provides faster sequential
 620      * performance but much poorer parallel performance than that of
 621      * {@code HashMap}.
 622      *
 623      * @return a set view of the mappings contained in this map
 624      */
 625     public Set<Map.Entry<K,V>> entrySet() {
 626         Set<Map.Entry<K,V>> es;
 627         return (es = entrySet) == null ? (entrySet = new LinkedEntrySet()) : es;
 628     }
 629 
 630     final class LinkedEntrySet extends AbstractSet<Map.Entry<K,V>> {
 631         public final int size()                 { return size; }
 632         public final void clear()               { LinkedHashMap.this.clear(); }
 633         public final Iterator<Map.Entry<K,V>> iterator() {
 634             return new LinkedEntryIterator();
 635         }
 636         public final boolean contains(Object o) {
 637             if (!(o instanceof Map.Entry))
 638                 return false;




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.util.function.Consumer;
  29 import java.util.function.BiConsumer;
  30 import java.util.function.BiFunction;
  31 import java.io.IOException;
  32 
  33 /**
  34  * <p>Hash table and linked list implementation of the {@code Map} interface,
  35  * with predictable iteration order.  This implementation differs from
  36  * {@code HashMap} in that it maintains a doubly-linked list running through
  37  * all of its entries.  This linked list defines the iteration ordering,
  38  * which is normally the order in which keys were inserted into the map
  39  * (<i>insertion-order</i>).  Note that insertion order is not affected
  40  * if a key is <i>re-inserted</i> into the map.  (A key {@code k} is
  41  * reinserted into a map {@code m} if {@code m.put(k, v)} is invoked when
  42  * {@code m.containsKey(k)} would return {@code true} immediately prior to
  43  * the invocation.)
  44  *
  45  * <p>This implementation spares its clients from the unspecified, generally
  46  * chaotic ordering provided by {@link HashMap} (and {@link Hashtable}),
  47  * without incurring the increased cost associated with {@link TreeMap}.  It
  48  * can be used to produce a copy of a map that has the same order as the
  49  * original, regardless of the original map's implementation:
  50  * <pre>
  51  *     void foo(Map m) {
  52  *         Map copy = new LinkedHashMap(m);
  53  *         ...
  54  *     }
  55  * </pre>
  56  * This technique is particularly useful if a module takes a map on input,
  57  * copies it, and later returns results whose order is determined by that of
  58  * the copy.  (Clients generally appreciate having things returned in the same
  59  * order they were presented.)
  60  *
  61  * <p>A special {@link #LinkedHashMap(int,float,boolean) constructor} is
  62  * provided to create a linked hash map whose order of iteration is the order
  63  * in which its entries were last accessed, from least-recently accessed to
  64  * most-recently (<i>access-order</i>).  This kind of map is well-suited to
  65  * building LRU caches.  Invoking the {@code put}, {@code putIfAbsent},
  66  * {@code get}, {@code getOrDefault}, {@code compute}, {@code computeIfAbsent},
  67  * {@code computeIfPresent}, or {@code merge} methods results
  68  * in an access to the corresponding entry (assuming it exists after the
  69  * invocation completes). The {@code replace} methods only result in an access
  70  * of the entry if the value is replaced.  The {@code putAll} method generates one
  71  * entry access for each mapping in the specified map, in the order that
  72  * key-value mappings are provided by the specified map's entry set iterator.
  73  * <i>No other methods generate entry accesses.</i>  In particular, operations
  74  * on collection-views do <i>not</i> affect the order of iteration of the
  75  * backing map.
  76  *
  77  * <p>The {@link #removeEldestEntry(Map.Entry)} method may be overridden to
  78  * impose a policy for removing stale mappings automatically when new mappings
  79  * are added to the map.
  80  *
  81  * <p>This class provides all of the optional {@code Map} operations, and
  82  * permits null elements.  Like {@code HashMap}, it provides constant-time
  83  * performance for the basic operations ({@code add}, {@code contains} and
  84  * {@code remove}), assuming the hash function disperses elements
  85  * properly among the buckets.  Performance is likely to be just slightly
  86  * below that of {@code HashMap}, due to the added expense of maintaining the
  87  * linked list, with one exception: Iteration over the collection-views
  88  * of a {@code LinkedHashMap} requires time proportional to the <i>size</i>
  89  * of the map, regardless of its capacity.  Iteration over a {@code HashMap}
  90  * is likely to be more expensive, requiring time proportional to its
  91  * <i>capacity</i>.
  92  *
  93  * <p>A linked hash map has two parameters that affect its performance:
  94  * <i>initial capacity</i> and <i>load factor</i>.  They are defined precisely
  95  * as for {@code HashMap}.  Note, however, that the penalty for choosing an
  96  * excessively high value for initial capacity is less severe for this class
  97  * than for {@code HashMap}, as iteration times for this class are unaffected
  98  * by capacity.
  99  *
 100  * <p><strong>Note that this implementation is not synchronized.</strong>
 101  * If multiple threads access a linked hash map concurrently, and at least
 102  * one of the threads modifies the map structurally, it <em>must</em> be
 103  * synchronized externally.  This is typically accomplished by
 104  * synchronizing on some object that naturally encapsulates the map.
 105  *
 106  * If no such object exists, the map should be "wrapped" using the
 107  * {@link Collections#synchronizedMap Collections.synchronizedMap}
 108  * method.  This is best done at creation time, to prevent accidental
 109  * unsynchronized access to the map:<pre>
 110  *   Map m = Collections.synchronizedMap(new LinkedHashMap(...));</pre>
 111  *
 112  * A structural modification is any operation that adds or deletes one or more
 113  * mappings or, in the case of access-ordered linked hash maps, affects
 114  * iteration order.  In insertion-ordered linked hash maps, merely changing
 115  * the value associated with a key that is already contained in the map is not
 116  * a structural modification.  <strong>In access-ordered linked hash maps,
 117  * merely querying the map with {@code get} is a structural modification.
 118  * </strong>)
 119  *
 120  * <p>The iterators returned by the {@code iterator} method of the collections
 121  * returned by all of this class's collection view methods are
 122  * <em>fail-fast</em>: if the map is structurally modified at any time after
 123  * the iterator is created, in any way except through the iterator's own
 124  * {@code remove} method, the iterator will throw a {@link
 125  * ConcurrentModificationException}.  Thus, in the face of concurrent
 126  * modification, the iterator fails quickly and cleanly, rather than risking
 127  * arbitrary, non-deterministic behavior at an undetermined time in the future.
 128  *
 129  * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
 130  * as it is, generally speaking, impossible to make any hard guarantees in the
 131  * presence of unsynchronized concurrent modification.  Fail-fast iterators
 132  * throw {@code ConcurrentModificationException} on a best-effort basis.
 133  * Therefore, it would be wrong to write a program that depended on this
 134  * exception for its correctness:   <i>the fail-fast behavior of iterators
 135  * should be used only to detect bugs.</i>
 136  *
 137  * <p>The spliterators returned by the spliterator method of the collections
 138  * returned by all of this class's collection view methods are
 139  * <em><a href="Spliterator.html#binding">late-binding</a></em>,
 140  * <em>fail-fast</em>, and additionally report {@link Spliterator#ORDERED}.
 141  *
 142  * <p>This class is a member of the
 143  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
 144  * Java Collections Framework</a>.
 145  *
 146  * @implNote
 147  * The spliterators returned by the spliterator method of the collections
 148  * returned by all of this class's collection view methods are created from
 149  * the iterators of the corresponding collections.
 150  *
 151  * @param <K> the type of keys maintained by this map
 152  * @param <V> the type of mapped values


 192     static class Entry<K,V> extends HashMap.Node<K,V> {
 193         Entry<K,V> before, after;
 194         Entry(int hash, K key, V value, Node<K,V> next) {
 195             super(hash, key, value, next);
 196         }
 197     }
 198 
 199     private static final long serialVersionUID = 3801124242820219131L;
 200 
 201     /**
 202      * The head (eldest) of the doubly linked list.
 203      */
 204     transient LinkedHashMap.Entry<K,V> head;
 205 
 206     /**
 207      * The tail (youngest) of the doubly linked list.
 208      */
 209     transient LinkedHashMap.Entry<K,V> tail;
 210 
 211     /**
 212      * The iteration ordering method for this linked hash map: {@code true}
 213      * for access-order, {@code false} for insertion-order.
 214      *
 215      * @serial
 216      */
 217     final boolean accessOrder;
 218 
 219     // internal utilities
 220 
 221     // link at the end of list
 222     private void linkNodeLast(LinkedHashMap.Entry<K,V> p) {
 223         LinkedHashMap.Entry<K,V> last = tail;
 224         tail = p;
 225         if (last == null)
 226             head = p;
 227         else {
 228             p.before = last;
 229             last.after = p;
 230         }
 231     }
 232 
 233     // apply src's links to dst


 318                 last = b;
 319             if (last == null)
 320                 head = p;
 321             else {
 322                 p.before = last;
 323                 last.after = p;
 324             }
 325             tail = p;
 326             ++modCount;
 327         }
 328     }
 329 
 330     void internalWriteEntries(java.io.ObjectOutputStream s) throws IOException {
 331         for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
 332             s.writeObject(e.key);
 333             s.writeObject(e.value);
 334         }
 335     }
 336 
 337     /**
 338      * Constructs an empty insertion-ordered {@code LinkedHashMap} instance
 339      * with the specified initial capacity and load factor.
 340      *
 341      * @param  initialCapacity the initial capacity
 342      * @param  loadFactor      the load factor
 343      * @throws IllegalArgumentException if the initial capacity is negative
 344      *         or the load factor is nonpositive
 345      */
 346     public LinkedHashMap(int initialCapacity, float loadFactor) {
 347         super(initialCapacity, loadFactor);
 348         accessOrder = false;
 349     }
 350 
 351     /**
 352      * Constructs an empty insertion-ordered {@code LinkedHashMap} instance
 353      * with the specified initial capacity and a default load factor (0.75).
 354      *
 355      * @param  initialCapacity the initial capacity
 356      * @throws IllegalArgumentException if the initial capacity is negative
 357      */
 358     public LinkedHashMap(int initialCapacity) {
 359         super(initialCapacity);
 360         accessOrder = false;
 361     }
 362 
 363     /**
 364      * Constructs an empty insertion-ordered {@code LinkedHashMap} instance
 365      * with the default initial capacity (16) and load factor (0.75).
 366      */
 367     public LinkedHashMap() {
 368         super();
 369         accessOrder = false;
 370     }
 371 
 372     /**
 373      * Constructs an insertion-ordered {@code LinkedHashMap} instance with
 374      * the same mappings as the specified map.  The {@code LinkedHashMap}
 375      * instance is created with a default load factor (0.75) and an initial
 376      * capacity sufficient to hold the mappings in the specified map.
 377      *
 378      * @param  m the map whose mappings are to be placed in this map
 379      * @throws NullPointerException if the specified map is null
 380      */
 381     public LinkedHashMap(Map<? extends K, ? extends V> m) {
 382         super();
 383         accessOrder = false;
 384         putMapEntries(m, false);
 385     }
 386 
 387     /**
 388      * Constructs an empty {@code LinkedHashMap} instance with the
 389      * specified initial capacity, load factor and ordering mode.
 390      *
 391      * @param  initialCapacity the initial capacity
 392      * @param  loadFactor      the load factor
 393      * @param  accessOrder     the ordering mode - {@code true} for
 394      *         access-order, {@code false} for insertion-order
 395      * @throws IllegalArgumentException if the initial capacity is negative
 396      *         or the load factor is nonpositive
 397      */
 398     public LinkedHashMap(int initialCapacity,
 399                          float loadFactor,
 400                          boolean accessOrder) {
 401         super(initialCapacity, loadFactor);
 402         this.accessOrder = accessOrder;
 403     }
 404 
 405 
 406     /**
 407      * Returns {@code true} if this map maps one or more keys to the
 408      * specified value.
 409      *
 410      * @param value value whose presence in this map is to be tested
 411      * @return {@code true} if this map maps one or more keys to the
 412      *         specified value
 413      */
 414     public boolean containsValue(Object value) {
 415         for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
 416             V v = e.value;
 417             if (v == value || (value != null && value.equals(v)))
 418                 return true;
 419         }
 420         return false;
 421     }
 422 
 423     /**
 424      * Returns the value to which the specified key is mapped,
 425      * or {@code null} if this map contains no mapping for the key.
 426      *
 427      * <p>More formally, if this map contains a mapping from a key
 428      * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
 429      * key.equals(k))}, then this method returns {@code v}; otherwise
 430      * it returns {@code null}.  (There can be at most one such mapping.)
 431      *


 448      * {@inheritDoc}
 449      */
 450     public V getOrDefault(Object key, V defaultValue) {
 451        Node<K,V> e;
 452        if ((e = getNode(hash(key), key)) == null)
 453            return defaultValue;
 454        if (accessOrder)
 455            afterNodeAccess(e);
 456        return e.value;
 457    }
 458 
 459     /**
 460      * {@inheritDoc}
 461      */
 462     public void clear() {
 463         super.clear();
 464         head = tail = null;
 465     }
 466 
 467     /**
 468      * Returns {@code true} if this map should remove its eldest entry.
 469      * This method is invoked by {@code put} and {@code putAll} after
 470      * inserting a new entry into the map.  It provides the implementor
 471      * with the opportunity to remove the eldest entry each time a new one
 472      * is added.  This is useful if the map represents a cache: it allows
 473      * the map to reduce memory consumption by deleting stale entries.
 474      *
 475      * <p>Sample use: this override will allow the map to grow up to 100
 476      * entries and then delete the eldest entry each time a new entry is
 477      * added, maintaining a steady state of 100 entries.
 478      * <pre>
 479      *     private static final int MAX_ENTRIES = 100;
 480      *
 481      *     protected boolean removeEldestEntry(Map.Entry eldest) {
 482      *        return size() &gt; MAX_ENTRIES;
 483      *     }
 484      * </pre>
 485      *
 486      * <p>This method typically does not modify the map in any way,
 487      * instead allowing the map to modify itself as directed by its
 488      * return value.  It <i>is</i> permitted for this method to modify
 489      * the map directly, but if it does so, it <i>must</i> return
 490      * {@code false} (indicating that the map should not attempt any
 491      * further modification).  The effects of returning {@code true}
 492      * after modifying the map from within this method are unspecified.
 493      *
 494      * <p>This implementation merely returns {@code false} (so that this
 495      * map acts like a normal map - the eldest element is never removed).
 496      *
 497      * @param    eldest The least recently inserted entry in the map, or if
 498      *           this is an access-ordered map, the least recently accessed
 499      *           entry.  This is the entry that will be removed it this
 500      *           method returns {@code true}.  If the map was empty prior
 501      *           to the {@code put} or {@code putAll} invocation resulting
 502      *           in this invocation, this will be the entry that was just
 503      *           inserted; in other words, if the map contains a single
 504      *           entry, the eldest entry is also the newest.
 505      * @return   {@code true} if the eldest entry should be removed
 506      *           from the map; {@code false} if it should be retained.
 507      */
 508     protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
 509         return false;
 510     }
 511 
 512     /**
 513      * Returns a {@link Set} view of the keys contained in this map.
 514      * The set is backed by the map, so changes to the map are
 515      * reflected in the set, and vice-versa.  If the map is modified
 516      * while an iteration over the set is in progress (except through
 517      * the iterator's own {@code remove} operation), the results of
 518      * the iteration are undefined.  The set supports element removal,
 519      * which removes the corresponding mapping from the map, via the
 520      * {@code Iterator.remove}, {@code Set.remove},
 521      * {@code removeAll}, {@code retainAll}, and {@code clear}
 522      * operations.  It does not support the {@code add} or {@code addAll}
 523      * operations.
 524      * Its {@link Spliterator} typically provides faster sequential
 525      * performance but much poorer parallel performance than that of
 526      * {@code HashMap}.
 527      *
 528      * @return a set view of the keys contained in this map
 529      */
 530     public Set<K> keySet() {
 531         Set<K> ks;
 532         return (ks = keySet) == null ? (keySet = new LinkedKeySet()) : ks;
 533     }
 534 
 535     final class LinkedKeySet extends AbstractSet<K> {
 536         public final int size()                 { return size; }
 537         public final void clear()               { LinkedHashMap.this.clear(); }
 538         public final Iterator<K> iterator() {
 539             return new LinkedKeyIterator();
 540         }
 541         public final boolean contains(Object o) { return containsKey(o); }
 542         public final boolean remove(Object key) {


 546             return Spliterators.spliterator(this, Spliterator.SIZED |
 547                                             Spliterator.ORDERED |
 548                                             Spliterator.DISTINCT);
 549         }
 550         public final void forEach(Consumer<? super K> action) {
 551             if (action == null)
 552                 throw new NullPointerException();
 553             int mc = modCount;
 554             for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
 555                 action.accept(e.key);
 556             if (modCount != mc)
 557                 throw new ConcurrentModificationException();
 558         }
 559     }
 560 
 561     /**
 562      * Returns a {@link Collection} view of the values contained in this map.
 563      * The collection is backed by the map, so changes to the map are
 564      * reflected in the collection, and vice-versa.  If the map is
 565      * modified while an iteration over the collection is in progress
 566      * (except through the iterator's own {@code remove} operation),
 567      * the results of the iteration are undefined.  The collection
 568      * supports element removal, which removes the corresponding
 569      * mapping from the map, via the {@code Iterator.remove},
 570      * {@code Collection.remove}, {@code removeAll},
 571      * {@code retainAll} and {@code clear} operations.  It does not
 572      * support the {@code add} or {@code addAll} operations.
 573      * Its {@link Spliterator} typically provides faster sequential
 574      * performance but much poorer parallel performance than that of
 575      * {@code HashMap}.
 576      *
 577      * @return a view of the values contained in this map
 578      */
 579     public Collection<V> values() {
 580         Collection<V> vs;
 581         return (vs = values) == null ? (values = new LinkedValues()) : vs;
 582     }
 583 
 584     final class LinkedValues extends AbstractCollection<V> {
 585         public final int size()                 { return size; }
 586         public final void clear()               { LinkedHashMap.this.clear(); }
 587         public final Iterator<V> iterator() {
 588             return new LinkedValueIterator();
 589         }
 590         public final boolean contains(Object o) { return containsValue(o); }
 591         public final Spliterator<V> spliterator() {
 592             return Spliterators.spliterator(this, Spliterator.SIZED |
 593                                             Spliterator.ORDERED);
 594         }
 595         public final void forEach(Consumer<? super V> action) {
 596             if (action == null)
 597                 throw new NullPointerException();
 598             int mc = modCount;
 599             for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
 600                 action.accept(e.value);
 601             if (modCount != mc)
 602                 throw new ConcurrentModificationException();
 603         }
 604     }
 605 
 606     /**
 607      * Returns a {@link Set} view of the mappings contained in this map.
 608      * The set is backed by the map, so changes to the map are
 609      * reflected in the set, and vice-versa.  If the map is modified
 610      * while an iteration over the set is in progress (except through
 611      * the iterator's own {@code remove} operation, or through the
 612      * {@code setValue} operation on a map entry returned by the
 613      * iterator) the results of the iteration are undefined.  The set
 614      * supports element removal, which removes the corresponding
 615      * mapping from the map, via the {@code Iterator.remove},
 616      * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
 617      * {@code clear} operations.  It does not support the
 618      * {@code add} or {@code addAll} operations.
 619      * Its {@link Spliterator} typically provides faster sequential
 620      * performance but much poorer parallel performance than that of
 621      * {@code HashMap}.
 622      *
 623      * @return a set view of the mappings contained in this map
 624      */
 625     public Set<Map.Entry<K,V>> entrySet() {
 626         Set<Map.Entry<K,V>> es;
 627         return (es = entrySet) == null ? (entrySet = new LinkedEntrySet()) : es;
 628     }
 629 
 630     final class LinkedEntrySet extends AbstractSet<Map.Entry<K,V>> {
 631         public final int size()                 { return size; }
 632         public final void clear()               { LinkedHashMap.this.clear(); }
 633         public final Iterator<Map.Entry<K,V>> iterator() {
 634             return new LinkedEntryIterator();
 635         }
 636         public final boolean contains(Object o) {
 637             if (!(o instanceof Map.Entry))
 638                 return false;


< prev index next >