jdk/src/share/classes/java/util/LinkedHashMap.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7803 56bc97d69d93
child 12448 b95438b17098
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7803
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * <p>Hash table and linked list implementation of the <tt>Map</tt> interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * with predictable iteration order.  This implementation differs from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <tt>HashMap</tt> in that it maintains a doubly-linked list running through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * all of its entries.  This linked list defines the iteration ordering,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * which is normally the order in which keys were inserted into the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * (<i>insertion-order</i>).  Note that insertion order is not affected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * if a key is <i>re-inserted</i> into the map.  (A key <tt>k</tt> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * reinserted into a map <tt>m</tt> if <tt>m.put(k, v)</tt> is invoked when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <tt>m.containsKey(k)</tt> would return <tt>true</tt> immediately prior to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the invocation.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>This implementation spares its clients from the unspecified, generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * chaotic ordering provided by {@link HashMap} (and {@link Hashtable}),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * without incurring the increased cost associated with {@link TreeMap}.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * can be used to produce a copy of a map that has the same order as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * original, regardless of the original map's implementation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     void foo(Map m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *         Map copy = new LinkedHashMap(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *         ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * This technique is particularly useful if a module takes a map on input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * copies it, and later returns results whose order is determined by that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the copy.  (Clients generally appreciate having things returned in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * order they were presented.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>A special {@link #LinkedHashMap(int,float,boolean) constructor} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * provided to create a linked hash map whose order of iteration is the order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * in which its entries were last accessed, from least-recently accessed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * most-recently (<i>access-order</i>).  This kind of map is well-suited to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * building LRU caches.  Invoking the <tt>put</tt> or <tt>get</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * results in an access to the corresponding entry (assuming it exists after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * the invocation completes).  The <tt>putAll</tt> method generates one entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * access for each mapping in the specified map, in the order that key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * mappings are provided by the specified map's entry set iterator.  <i>No
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * other methods generate entry accesses.</i> In particular, operations on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * collection-views do <i>not</i> affect the order of iteration of the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>The {@link #removeEldestEntry(Map.Entry)} method may be overridden to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * impose a policy for removing stale mappings automatically when new mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * are added to the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p>This class provides all of the optional <tt>Map</tt> operations, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * permits null elements.  Like <tt>HashMap</tt>, it provides constant-time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <tt>remove</tt>), assuming the hash function disperses elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * properly among the buckets.  Performance is likely to be just slightly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * below that of <tt>HashMap</tt>, due to the added expense of maintaining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * linked list, with one exception: Iteration over the collection-views
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * of a <tt>LinkedHashMap</tt> requires time proportional to the <i>size</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * of the map, regardless of its capacity.  Iteration over a <tt>HashMap</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * is likely to be more expensive, requiring time proportional to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <i>capacity</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>A linked hash map has two parameters that affect its performance:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <i>initial capacity</i> and <i>load factor</i>.  They are defined precisely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * as for <tt>HashMap</tt>.  Note, however, that the penalty for choosing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * excessively high value for initial capacity is less severe for this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * than for <tt>HashMap</tt>, as iteration times for this class are unaffected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * by capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * If multiple threads access a linked hash map concurrently, and at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * one of the threads modifies the map structurally, it <em>must</em> be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * synchronized externally.  This is typically accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * synchronizing on some object that naturally encapsulates the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * If no such object exists, the map should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * unsynchronized access to the map:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *   Map m = Collections.synchronizedMap(new LinkedHashMap(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * A structural modification is any operation that adds or deletes one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * mappings or, in the case of access-ordered linked hash maps, affects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * iteration order.  In insertion-ordered linked hash maps, merely changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * the value associated with a key that is already contained in the map is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * a structural modification.  <strong>In access-ordered linked hash maps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * merely querying the map with <tt>get</tt> is a structural
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * modification.</strong>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <p>The iterators returned by the <tt>iterator</tt> method of the collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * returned by all of this class's collection view methods are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <em>fail-fast</em>: if the map is structurally modified at any time after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * the iterator is created, in any way except through the iterator's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <tt>remove</tt> method, the iterator will throw a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * arbitrary, non-deterministic behavior at an undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * exception for its correctness:   <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * @see     HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * @see     Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
public class LinkedHashMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    extends HashMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    implements Map<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static final long serialVersionUID = 3801124242820219131L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * The head of the doubly linked list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private transient Entry<K,V> header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * The iteration ordering method for this linked hash map: <tt>true</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * for access-order, <tt>false</tt> for insertion-order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private final boolean accessOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * with the specified initial capacity and load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param  initialCapacity the initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param  loadFactor      the load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @throws IllegalArgumentException if the initial capacity is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *         or the load factor is nonpositive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public LinkedHashMap(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        super(initialCapacity, loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        accessOrder = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * with the specified initial capacity and a default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param  initialCapacity the initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @throws IllegalArgumentException if the initial capacity is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public LinkedHashMap(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        super(initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        accessOrder = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * with the default initial capacity (16) and load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public LinkedHashMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        accessOrder = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Constructs an insertion-ordered <tt>LinkedHashMap</tt> instance with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * the same mappings as the specified map.  The <tt>LinkedHashMap</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * instance is created with a default load factor (0.75) and an initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * capacity sufficient to hold the mappings in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param  m the map whose mappings are to be placed in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public LinkedHashMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        super(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        accessOrder = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Constructs an empty <tt>LinkedHashMap</tt> instance with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * specified initial capacity, load factor and ordering mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param  initialCapacity the initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param  loadFactor      the load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param  accessOrder     the ordering mode - <tt>true</tt> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *         access-order, <tt>false</tt> for insertion-order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @throws IllegalArgumentException if the initial capacity is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *         or the load factor is nonpositive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public LinkedHashMap(int initialCapacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                         float loadFactor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                         boolean accessOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        super(initialCapacity, loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        this.accessOrder = accessOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Called by superclass constructors and pseudoconstructors (clone,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * readObject) before any entries are inserted into the map.  Initializes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * the chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    void init() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   240
        header = new Entry<>(-1, null, null, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        header.before = header.after = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Transfers all entries to new table array.  This method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * by superclass resize.  It is overridden for performance, as it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * faster to iterate using our linked list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    void transfer(HashMap.Entry[] newTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        int newCapacity = newTable.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        for (Entry<K,V> e = header.after; e != header; e = e.after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            int index = indexFor(e.hash, newCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            e.next = newTable[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            newTable[index] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Returns <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param value value whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *         specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // Overridden to take advantage of faster iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (value==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            for (Entry e = header.after; e != header; e = e.after)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if (e.value==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            for (Entry e = header.after; e != header; e = e.after)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if (value.equals(e.value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * key.equals(k))}, then this method returns {@code v}; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * it returns {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        Entry<K,V> e = (Entry<K,V>)getEntry(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        e.recordAccess(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        super.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        header.before = header.after = header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * LinkedHashMap entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private static class Entry<K,V> extends HashMap.Entry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        // These fields comprise the doubly linked list used for iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        Entry<K,V> before, after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        Entry(int hash, K key, V value, HashMap.Entry<K,V> next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            super(hash, key, value, next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * Removes this entry from the linked list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        private void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            before.after = after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            after.before = before;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * Inserts this entry before the specified existing entry in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        private void addBefore(Entry<K,V> existingEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            after  = existingEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            before = existingEntry.before;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            before.after = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            after.before = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * This method is invoked by the superclass whenever the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * of a pre-existing entry is read by Map.get or modified by Map.set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * If the enclosing Map is access-ordered, it moves the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         * to the end of the list; otherwise, it does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        void recordAccess(HashMap<K,V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (lm.accessOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                lm.modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                addBefore(lm.header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        void recordRemoval(HashMap<K,V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private abstract class LinkedHashIterator<T> implements Iterator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        Entry<K,V> nextEntry    = header.after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        Entry<K,V> lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * The modCount value that the iterator believes that the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         * List should have.  If this expectation is violated, the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
         * has detected concurrent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return nextEntry != header;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            if (lastReturned == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            LinkedHashMap.this.remove(lastReturned.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        Entry<K,V> nextEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (nextEntry == header)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            Entry<K,V> e = lastReturned = nextEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            nextEntry = e.after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    private class KeyIterator extends LinkedHashIterator<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        public K next() { return nextEntry().getKey(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    private class ValueIterator extends LinkedHashIterator<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        public V next() { return nextEntry().value; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private class EntryIterator extends LinkedHashIterator<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        public Map.Entry<K,V> next() { return nextEntry(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    // These Overrides alter the behavior of superclass view iterator() methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    Iterator<K> newKeyIterator()   { return new KeyIterator();   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    Iterator<V> newValueIterator() { return new ValueIterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    Iterator<Map.Entry<K,V>> newEntryIterator() { return new EntryIterator(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * This override alters behavior of superclass put method. It causes newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * allocated entry to get inserted at the end of the linked list and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * removes the eldest entry if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    void addEntry(int hash, K key, V value, int bucketIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        createEntry(hash, key, value, bucketIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // Remove eldest entry if instructed, else grow capacity if appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        Entry<K,V> eldest = header.after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (removeEldestEntry(eldest)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            removeEntryForKey(eldest.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (size >= threshold)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                resize(2 * table.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * This override differs from addEntry in that it doesn't resize the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * table or remove the eldest entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    void createEntry(int hash, K key, V value, int bucketIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        HashMap.Entry<K,V> old = table[bucketIndex];
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   441
        Entry<K,V> e = new Entry<>(hash, key, value, old);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        table[bucketIndex] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        e.addBefore(header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Returns <tt>true</tt> if this map should remove its eldest entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * This method is invoked by <tt>put</tt> and <tt>putAll</tt> after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * inserting a new entry into the map.  It provides the implementor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * with the opportunity to remove the eldest entry each time a new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * is added.  This is useful if the map represents a cache: it allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * the map to reduce memory consumption by deleting stale entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * <p>Sample use: this override will allow the map to grow up to 100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * entries and then delete the eldest entry each time a new entry is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * added, maintaining a steady state of 100 entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *     private static final int MAX_ENTRIES = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *     protected boolean removeEldestEntry(Map.Entry eldest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *        return size() > MAX_ENTRIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <p>This method typically does not modify the map in any way,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * instead allowing the map to modify itself as directed by its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * return value.  It <i>is</i> permitted for this method to modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * the map directly, but if it does so, it <i>must</i> return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * <tt>false</tt> (indicating that the map should not attempt any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * further modification).  The effects of returning <tt>true</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * after modifying the map from within this method are unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p>This implementation merely returns <tt>false</tt> (so that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * map acts like a normal map - the eldest element is never removed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param    eldest The least recently inserted entry in the map, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *           this is an access-ordered map, the least recently accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *           entry.  This is the entry that will be removed it this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *           method returns <tt>true</tt>.  If the map was empty prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *           to the <tt>put</tt> or <tt>putAll</tt> invocation resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *           in this invocation, this will be the entry that was just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *           inserted; in other words, if the map contains a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *           entry, the eldest entry is also the newest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @return   <tt>true</tt> if the eldest entry should be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *           from the map; <tt>false</tt> if it should be retained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
}