jdk/src/share/classes/java/util/HashMap.java
author mduigou
Tue, 16 Apr 2013 11:17:19 -0700
changeset 16867 76499721c6c1
parent 16855 106eaf391fbc
child 17168 b7d3500f2516
permissions -rw-r--r--
8004518: Add in-place operations to Map 8010122: Add defaults for ConcurrentMap operations to Map Reviewed-by: darcy, briangoetz, mduigou, dholmes, ulfzibis Contributed-by: Doug Lea <dl@cs.oswego.edu>, Henry Jen <henry.jen@oracle.com>, Akhil Arora <akhil.arora@oracle.com>, Peter Levart <peter.levart@gmail.com>, Mike Duigou <mike.duigou@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
     2
 * Copyright (c) 1997, 2013, 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: 4110
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: 4110
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: 4110
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
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;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
    27
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
    29
import java.util.function.Consumer;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
    30
import java.util.function.BiFunction;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
    31
import java.util.function.Function;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Hash table based implementation of the <tt>Map</tt> interface.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * implementation provides all of the optional map operations, and permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <tt>null</tt> values and the <tt>null</tt> key.  (The <tt>HashMap</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * class is roughly equivalent to <tt>Hashtable</tt>, except that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * unsynchronized and permits nulls.)  This class makes no guarantees as to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the order of the map; in particular, it does not guarantee that the order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * will remain constant over time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>This implementation provides constant-time performance for the basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * operations (<tt>get</tt> and <tt>put</tt>), assuming the hash function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * disperses the elements properly among the buckets.  Iteration over
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * collection views requires time proportional to the "capacity" of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <tt>HashMap</tt> instance (the number of buckets) plus its size (the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * of key-value mappings).  Thus, it's very important not to set the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * capacity too high (or the load factor too low) if iteration performance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * important.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>An instance of <tt>HashMap</tt> has two parameters that affect its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * performance: <i>initial capacity</i> and <i>load factor</i>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <i>capacity</i> is the number of buckets in the hash table, and the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * capacity is simply the capacity at the time the hash table is created.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <i>load factor</i> is a measure of how full the hash table is allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * get before its capacity is automatically increased.  When the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * entries in the hash table exceeds the product of the load factor and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * current capacity, the hash table is <i>rehashed</i> (that is, internal data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * structures are rebuilt) so that the hash table has approximately twice the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * number of buckets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>As a general rule, the default load factor (.75) offers a good tradeoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * between time and space costs.  Higher values decrease the space overhead
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * but increase the lookup cost (reflected in most of the operations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <tt>HashMap</tt> class, including <tt>get</tt> and <tt>put</tt>).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * expected number of entries in the map and its load factor should be taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * into account when setting its initial capacity, so as to minimize the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * number of rehash operations.  If the initial capacity is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * than the maximum number of entries divided by the load factor, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * rehash operations will ever occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>If many mappings are to be stored in a <tt>HashMap</tt> instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * creating it with a sufficiently large capacity will allow the mappings to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * be stored more efficiently than letting it perform automatic rehashing as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * needed to grow the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * If multiple threads access a hash map concurrently, and at least one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * the threads modifies the map structurally, it <i>must</i> be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * synchronized externally.  (A structural modification is any operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * that adds or deletes one or more mappings; merely changing the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * associated with a key that an instance already contains is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * structural modification.)  This is typically accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * synchronizing on some object that naturally encapsulates the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * If no such object exists, the map should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * unsynchronized access to the map:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   Map m = Collections.synchronizedMap(new HashMap(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p>The iterators returned by all of this class's "collection view methods"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * are <i>fail-fast</i>: if the map is structurally modified at any time after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * the iterator is created, in any way except through the iterator's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <tt>remove</tt> method, the iterator will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * {@link ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * arbitrary, non-deterministic behavior at an undetermined time in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * exception for its correctness: <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * @author  Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @see     Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
public class HashMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    implements Map<K,V>, Cloneable, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * The default initial capacity - MUST be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   136
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * The maximum capacity, used if a higher value is implicitly specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * by either of the constructors with arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * MUST be a power of two <= 1<<30.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static final int MAXIMUM_CAPACITY = 1 << 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * The load factor used when none specified in constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   151
     * An empty table instance to share when the table is not inflated.
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   152
     */
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   153
    static final Entry<?,?>[] EMPTY_TABLE = {};
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   154
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   155
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * The table, resized as necessary. Length MUST Always be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   158
    transient Entry<?,?>[] table = EMPTY_TABLE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * The number of key-value mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    transient int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * The next size value at which to resize (capacity * load factor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   169
    // If table == EMPTY_TABLE then this is the initial capacity at which the
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   170
    // table will be created when inflated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    int threshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * The load factor for the hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    final float loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * The number of times this HashMap has been structurally modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Structural modifications are those that change the number of mappings in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * the HashMap or otherwise modify its internal structure (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * rehash).  This field is used to make iterators on Collection-views of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * the HashMap fail-fast.  (See ConcurrentModificationException).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
65
51fc1d79463f 6625725: (coll) modCount should not be volatile
martin
parents: 2
diff changeset
   187
    transient int modCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   189
    private static class Holder {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   190
         /**
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   191
         *
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   192
         */
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   193
        static final sun.misc.Unsafe UNSAFE;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   194
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   195
        /**
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   196
         * Offset of "final" hashSeed field we must set in
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   197
         * readObject() method.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   198
         */
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   199
        static final long HASHSEED_OFFSET;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   200
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   201
        static {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   202
            try {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   203
                UNSAFE = sun.misc.Unsafe.getUnsafe();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   204
                HASHSEED_OFFSET = UNSAFE.objectFieldOffset(
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   205
                    HashMap.class.getDeclaredField("hashSeed"));
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   206
            } catch (NoSuchFieldException | SecurityException e) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   207
                throw new InternalError("Failed to record hashSeed offset", e);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   208
            }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   209
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   210
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   211
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   212
    /**
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   213
     * A randomizing value associated with this instance that is applied to
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   214
     * hash code of keys to make hash collisions harder to find.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   215
     */
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   216
    transient final int hashSeed = sun.misc.Hashing.randomHashSeed(this);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   217
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Constructs an empty <tt>HashMap</tt> with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * capacity and load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param  initialCapacity the initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param  loadFactor      the load factor
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 HashMap(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (initialCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new IllegalArgumentException("Illegal initial capacity: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                               initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (initialCapacity > MAXIMUM_CAPACITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            initialCapacity = MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new IllegalArgumentException("Illegal load factor: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                               loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        this.loadFactor = loadFactor;
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   238
        threshold = initialCapacity;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Constructs an empty <tt>HashMap</tt> with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * capacity and the default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param  initialCapacity the initial capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @throws IllegalArgumentException if the initial capacity is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public HashMap(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Constructs an empty <tt>HashMap</tt> with the default initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * (16) and the default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public HashMap() {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   258
        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Constructs a new <tt>HashMap</tt> with the same mappings as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * specified <tt>Map</tt>.  The <tt>HashMap</tt> is created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * default load factor (0.75) and an initial capacity sufficient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * hold the mappings in the specified <tt>Map</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param   m the map whose mappings are to be placed in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @throws  NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public HashMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                      DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   273
        inflateTable(threshold);
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   274
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        putAllForCreate(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   278
    private static int roundUpToPowerOf2(int number) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   279
        // assert number >= 0 : "number must be non-negative";
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   280
        int rounded = number >= MAXIMUM_CAPACITY
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   281
                ? MAXIMUM_CAPACITY
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   282
                : (rounded = Integer.highestOneBit(number)) != 0
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   283
                    ? (Integer.bitCount(number) > 1) ? rounded << 1 : rounded
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   284
                    : 1;
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   285
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   286
        return rounded;
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   287
    }
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   288
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   289
    /**
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   290
     * Inflates the table.
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   291
     */
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   292
    private void inflateTable(int toSize) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   293
        // Find a power of 2 >= toSize
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   294
        int capacity = roundUpToPowerOf2(toSize);
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   295
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   296
        threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   297
        table = new Entry[capacity];
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   298
    }
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   299
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    // internal utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Initialization hook for subclasses. This method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * in all constructors and pseudo-constructors (clone, readObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * after HashMap has been initialized but before any entries have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * been inserted.  (In the absence of this method, readObject would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * require explicit knowledge of subclasses.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   313
     * Retrieve object hash code and applies a supplemental hash function to the
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   314
     * result hash, which defends against poor quality hash functions.  This is
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   315
     * critical because HashMap uses power-of-two length hash tables, that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * otherwise encounter collisions for hashCodes that do not differ
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   317
     * in lower bits.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   319
    final int hash(Object k) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   320
        if (k instanceof String) {
13018
92c86cea72a8 7173919: Minor optimization of hashing methods
mduigou
parents: 12883
diff changeset
   321
            return ((String) k).hash32();
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   322
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   323
13018
92c86cea72a8 7173919: Minor optimization of hashing methods
mduigou
parents: 12883
diff changeset
   324
        int  h = hashSeed ^ k.hashCode();
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   325
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // This function ensures that hashCodes that differ only by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // constant multiples at each bit position have a bounded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // number of collisions (approximately 8 at default load factor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        h ^= (h >>> 20) ^ (h >>> 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return h ^ (h >>> 7) ^ (h >>> 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Returns index for hash code h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    static int indexFor(int h, int length) {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   337
        // assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return h & (length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Returns the number of key-value mappings in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Returns <tt>true</tt> if this map contains no key-value mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @return <tt>true</tt> if this map contains no key-value mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * key.equals(k))}, then this method returns {@code v}; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * it returns {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @see #put(Object, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   376
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public V get(Object key) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   378
        Entry<K,V> entry = getEntry(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   380
        return null == entry ? null : entry.getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   383
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   384
    public V getOrDefault(Object key, V defaultValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   385
        Entry<K,V> entry = getEntry(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   386
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   387
        return (entry == null) ? defaultValue : entry.getValue();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   388
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   389
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Returns <tt>true</tt> if this map contains a mapping for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param   key   The key whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @return <tt>true</tt> if this map contains a mapping for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return getEntry(key) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Returns the entry associated with the specified key in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * HashMap.  Returns null if the HashMap contains no mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   407
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    final Entry<K,V> getEntry(Object key) {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   409
        if (isEmpty()) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   410
            return null;
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   411
        }
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   412
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   413
        int hash = (key == null) ? 0 : hash(key);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   414
        for (Entry<?,?> e = table[indexFor(hash, table.length)];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
             e != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
             e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            Object k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            if (e.hash == hash &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                ((k = e.key) == key || (key != null && key.equals(k))))
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   420
                return (Entry<K,V>)e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Associates the specified value with the specified key in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * If the map previously contained a mapping for the key, the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param key key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @param value value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    public V put(K key, V value) {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   438
        if (table == EMPTY_TABLE) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   439
            inflateTable(threshold);
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   440
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return putForNullKey(value);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   443
        int hash = hash(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        int i = indexFor(hash, table.length);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   445
        @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   446
        Entry<K,V> e = (Entry<K,V>)table[i];
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   447
        for(; e != null; e = e.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            Object k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                V oldValue = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                e.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                e.recordAccess(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        addEntry(hash, key, value, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Offloaded version of put for null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    private V putForNullKey(V value) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   466
        @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   467
        Entry<K,V> e = (Entry<K,V>)table[0];
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   468
        for(; e != null; e = e.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (e.key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                V oldValue = e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                e.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                e.recordAccess(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        addEntry(0, null, value, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * This method is used instead of put by constructors and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * pseudoconstructors (clone, readObject).  It does not resize the table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * check for comodification, etc.  It calls createEntry rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * addEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private void putForCreate(K key, V value) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   488
        int hash = null == key ? 0 : hash(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        int i = indexFor(hash, table.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * Look for preexisting entry for key.  This will never happen for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         * clone or deserialize.  It will only happen for construction if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         * input Map is a sorted map whose ordering is inconsistent w/ equals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   496
        for (@SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   497
             Entry<?,V> e = (Entry<?,V>)table[i]; e != null; e = e.next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            Object k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (e.hash == hash &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                ((k = e.key) == key || (key != null && key.equals(k)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                e.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        createEntry(hash, key, value, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    private void putAllForCreate(Map<? extends K, ? extends V> m) {
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 715
diff changeset
   510
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            putForCreate(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Rehashes the contents of this map into a new array with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * larger capacity.  This method is called automatically when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * number of keys in this map reaches its threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * If current capacity is MAXIMUM_CAPACITY, this method does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * resize the map, but sets threshold to Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * This has the effect of preventing future calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @param newCapacity the new capacity, MUST be a power of two;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *        must be greater than current capacity unless current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *        capacity is MAXIMUM_CAPACITY (in which case value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *        is irrelevant).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    void resize(int newCapacity) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   529
        Entry<?,?>[] oldTable = table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        int oldCapacity = oldTable.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (oldCapacity == MAXIMUM_CAPACITY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            threshold = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   536
        Entry<?,?>[] newTable = new Entry<?,?>[newCapacity];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        transfer(newTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        table = newTable;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   539
        threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Transfers all entries from current table to newTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   545
    @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   546
    void transfer(Entry<?,?>[] newTable) {
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   547
        Entry<?,?>[] src = table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        int newCapacity = newTable.length;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   549
        for (int j = 0; j < src.length; j++ ) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   550
            Entry<K,V> e = (Entry<K,V>) src[j];
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   551
            while(null != e) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   552
                Entry<K,V> next = e.next;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   553
                int i = indexFor(e.hash, newCapacity);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   554
                e.next = (Entry<K,V>) newTable[i];
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   555
                newTable[i] = e;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   556
                e = next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   559
        Arrays.fill(table, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Copies all of the mappings from the specified map to this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * These mappings will replace any mappings that this map had for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * any of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @param m mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        int numKeysToBeAdded = m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (numKeysToBeAdded == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   575
        if (table == EMPTY_TABLE) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   576
            inflateTable((int) Math.max(numKeysToBeAdded * loadFactor, threshold));
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   577
        }
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   578
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         * Expand the map if the map if the number of mappings to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
         * is greater than or equal to threshold.  This is conservative; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * obvious condition is (m.size() + size) >= threshold, but this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         * condition could result in a map with twice the appropriate capacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * if the keys to be added overlap with the keys already in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         * By using the conservative calculation, we subject ourself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         * to at most one extra resize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (numKeysToBeAdded > threshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if (targetCapacity > MAXIMUM_CAPACITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                targetCapacity = MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            int newCapacity = table.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            while (newCapacity < targetCapacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                newCapacity <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (newCapacity > table.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                resize(newCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 715
diff changeset
   599
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            put(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Removes the mapping for the specified key from this map if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @param  key key whose mapping is to be removed from the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        Entry<K,V> e = removeEntryForKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return (e == null ? null : e.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   617
    // optimized implementations of default methods in Map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   618
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   619
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   620
    public V putIfAbsent(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   621
        if (table == EMPTY_TABLE) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   622
            inflateTable(threshold);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   623
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   624
        int hash = (key == null) ? 0 : hash(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   625
        int i = indexFor(hash, table.length);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   626
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   627
        Entry<K,V> e = (Entry<K,V>)table[i];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   628
        for(; e != null; e = e.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   629
            if (e.hash == hash && Objects.equals(e.key, key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   630
                if(e.value != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   631
                    return e.value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   632
                }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   633
                e.value = value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   634
                modCount++;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   635
                e.recordAccess(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   636
                return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   637
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   638
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   639
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   640
        modCount++;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   641
        addEntry(hash, key, value, i);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   642
        return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   643
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   644
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   645
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   646
    public boolean remove(Object key, Object value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   647
        if (isEmpty()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   648
            return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   649
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   650
        int hash = (key == null) ? 0 : hash(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   651
        int i = indexFor(hash, table.length);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   652
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   653
        Entry<K,V> prev = (Entry<K,V>)table[i];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   654
        Entry<K,V> e = prev;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   655
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   656
        while (e != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   657
            Entry<K,V> next = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   658
            if (e.hash == hash && Objects.equals(e.key, key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   659
                if (!Objects.equals(e.value, value)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   660
                    return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   661
                }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   662
                modCount++;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   663
                size--;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   664
                if (prev == e)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   665
                    table[i] = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   666
                else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   667
                    prev.next = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   668
                e.recordRemoval(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   669
                return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   670
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   671
            prev = e;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   672
            e = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   673
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   674
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   675
        return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   676
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   677
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   678
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   679
    public boolean replace(K key, V oldValue, V newValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   680
        if (isEmpty()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   681
            return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   682
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   683
        int hash = (key == null) ? 0 : hash(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   684
        int i = indexFor(hash, table.length);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   685
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   686
        Entry<K,V> e = (Entry<K,V>)table[i];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   687
        for (; e != null; e = e.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   688
            if (e.hash == hash && Objects.equals(e.key, key) && Objects.equals(e.value, oldValue)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   689
                e.value = newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   690
                e.recordAccess(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   691
                return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   692
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   693
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   694
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   695
        return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   696
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   697
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   698
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   699
    public V replace(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   700
        if (isEmpty()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   701
            return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   702
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   703
        int hash = (key == null) ? 0 : hash(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   704
        int i = indexFor(hash, table.length);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   705
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   706
        Entry<K,V> e = (Entry<K,V>)table[i];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   707
        for (; e != null; e = e.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   708
            if (e.hash == hash && Objects.equals(e.key, key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   709
                V oldValue = e.value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   710
                e.value = value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   711
                e.recordAccess(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   712
                return oldValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   713
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   714
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   715
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   716
        return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   717
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   718
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   719
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   720
    public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   721
        if (table == EMPTY_TABLE) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   722
            inflateTable(threshold);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   723
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   724
        int hash = (key == null) ? 0 : hash(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   725
        int i = indexFor(hash, table.length);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   726
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   727
        Entry<K,V> e = (Entry<K,V>)table[i];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   728
        for (; e != null; e = e.next) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   729
            if (e.hash == hash && Objects.equals(e.key, key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   730
                V oldValue = e.value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   731
                return oldValue == null ? (e.value = mappingFunction.apply(key)) : oldValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   732
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   733
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   734
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   735
        V newValue = mappingFunction.apply(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   736
        if (newValue != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   737
            modCount++;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   738
            addEntry(hash, key, newValue, i);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   739
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   740
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   741
        return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   742
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   743
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   744
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   745
    public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   746
        if (isEmpty()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   747
            return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   748
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   749
        int hash = (key == null) ? 0 : hash(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   750
        int i = indexFor(hash, table.length);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   751
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   752
        Entry<K,V> prev = (Entry<K,V>)table[i];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   753
        Entry<K,V> e = prev;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   754
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   755
        while (e != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   756
            Entry<K,V> next = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   757
            if (e.hash == hash && Objects.equals(e.key, key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   758
                V oldValue = e.value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   759
                if (oldValue == null)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   760
                    break;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   761
                V newValue = remappingFunction.apply(key, oldValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   762
                modCount++;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   763
                if (newValue == null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   764
                    size--;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   765
                    if (prev == e)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   766
                        table[i] = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   767
                    else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   768
                        prev.next = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   769
                    e.recordRemoval(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   770
                } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   771
                    e.value = newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   772
                    e.recordAccess(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   773
                }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   774
                return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   775
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   776
            prev = e;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   777
            e = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   778
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   779
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   780
        return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   781
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   782
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   783
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   784
    public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   785
        if (table == EMPTY_TABLE) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   786
            inflateTable(threshold);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   787
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   788
        int hash = (key == null) ? 0 : hash(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   789
        int i = indexFor(hash, table.length);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   790
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   791
        Entry<K,V> prev = (Entry<K,V>)table[i];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   792
        Entry<K,V> e = prev;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   793
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   794
        while (e != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   795
            Entry<K,V> next = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   796
            if (e.hash == hash && Objects.equals(e.key, key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   797
                V oldValue = e.value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   798
                V newValue = remappingFunction.apply(key, oldValue);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   799
                if (newValue != oldValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   800
                    modCount++;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   801
                    if (newValue == null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   802
                        size--;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   803
                        if (prev == e)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   804
                            table[i] = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   805
                        else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   806
                            prev.next = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   807
                        e.recordRemoval(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   808
                    } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   809
                        e.value = newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   810
                        e.recordAccess(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   811
                    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   812
                }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   813
                return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   814
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   815
            prev = e;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   816
            e = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   817
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   818
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   819
        V newValue = remappingFunction.apply(key, null);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   820
        if (newValue != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   821
            modCount++;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   822
            addEntry(hash, key, newValue, i);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   823
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   824
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   825
        return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   826
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   827
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   828
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   829
    public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   830
        if (table == EMPTY_TABLE) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   831
            inflateTable(threshold);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   832
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   833
        int hash = (key == null) ? 0 : hash(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   834
        int i = indexFor(hash, table.length);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   835
        @SuppressWarnings("unchecked")
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   836
        Entry<K,V> prev = (Entry<K,V>)table[i];
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   837
        Entry<K,V> e = prev;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   838
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   839
        while (e != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   840
            Entry<K,V> next = e.next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   841
            if (e.hash == hash && Objects.equals(e.key, key)) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   842
                V oldValue = e.value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   843
                V newValue = remappingFunction.apply(oldValue, value);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   844
                modCount++;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   845
                if (newValue == null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   846
                    size--;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   847
                    if (prev == e)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   848
                        table[i] = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   849
                    else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   850
                        prev.next = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   851
                    e.recordRemoval(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   852
                } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   853
                    e.value = newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   854
                    e.recordAccess(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   855
                }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   856
                return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   857
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   858
            prev = e;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   859
            e = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   860
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   861
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   862
        if (value != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   863
            modCount++;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   864
            addEntry(hash, key, value, i);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   865
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   866
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   867
        return value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   868
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   869
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   870
    // end of optimized implementations of default methods in Map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   871
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * Removes and returns the entry associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * in the HashMap.  Returns null if the HashMap contains no mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * for this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    final Entry<K,V> removeEntryForKey(Object key) {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   878
        if (isEmpty()) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   879
            return null;
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   880
        }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   881
        int hash = (key == null) ? 0 : hash(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        int i = indexFor(hash, table.length);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   883
        @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   884
            Entry<K,V> prev = (Entry<K,V>)table[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        Entry<K,V> e = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        while (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            Entry<K,V> next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            Object k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            if (e.hash == hash &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                ((k = e.key) == key || (key != null && key.equals(k)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                if (prev == e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                    table[i] = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    prev.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                e.recordRemoval(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            prev = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            e = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    /**
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   909
     * Special version of remove for EntrySet using {@code Map.Entry.equals()}
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   910
     * for matching.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    final Entry<K,V> removeMapping(Object o) {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   913
        if (isEmpty() || !(o instanceof Map.Entry))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   916
        Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        Object key = entry.getKey();
12883
0553a7aa3404 7174736: JCK test api/java_util/HashMap/index_EntrySet failing
mduigou
parents: 12863
diff changeset
   918
        int hash = (key == null) ? 0 : hash(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        int i = indexFor(hash, table.length);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   920
        @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   921
            Entry<K,V> prev = (Entry<K,V>)table[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        Entry<K,V> e = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        while (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            Entry<K,V> next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            if (e.hash == hash && e.equals(entry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                if (prev == e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    table[i] = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                    prev.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                e.recordRemoval(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            prev = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            e = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        modCount++;
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   949
        Arrays.fill(table, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * Returns <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @param value value whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * @return <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *         specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            return containsNullValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   965
        Entry<?,?>[] tab = table;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   966
        for (int i = 0; i < tab.length; i++)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   967
            for (Entry<?,?> e = tab[i]; e != null; e = e.next)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                if (value.equals(e.value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * Special-case code for containsValue with null argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    private boolean containsNullValue() {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   977
        Entry<?,?>[] tab = table;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   978
        for (int i = 0; i < tab.length; i++)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   979
            for (Entry<?,?> e = tab[i]; e != null; e = e.next)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                if (e.value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * values themselves are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @return a shallow copy of this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   991
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        HashMap<K,V> result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            result = (HashMap<K,V>)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            // assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        }
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   999
        if (result.table != EMPTY_TABLE) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1000
            result.inflateTable(Math.min(
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1001
                (int) Math.min(
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1002
                    size * Math.min(1 / loadFactor, 4.0f),
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1003
                    // we have limits...
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1004
                    HashMap.MAXIMUM_CAPACITY),
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1005
                table.length));
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1006
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        result.entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        result.modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        result.size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        result.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        result.putAllForCreate(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    static class Entry<K,V> implements Map.Entry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        final K key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        V value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        Entry<K,V> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        final int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
         * Creates new entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        Entry(int h, K k, V v, Entry<K,V> n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            value = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            next = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            key = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            hash = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        public final K getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        public final V getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        public final V setValue(V newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            V oldValue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            value = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        public final boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1049
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            Object k1 = getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            Object k2 = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            if (k1 == k2 || (k1 != null && k1.equals(k2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                Object v1 = getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                Object v2 = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                if (v1 == v2 || (v1 != null && v1.equals(v2)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        public final int hashCode() {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1062
            return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        public final String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            return getKey() + "=" + getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
         * This method is invoked whenever the value in an entry is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
         * overwritten by an invocation of put(k,v) for a key k that's already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
         * in the HashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        void recordAccess(HashMap<K,V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         * This method is invoked whenever the entry is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         * removed from the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        void recordRemoval(HashMap<K,V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * Adds a new entry with the specified key, value and hash code to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * the specified bucket.  It is the responsibility of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * method to resize the table if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * Subclass overrides this to alter the behavior of put method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    void addEntry(int hash, K key, V value, int bucketIndex) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1093
        if ((size >= threshold) && (null != table[bucketIndex])) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            resize(2 * table.length);
12863
77e41e310650 7173432: Handle null key at HashMap resize
mduigou
parents: 12859
diff changeset
  1095
            hash = (null != key) ? hash(key) : 0;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1096
            bucketIndex = indexFor(hash, table.length);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1097
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1098
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1099
        createEntry(hash, key, value, bucketIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * Like addEntry except that this version is used when creating entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * as part of Map construction or "pseudo-construction" (cloning,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * deserialization).  This version needn't worry about resizing the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * Subclass overrides this to alter the behavior of HashMap(Map),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * clone, and readObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    void createEntry(int hash, K key, V value, int bucketIndex) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1111
        @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1112
            Entry<K,V> e = (Entry<K,V>)table[bucketIndex];
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1113
        table[bucketIndex] = new Entry<>(hash, key, value, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    private abstract class HashIterator<E> implements Iterator<E> {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1118
        Entry<?,?> next;        // next entry to return
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        int expectedModCount;   // For fast-fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        int index;              // current slot
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1121
        Entry<?,?> current;     // current entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        HashIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            if (size > 0) { // advance to first entry
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1126
                Entry<?,?>[] t = table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                while (index < t.length && (next = t[index++]) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                    ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        public final boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            return next != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1136
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        final Entry<K,V> nextEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                throw new ConcurrentModificationException();
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1140
            Entry<?,?> e = next;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            if ((next = e.next) == null) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1145
                Entry<?,?>[] t = table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                while (index < t.length && (next = t[index++]) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                    ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            current = e;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1150
            return (Entry<K,V>)e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            if (current == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            Object k = current.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            current = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            HashMap.this.removeEntryForKey(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    private final class ValueIterator extends HashIterator<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        public V next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            return nextEntry().value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    private final class KeyIterator extends HashIterator<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            return nextEntry().getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            return nextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    // Subclass overrides these to alter behavior of views' iterator() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    Iterator<K> newKeyIterator()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        return new KeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    Iterator<V> newValueIterator()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        return new ValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    Iterator<Map.Entry<K,V>> newEntryIterator()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        return new EntryIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    private transient Set<Map.Entry<K,V>> entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * the iterator's own <tt>remove</tt> operation), the results of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * the iteration are undefined.  The set supports element removal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * which removes the corresponding mapping from the map, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        Set<K> ks = keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        return (ks != null ? ks : (keySet = new KeySet()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    private final class KeySet extends AbstractSet<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            return newKeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            return containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            return HashMap.this.removeEntryForKey(o) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            HashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * modified while an iteration over the collection is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * (except through the iterator's own <tt>remove</tt> operation),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        Collection<V> vs = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        return (vs != null ? vs : (values = new Values()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    private final class Values extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            return newValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            return containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            HashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * the iterator's own <tt>remove</tt> operation, or through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * <tt>setValue</tt> operation on a map entry returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * iterator) the results of the iteration are undefined.  The set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * <tt>clear</tt> operations.  It does not support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * @return a set view of the mappings contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        return entrySet0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    private Set<Map.Entry<K,V>> entrySet0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        Set<Map.Entry<K,V>> es = entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        return es != null ? es : (entrySet = new EntrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            return newEntryIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1300
            Map.Entry<?,?> e = (Map.Entry<?,?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            Entry<K,V> candidate = getEntry(e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            return candidate != null && candidate.equals(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            return removeMapping(o) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            HashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * Save the state of the <tt>HashMap</tt> instance to a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * @serialData The <i>capacity</i> of the HashMap (the length of the
16725
9e7cfcc4ff6a 8011199: Backout changeset JDK-7143928 (2b34a1eb3153)
mduigou
parents: 16723
diff changeset
  1320
     *             bucket array) is emitted (int), followed by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *             <i>size</i> (an int, the number of key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     *             mappings), followed by the key (Object) and value (Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     *             for each key-value mapping.  The key-value mappings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *             emitted in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        // Write out the threshold, loadfactor, and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        // Write out number of buckets
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1333
        if (table==EMPTY_TABLE) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1334
            s.writeInt(roundUpToPowerOf2(threshold));
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1335
        } else {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1336
           s.writeInt(table.length);
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1337
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        // Write out size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        s.writeInt(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        // Write out keys and values (alternating)
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1343
        if (size > 0) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1344
            for(Map.Entry<K,V> e : entrySet0()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                s.writeObject(e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                s.writeObject(e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    private static final long serialVersionUID = 362498820763181265L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    /**
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1354
     * Reconstitute the {@code HashMap} instance from a stream (i.e.,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1360
        // Read in the threshold (ignored), loadfactor, and any hidden stuff
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        s.defaultReadObject();
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1362
        if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1363
            throw new InvalidObjectException("Illegal load factor: " +
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1364
                                               loadFactor);
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1365
        }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1366
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1367
        // set other fields that need values
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1368
        Holder.UNSAFE.putIntVolatile(this, Holder.HASHSEED_OFFSET,
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1369
                sun.misc.Hashing.randomHashSeed(this));
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1370
        table = EMPTY_TABLE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1372
        // Read in number of buckets
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1373
        s.readInt(); // ignored.
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1374
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1375
        // Read number of mappings
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1376
        int mappings = s.readInt();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1377
        if (mappings < 0)
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1378
            throw new InvalidObjectException("Illegal mappings count: " +
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1379
                                               mappings);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1381
        // capacity chosen by number of mappings and desired load (if >= 0.25)
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1382
        int capacity = (int) Math.min(
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1383
                    mappings * Math.min(1 / loadFactor, 4.0f),
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1384
                    // we have limits...
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1385
                    HashMap.MAXIMUM_CAPACITY);
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1386
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1387
        // allocate the bucket array;
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1388
        if (mappings > 0) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1389
            inflateTable(capacity);
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1390
        } else {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1391
            threshold = capacity;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1392
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1393
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        init();  // Give subclass a chance to do its thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        // Read the keys and values, and put the mappings in the HashMap
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1397
        for (int i=0; i<mappings; i++) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1398
            @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1399
                K key = (K) s.readObject();
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1400
            @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1401
                V value = (V) s.readObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            putForCreate(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    // These methods are used when serializing HashSets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    int   capacity()     { return table.length; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    float loadFactor()   { return loadFactor;   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
}