1 /* HashMap.java -- a class providing a basic hashtable data structure,
2 mapping Object --> Object
3 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 As a special exception, if you link this library with other files to
23 produce an executable, this library does not by itself cause the
24 resulting executable to be covered by the GNU General Public License.
25 This exception does not however invalidate any other reasons why the
26 executable file might be covered by the GNU General Public License. */
31 import java.io.IOException;
32 import java.io.Serializable;
33 import java.io.ObjectInputStream;
34 import java.io.ObjectOutputStream;
36 // NOTE: This implementation is very similar to that of Hashtable. If you fix
37 // a bug in here, chances are you should make a similar change to the Hashtable
40 // NOTE: This implementation has some nasty coding style in order to
41 // support LinkedHashMap, which extends this.
44 * This class provides a hashtable-backed implementation of the
48 * It uses a hash-bucket approach; that is, hash collisions are handled
49 * by linking the new node off of the pre-existing node (or list of
50 * nodes). In this manner, techniques such as linear probing (which
51 * can cause primary clustering) and rehashing (which does not fit very
52 * well with Java's method of precomputing hash codes) are avoided.
55 * Under ideal circumstances (no collisions), HashMap offers O(1)
56 * performance on most operations (<code>containsValue()</code> is,
57 * of course, O(n)). In the worst case (all keys map to the same
58 * hash code -- very unlikely), most operations are O(n).
61 * HashMap is part of the JDK1.2 Collections API. It differs from
62 * Hashtable in that it accepts the null key and null values, and it
63 * does not support "Enumeration views." Also, it is not synchronized;
64 * if you plan to use it in multiple threads, consider using:<br>
65 * <code>Map m = Collections.synchronizedMap(new HashMap(...));</code>
68 * The iterators are <i>fail-fast</i>, meaning that any structural
69 * modification, except for <code>remove()</code> called on the iterator
70 * itself, cause the iterator to throw a
71 * <code>ConcurrentModificationException</code> rather than exhibit
72 * non-deterministic behavior.
74 * @author Jon Zeppieri
75 * @author Jochen Hoenicke
76 * @author Bryce McKinlay
77 * @author Eric Blake <ebb9@email.byu.edu>
78 * @see Object#hashCode()
83 * @see IdentityHashMap
86 * @status updated to 1.4
88 public class HashMap extends AbstractMap
89 implements Map, Cloneable, Serializable
92 * Default number of buckets. This is the value the JDK 1.3 uses. Some
93 * early documentation specified this value as 101. That is incorrect.
94 * Package visible for use by HashSet.
96 static final int DEFAULT_CAPACITY = 11;
99 * The default load factor; this is explicitly specified by the spec.
100 * Package visible for use by HashSet.
102 static final float DEFAULT_LOAD_FACTOR = 0.75f;
105 * Compatible with JDK 1.2.
107 private static final long serialVersionUID = 362498820763181265L;
110 * The rounded product of the capacity and the load factor; when the number
111 * of elements exceeds the threshold, the HashMap calls
112 * <code>rehash()</code>.
113 * @serial the threshold for rehashing
115 private int threshold;
118 * Load factor of this HashMap: used in computing the threshold.
119 * Package visible for use by HashSet.
120 * @serial the load factor
122 final float loadFactor;
125 * Array containing the actual key-value mappings.
126 * Package visible for use by nested and subclasses.
128 transient HashEntry[] buckets;
131 * Counts the number of modifications this HashMap has undergone, used
132 * by Iterators to know when to throw ConcurrentModificationExceptions.
133 * Package visible for use by nested and subclasses.
135 transient int modCount;
138 * The size of this HashMap: denotes the number of key-value pairs.
139 * Package visible for use by nested and subclasses.
144 * The cache for {@link #entrySet()}.
146 private transient Set entries;
149 * Class to represent an entry in the hash table. Holds a single key-value
150 * pair. Package visible for use by subclass.
152 * @author Eric Blake <ebb9@email.byu.edu>
154 static class HashEntry extends BasicMapEntry
157 * The next entry in the linked list. Package visible for use by subclass.
162 * Simple constructor.
164 * @param value the value
166 HashEntry(Object key, Object value)
172 * Called when this entry is removed from the map. This version simply
173 * returns the value, but in LinkedHashMap, it must also do bookkeeping.
175 * @return the value of this key as it is removed
184 * Construct a new HashMap with the default capacity (11) and the default
185 * load factor (0.75).
189 this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
193 * Construct a new HashMap from the given Map, with initial capacity
194 * the greater of the size of <code>m</code> or the default of 11.
197 * Every element in Map m will be put into this new HashMap.
199 * @param m a Map whose key / value pairs will be put into the new HashMap.
200 * <b>NOTE: key / value pairs are not cloned in this constructor.</b>
201 * @throws NullPointerException if m is null
203 public HashMap(Map m)
205 this(Math.max(m.size() * 2, DEFAULT_CAPACITY), DEFAULT_LOAD_FACTOR);
210 * Construct a new HashMap with a specific inital capacity and
211 * default load factor of 0.75.
213 * @param initialCapacity the initial capacity of this HashMap (>=0)
214 * @throws IllegalArgumentException if (initialCapacity < 0)
216 public HashMap(int initialCapacity)
218 this(initialCapacity, DEFAULT_LOAD_FACTOR);
222 * Construct a new HashMap with a specific inital capacity and load factor.
224 * @param initialCapacity the initial capacity (>=0)
225 * @param loadFactor the load factor (> 0, not NaN)
226 * @throws IllegalArgumentException if (initialCapacity < 0) ||
227 * ! (loadFactor > 0.0)
229 public HashMap(int initialCapacity, float loadFactor)
231 if (initialCapacity < 0)
232 throw new IllegalArgumentException("Illegal Capacity: "
234 if (! (loadFactor > 0)) // check for NaN too
235 throw new IllegalArgumentException("Illegal Load: " + loadFactor);
237 if (initialCapacity == 0)
239 buckets = new HashEntry[initialCapacity];
240 this.loadFactor = loadFactor;
241 threshold = (int) (initialCapacity * loadFactor);
245 * Returns the number of kay-value mappings currently in this Map.
255 * Returns true if there are no key-value mappings currently in this Map.
257 * @return <code>size() == 0</code>
259 public boolean isEmpty()
265 * Return the value in this HashMap associated with the supplied key,
266 * or <code>null</code> if the key maps to nothing. NOTE: Since the value
267 * could also be null, you must use containsKey to see if this key
268 * actually maps to something.
270 * @param key the key for which to fetch an associated value
271 * @return what the key maps to, if present
272 * @see #put(Object, Object)
273 * @see #containsKey(Object)
275 public Object get(Object key)
278 HashEntry e = buckets[idx];
281 if (equals(key, e.key))
289 * Returns true if the supplied object <code>equals()</code> a key
292 * @param key the key to search for in this HashMap
293 * @return true if the key is in the table
294 * @see #containsValue(Object)
296 public boolean containsKey(Object key)
299 HashEntry e = buckets[idx];
302 if (equals(key, e.key))
310 * Puts the supplied value into the Map, mapped by the supplied key.
311 * The value may be retrieved by any object which <code>equals()</code>
312 * this key. NOTE: Since the prior value could also be null, you must
313 * first use containsKey if you want to see if you are replacing the
316 * @param key the key used to locate the value
317 * @param value the value to be stored in the HashMap
318 * @return the prior mapping of the key, or null if there was none
320 * @see Object#equals(Object)
322 public Object put(Object key, Object value)
325 HashEntry e = buckets[idx];
329 if (equals(key, e.key))
330 // Must use this method for necessary bookkeeping in LinkedHashMap.
331 return e.setValue(value);
336 // At this point, we know we need to add a new entry.
338 if (++size > threshold)
341 // Need a new hash value to suit the bigger table.
345 // LinkedHashMap cannot override put(), hence this call.
346 addEntry(key, value, idx, true);
351 * Copies all elements of the given map into this hashtable. If this table
352 * already has a mapping for a key, the new mapping replaces the current
355 * @param m the map to be hashed into this
357 public void putAll(Map m)
359 Iterator itr = m.entrySet().iterator();
361 for (int msize = m.size(); msize > 0; msize--)
363 Map.Entry e = (Map.Entry) itr.next();
364 // Optimize in case the Entry is one of our own.
365 if (e instanceof BasicMapEntry)
367 BasicMapEntry entry = (BasicMapEntry) e;
368 put(entry.key, entry.value);
372 put(e.getKey(), e.getValue());
378 * Removes from the HashMap and returns the value which is mapped by the
379 * supplied key. If the key maps to nothing, then the HashMap remains
380 * unchanged, and <code>null</code> is returned. NOTE: Since the value
381 * could also be null, you must use containsKey to see if you are
382 * actually removing a mapping.
384 * @param key the key used to locate the value to remove
385 * @return whatever the key mapped to, if present
387 public Object remove(Object key)
390 HashEntry e = buckets[idx];
391 HashEntry last = null;
395 if (equals(key, e.key))
399 buckets[idx] = e.next;
403 // Method call necessary for LinkedHashMap to work correctly.
413 * Clears the Map so it has no keys. This is O(1).
420 Arrays.fill(buckets, null);
426 * Returns true if this HashMap contains a value <code>o</code>, such that
427 * <code>o.equals(value)</code>.
429 * @param value the value to search for in this HashMap
430 * @return true if at least one key maps to the value
431 * @see containsKey(Object)
433 public boolean containsValue(Object value)
435 for (int i = buckets.length - 1; i >= 0; i--)
437 HashEntry e = buckets[i];
440 if (equals(value, e.value))
449 * Returns a shallow clone of this HashMap. The Map itself is cloned,
450 * but its contents are not. This is O(n).
454 public Object clone()
459 copy = (HashMap) super.clone();
461 catch (CloneNotSupportedException x)
463 // This is impossible.
465 copy.buckets = new HashEntry[buckets.length];
466 copy.putAllInternal(this);
467 // Clear the entry cache. AbstractMap.clone() does the others.
473 * Returns a "set view" of this HashMap's keys. The set is backed by the
474 * HashMap, so changes in one show up in the other. The set supports
475 * element removal, but not element addition.
477 * @return a set view of the keys
484 // Create an AbstractSet with custom implementations of those methods
485 // that can be overridden easily and efficiently.
486 keys = new AbstractSet()
493 public Iterator iterator()
495 // Cannot create the iterator directly, because of LinkedHashMap.
496 return HashMap.this.iterator(KEYS);
501 HashMap.this.clear();
504 public boolean contains(Object o)
506 return containsKey(o);
509 public boolean remove(Object o)
511 // Test against the size of the HashMap to determine if anything
512 // really got removed. This is neccessary because the return value
513 // of HashMap.remove() is ambiguous in the null case.
515 HashMap.this.remove(o);
516 return oldsize != size;
523 * Returns a "collection view" (or "bag view") of this HashMap's values.
524 * The collection is backed by the HashMap, so changes in one show up
525 * in the other. The collection supports element removal, but not element
528 * @return a bag view of the values
532 public Collection values()
535 // We don't bother overriding many of the optional methods, as doing so
536 // wouldn't provide any significant performance advantage.
537 values = new AbstractCollection()
544 public Iterator iterator()
546 // Cannot create the iterator directly, because of LinkedHashMap.
547 return HashMap.this.iterator(VALUES);
552 HashMap.this.clear();
559 * Returns a "set view" of this HashMap's entries. The set is backed by
560 * the HashMap, so changes in one show up in the other. The set supports
561 * element removal, but not element addition.<p>
563 * Note that the iterators for all three views, from keySet(), entrySet(),
564 * and values(), traverse the HashMap in the same sequence.
566 * @return a set view of the entries
571 public Set entrySet()
574 // Create an AbstractSet with custom implementations of those methods
575 // that can be overridden easily and efficiently.
576 entries = new AbstractSet()
583 public Iterator iterator()
585 // Cannot create the iterator directly, because of LinkedHashMap.
586 return HashMap.this.iterator(ENTRIES);
591 HashMap.this.clear();
594 public boolean contains(Object o)
596 return getEntry(o) != null;
599 public boolean remove(Object o)
601 HashEntry e = getEntry(o);
604 HashMap.this.remove(e.key);
614 * Helper method for put, that creates and adds a new Entry. This is
615 * overridden in LinkedHashMap for bookkeeping purposes.
617 * @param key the key of the new Entry
618 * @param value the value
619 * @param idx the index in buckets where the new Entry belongs
620 * @param callRemove whether to call the removeEldestEntry method
621 * @see #put(Object, Object)
623 void addEntry(Object key, Object value, int idx, boolean callRemove)
625 HashEntry e = new HashEntry(key, value);
627 e.next = buckets[idx];
632 * Helper method for entrySet(), which matches both key and value
635 * @param o the entry to match
636 * @return the matching entry, if found, or null
639 private HashEntry getEntry(Object o)
641 if (!(o instanceof Map.Entry))
643 Map.Entry me = (Map.Entry) o;
644 int idx = hash(me.getKey());
645 HashEntry e = buckets[idx];
656 * Helper method that returns an index in the buckets array for `key'
657 * based on its hashCode(). Package visible for use by subclasses.
660 * @return the bucket number
662 final int hash(Object key)
664 return key == null ? 0 : Math.abs(key.hashCode() % buckets.length);
668 * Generates a parameterized iterator. Must be overrideable, since
669 * LinkedHashMap iterates in a different order.
671 * @param type {@link #KEYS}, {@link #VALUES}, or {@link #ENTRIES}
672 * @return the appropriate iterator
674 Iterator iterator(int type)
676 return new HashIterator(type);
680 * A simplified, more efficient internal implementation of putAll(). The
681 * Map constructor and clone() should not call putAll or put, in order to
682 * be compatible with the JDK implementation with respect to subclasses.
684 * @param m the map to initialize this from
686 void putAllInternal(Map m)
688 Iterator itr = m.entrySet().iterator();
689 int msize = m.size();
692 for (; msize > 0; msize--)
694 Map.Entry e = (Map.Entry) itr.next();
695 Object key = e.getKey();
697 addEntry(key, e.getValue(), idx, false);
702 * Increases the size of the HashMap and rehashes all keys to new array
703 * indices; this is called when the addition of a new value would cause
704 * size() > threshold. Note that the existing Entry objects are reused in
705 * the new hash table.
708 * This is not specified, but the new size is twice the current size plus
709 * one; this number is not always prime, unfortunately.
711 private void rehash()
713 HashEntry[] oldBuckets = buckets;
715 int newcapacity = (buckets.length * 2) + 1;
716 threshold = (int) (newcapacity * loadFactor);
717 buckets = new HashEntry[newcapacity];
719 for (int i = oldBuckets.length - 1; i >= 0; i--)
721 HashEntry e = oldBuckets[i];
724 int idx = hash(e.key);
725 HashEntry dest = buckets[idx];
729 while (dest.next != null)
738 HashEntry next = e.next;
746 * Serializes this object to the given stream.
748 * @param s the stream to write to
749 * @throws IOException if the underlying stream fails
750 * @serialData the <i>capacity</i>(int) that is the length of the
751 * bucket array, the <i>size</i>(int) of the hash map
752 * are emitted first. They are followed by size entries,
753 * each consisting of a key (Object) and a value (Object).
755 private void writeObject(ObjectOutputStream s) throws IOException
757 // Write the threshold and loadFactor fields.
758 s.defaultWriteObject();
760 s.writeInt(buckets.length);
762 // Avoid creating a wasted Set by creating the iterator directly.
763 Iterator it = iterator(ENTRIES);
766 HashEntry entry = (HashEntry) it.next();
767 s.writeObject(entry.key);
768 s.writeObject(entry.value);
773 * Deserializes this object from the given stream.
775 * @param s the stream to read from
776 * @throws ClassNotFoundException if the underlying stream fails
777 * @throws IOException if the underlying stream fails
778 * @serialData the <i>capacity</i>(int) that is the length of the
779 * bucket array, the <i>size</i>(int) of the hash map
780 * are emitted first. They are followed by size entries,
781 * each consisting of a key (Object) and a value (Object).
783 private void readObject(ObjectInputStream s)
784 throws IOException, ClassNotFoundException
786 // Read the threshold and loadFactor fields.
787 s.defaultReadObject();
789 // Read and use capacity.
790 buckets = new HashEntry[s.readInt()];
791 int len = s.readInt();
793 // Read and use key/value pairs.
794 for ( ; len > 0; len--)
795 put(s.readObject(), s.readObject());
799 * Iterate over HashMap's entries.
800 * This implementation is parameterized to give a sequential view of
801 * keys, values, or entries.
803 * @author Jon Zeppieri
805 private final class HashIterator implements Iterator
808 * The type of this Iterator: {@link #KEYS}, {@link #VALUES},
809 * or {@link #ENTRIES}.
811 private final int type;
813 * The number of modifications to the backing HashMap that we know about.
815 private int knownMod = modCount;
816 /** The number of elements remaining to be returned by next(). */
817 private int count = size;
818 /** Current index in the physical hash table. */
819 private int idx = buckets.length;
820 /** The last Entry returned by a next() call. */
821 private HashEntry last;
823 * The next entry that should be returned by next(). It is set to something
824 * if we're iterating through a bucket that contains multiple linked
825 * entries. It is null if next() needs to find a new bucket.
827 private HashEntry next;
830 * Construct a new HashIterator with the supplied type.
831 * @param type {@link #KEYS}, {@link #VALUES}, or {@link #ENTRIES}
833 HashIterator(int type)
839 * Returns true if the Iterator has more elements.
840 * @return true if there are more elements
841 * @throws ConcurrentModificationException if the HashMap was modified
843 public boolean hasNext()
845 if (knownMod != modCount)
846 throw new ConcurrentModificationException();
851 * Returns the next element in the Iterator's sequential view.
852 * @return the next element
853 * @throws ConcurrentModificationException if the HashMap was modified
854 * @throws NoSuchElementException if there is none
858 if (knownMod != modCount)
859 throw new ConcurrentModificationException();
861 throw new NoSuchElementException();
878 * Removes from the backing HashMap the last element which was fetched
879 * with the <code>next()</code> method.
880 * @throws ConcurrentModificationException if the HashMap was modified
881 * @throws IllegalStateException if called when there is no last element
885 if (knownMod != modCount)
886 throw new ConcurrentModificationException();
888 throw new IllegalStateException();
890 HashMap.this.remove(last.key);