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