Locale.getDefault() reads real value from navigator.language & co.
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
30 * Hash table based implementation of the <tt>Map</tt> interface. This
31 * implementation provides all of the optional map operations, and permits
32 * <tt>null</tt> values and the <tt>null</tt> key. (The <tt>HashMap</tt>
33 * class is roughly equivalent to <tt>Hashtable</tt>, except that it is
34 * unsynchronized and permits nulls.) This class makes no guarantees as to
35 * the order of the map; in particular, it does not guarantee that the order
36 * will remain constant over time.
38 * <p>This implementation provides constant-time performance for the basic
39 * operations (<tt>get</tt> and <tt>put</tt>), assuming the hash function
40 * disperses the elements properly among the buckets. Iteration over
41 * collection views requires time proportional to the "capacity" of the
42 * <tt>HashMap</tt> instance (the number of buckets) plus its size (the number
43 * of key-value mappings). Thus, it's very important not to set the initial
44 * capacity too high (or the load factor too low) if iteration performance is
47 * <p>An instance of <tt>HashMap</tt> has two parameters that affect its
48 * performance: <i>initial capacity</i> and <i>load factor</i>. The
49 * <i>capacity</i> is the number of buckets in the hash table, and the initial
50 * capacity is simply the capacity at the time the hash table is created. The
51 * <i>load factor</i> is a measure of how full the hash table is allowed to
52 * get before its capacity is automatically increased. When the number of
53 * entries in the hash table exceeds the product of the load factor and the
54 * current capacity, the hash table is <i>rehashed</i> (that is, internal data
55 * structures are rebuilt) so that the hash table has approximately twice the
58 * <p>As a general rule, the default load factor (.75) offers a good tradeoff
59 * between time and space costs. Higher values decrease the space overhead
60 * but increase the lookup cost (reflected in most of the operations of the
61 * <tt>HashMap</tt> class, including <tt>get</tt> and <tt>put</tt>). The
62 * expected number of entries in the map and its load factor should be taken
63 * into account when setting its initial capacity, so as to minimize the
64 * number of rehash operations. If the initial capacity is greater
65 * than the maximum number of entries divided by the load factor, no
66 * rehash operations will ever occur.
68 * <p>If many mappings are to be stored in a <tt>HashMap</tt> instance,
69 * creating it with a sufficiently large capacity will allow the mappings to
70 * be stored more efficiently than letting it perform automatic rehashing as
71 * needed to grow the table.
73 * <p><strong>Note that this implementation is not synchronized.</strong>
74 * If multiple threads access a hash map concurrently, and at least one of
75 * the threads modifies the map structurally, it <i>must</i> be
76 * synchronized externally. (A structural modification is any operation
77 * that adds or deletes one or more mappings; merely changing the value
78 * associated with a key that an instance already contains is not a
79 * structural modification.) This is typically accomplished by
80 * synchronizing on some object that naturally encapsulates the map.
82 * If no such object exists, the map should be "wrapped" using the
83 * {@link Collections#synchronizedMap Collections.synchronizedMap}
84 * method. This is best done at creation time, to prevent accidental
85 * unsynchronized access to the map:<pre>
86 * Map m = Collections.synchronizedMap(new HashMap(...));</pre>
88 * <p>The iterators returned by all of this class's "collection view methods"
89 * are <i>fail-fast</i>: if the map is structurally modified at any time after
90 * the iterator is created, in any way except through the iterator's own
91 * <tt>remove</tt> method, the iterator will throw a
92 * {@link ConcurrentModificationException}. Thus, in the face of concurrent
93 * modification, the iterator fails quickly and cleanly, rather than risking
94 * arbitrary, non-deterministic behavior at an undetermined time in the
97 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
98 * as it is, generally speaking, impossible to make any hard guarantees in the
99 * presence of unsynchronized concurrent modification. Fail-fast iterators
100 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
101 * Therefore, it would be wrong to write a program that depended on this
102 * exception for its correctness: <i>the fail-fast behavior of iterators
103 * should be used only to detect bugs.</i>
105 * <p>This class is a member of the
106 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
107 * Java Collections Framework</a>.
109 * @param <K> the type of keys maintained by this map
110 * @param <V> the type of mapped values
114 * @author Arthur van Hoff
115 * @author Neal Gafter
116 * @see Object#hashCode()
124 public class HashMap<K,V>
125 extends AbstractMap<K,V>
126 implements Map<K,V>, Cloneable, Serializable
130 * The default initial capacity - MUST be a power of two.
132 static final int DEFAULT_INITIAL_CAPACITY = 16;
135 * The maximum capacity, used if a higher value is implicitly specified
136 * by either of the constructors with arguments.
137 * MUST be a power of two <= 1<<30.
139 static final int MAXIMUM_CAPACITY = 1 << 30;
142 * The load factor used when none specified in constructor.
144 static final float DEFAULT_LOAD_FACTOR = 0.75f;
147 * The table, resized as necessary. Length MUST Always be a power of two.
149 transient Entry[] table;
152 * The number of key-value mappings contained in this map.
157 * The next size value at which to resize (capacity * load factor).
163 * The load factor for the hash table.
167 final float loadFactor;
170 * The number of times this HashMap has been structurally modified
171 * Structural modifications are those that change the number of mappings in
172 * the HashMap or otherwise modify its internal structure (e.g.,
173 * rehash). This field is used to make iterators on Collection-views of
174 * the HashMap fail-fast. (See ConcurrentModificationException).
176 transient int modCount;
179 * Constructs an empty <tt>HashMap</tt> with the specified initial
180 * capacity and load factor.
182 * @param initialCapacity the initial capacity
183 * @param loadFactor the load factor
184 * @throws IllegalArgumentException if the initial capacity is negative
185 * or the load factor is nonpositive
187 public HashMap(int initialCapacity, float loadFactor) {
188 if (initialCapacity < 0)
189 throw new IllegalArgumentException("Illegal initial capacity: " +
191 if (initialCapacity > MAXIMUM_CAPACITY)
192 initialCapacity = MAXIMUM_CAPACITY;
193 if (loadFactor <= 0 || Float.isNaN(loadFactor))
194 throw new IllegalArgumentException("Illegal load factor: " +
197 // Find a power of 2 >= initialCapacity
199 while (capacity < initialCapacity)
202 this.loadFactor = loadFactor;
203 threshold = (int)(capacity * loadFactor);
204 table = new Entry[capacity];
209 * Constructs an empty <tt>HashMap</tt> with the specified initial
210 * capacity and the default load factor (0.75).
212 * @param initialCapacity the initial capacity.
213 * @throws IllegalArgumentException if the initial capacity is negative.
215 public HashMap(int initialCapacity) {
216 this(initialCapacity, DEFAULT_LOAD_FACTOR);
220 * Constructs an empty <tt>HashMap</tt> with the default initial capacity
221 * (16) and the default load factor (0.75).
224 this.loadFactor = DEFAULT_LOAD_FACTOR;
225 threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
226 table = new Entry[DEFAULT_INITIAL_CAPACITY];
231 * Constructs a new <tt>HashMap</tt> with the same mappings as the
232 * specified <tt>Map</tt>. The <tt>HashMap</tt> is created with
233 * default load factor (0.75) and an initial capacity sufficient to
234 * hold the mappings in the specified <tt>Map</tt>.
236 * @param m the map whose mappings are to be placed in this map
237 * @throws NullPointerException if the specified map is null
239 public HashMap(Map<? extends K, ? extends V> m) {
240 this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
241 DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
245 // internal utilities
248 * Initialization hook for subclasses. This method is called
249 * in all constructors and pseudo-constructors (clone, readObject)
250 * after HashMap has been initialized but before any entries have
251 * been inserted. (In the absence of this method, readObject would
252 * require explicit knowledge of subclasses.)
258 * Applies a supplemental hash function to a given hashCode, which
259 * defends against poor quality hash functions. This is critical
260 * because HashMap uses power-of-two length hash tables, that
261 * otherwise encounter collisions for hashCodes that do not differ
262 * in lower bits. Note: Null keys always map to hash 0, thus index 0.
264 static int hash(int h) {
265 // This function ensures that hashCodes that differ only by
266 // constant multiples at each bit position have a bounded
267 // number of collisions (approximately 8 at default load factor).
268 h ^= (h >>> 20) ^ (h >>> 12);
269 return h ^ (h >>> 7) ^ (h >>> 4);
273 * Returns index for hash code h.
275 static int indexFor(int h, int length) {
276 return h & (length-1);
280 * Returns the number of key-value mappings in this map.
282 * @return the number of key-value mappings in this map
289 * Returns <tt>true</tt> if this map contains no key-value mappings.
291 * @return <tt>true</tt> if this map contains no key-value mappings
293 public boolean isEmpty() {
298 * Returns the value to which the specified key is mapped,
299 * or {@code null} if this map contains no mapping for the key.
301 * <p>More formally, if this map contains a mapping from a key
302 * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
303 * key.equals(k))}, then this method returns {@code v}; otherwise
304 * it returns {@code null}. (There can be at most one such mapping.)
306 * <p>A return value of {@code null} does not <i>necessarily</i>
307 * indicate that the map contains no mapping for the key; it's also
308 * possible that the map explicitly maps the key to {@code null}.
309 * The {@link #containsKey containsKey} operation may be used to
310 * distinguish these two cases.
312 * @see #put(Object, Object)
314 public V get(Object key) {
316 return getForNullKey();
317 int hash = hash(key.hashCode());
318 for (Entry<K,V> e = table[indexFor(hash, table.length)];
322 if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
329 * Offloaded version of get() to look up null keys. Null keys map
330 * to index 0. This null case is split out into separate methods
331 * for the sake of performance in the two most commonly used
332 * operations (get and put), but incorporated with conditionals in
335 private V getForNullKey() {
336 for (Entry<K,V> e = table[0]; e != null; e = e.next) {
344 * Returns <tt>true</tt> if this map contains a mapping for the
347 * @param key The key whose presence in this map is to be tested
348 * @return <tt>true</tt> if this map contains a mapping for the specified
351 public boolean containsKey(Object key) {
352 return getEntry(key) != null;
356 * Returns the entry associated with the specified key in the
357 * HashMap. Returns null if the HashMap contains no mapping
360 final Entry<K,V> getEntry(Object key) {
361 int hash = (key == null) ? 0 : hash(key.hashCode());
362 for (Entry<K,V> e = table[indexFor(hash, table.length)];
366 if (e.hash == hash &&
367 ((k = e.key) == key || (key != null && key.equals(k))))
375 * Associates the specified value with the specified key in this map.
376 * If the map previously contained a mapping for the key, the old
379 * @param key key with which the specified value is to be associated
380 * @param value value to be associated with the specified key
381 * @return the previous value associated with <tt>key</tt>, or
382 * <tt>null</tt> if there was no mapping for <tt>key</tt>.
383 * (A <tt>null</tt> return can also indicate that the map
384 * previously associated <tt>null</tt> with <tt>key</tt>.)
386 public V put(K key, V value) {
388 return putForNullKey(value);
389 int hash = hash(key.hashCode());
390 int i = indexFor(hash, table.length);
391 for (Entry<K,V> e = table[i]; e != null; e = e.next) {
393 if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
394 V oldValue = e.value;
396 e.recordAccess(this);
402 addEntry(hash, key, value, i);
407 * Offloaded version of put for null keys
409 private V putForNullKey(V value) {
410 for (Entry<K,V> e = table[0]; e != null; e = e.next) {
412 V oldValue = e.value;
414 e.recordAccess(this);
419 addEntry(0, null, value, 0);
424 * This method is used instead of put by constructors and
425 * pseudoconstructors (clone, readObject). It does not resize the table,
426 * check for comodification, etc. It calls createEntry rather than
429 private void putForCreate(K key, V value) {
430 int hash = (key == null) ? 0 : hash(key.hashCode());
431 int i = indexFor(hash, table.length);
434 * Look for preexisting entry for key. This will never happen for
435 * clone or deserialize. It will only happen for construction if the
436 * input Map is a sorted map whose ordering is inconsistent w/ equals.
438 for (Entry<K,V> e = table[i]; e != null; e = e.next) {
440 if (e.hash == hash &&
441 ((k = e.key) == key || (key != null && key.equals(k)))) {
447 createEntry(hash, key, value, i);
450 private void putAllForCreate(Map<? extends K, ? extends V> m) {
451 for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
452 putForCreate(e.getKey(), e.getValue());
456 * Rehashes the contents of this map into a new array with a
457 * larger capacity. This method is called automatically when the
458 * number of keys in this map reaches its threshold.
460 * If current capacity is MAXIMUM_CAPACITY, this method does not
461 * resize the map, but sets threshold to Integer.MAX_VALUE.
462 * This has the effect of preventing future calls.
464 * @param newCapacity the new capacity, MUST be a power of two;
465 * must be greater than current capacity unless current
466 * capacity is MAXIMUM_CAPACITY (in which case value
469 void resize(int newCapacity) {
470 Entry[] oldTable = table;
471 int oldCapacity = oldTable.length;
472 if (oldCapacity == MAXIMUM_CAPACITY) {
473 threshold = Integer.MAX_VALUE;
477 Entry[] newTable = new Entry[newCapacity];
480 threshold = (int)(newCapacity * loadFactor);
484 * Transfers all entries from current table to newTable.
486 void transfer(Entry[] newTable) {
488 int newCapacity = newTable.length;
489 for (int j = 0; j < src.length; j++) {
490 Entry<K,V> e = src[j];
494 Entry<K,V> next = e.next;
495 int i = indexFor(e.hash, newCapacity);
496 e.next = newTable[i];
505 * Copies all of the mappings from the specified map to this map.
506 * These mappings will replace any mappings that this map had for
507 * any of the keys currently in the specified map.
509 * @param m mappings to be stored in this map
510 * @throws NullPointerException if the specified map is null
512 public void putAll(Map<? extends K, ? extends V> m) {
513 int numKeysToBeAdded = m.size();
514 if (numKeysToBeAdded == 0)
518 * Expand the map if the map if the number of mappings to be added
519 * is greater than or equal to threshold. This is conservative; the
520 * obvious condition is (m.size() + size) >= threshold, but this
521 * condition could result in a map with twice the appropriate capacity,
522 * if the keys to be added overlap with the keys already in this map.
523 * By using the conservative calculation, we subject ourself
524 * to at most one extra resize.
526 if (numKeysToBeAdded > threshold) {
527 int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
528 if (targetCapacity > MAXIMUM_CAPACITY)
529 targetCapacity = MAXIMUM_CAPACITY;
530 int newCapacity = table.length;
531 while (newCapacity < targetCapacity)
533 if (newCapacity > table.length)
537 for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
538 put(e.getKey(), e.getValue());
542 * Removes the mapping for the specified key from this map if present.
544 * @param key key whose mapping is to be removed from the map
545 * @return the previous value associated with <tt>key</tt>, or
546 * <tt>null</tt> if there was no mapping for <tt>key</tt>.
547 * (A <tt>null</tt> return can also indicate that the map
548 * previously associated <tt>null</tt> with <tt>key</tt>.)
550 public V remove(Object key) {
551 Entry<K,V> e = removeEntryForKey(key);
552 return (e == null ? null : e.value);
556 * Removes and returns the entry associated with the specified key
557 * in the HashMap. Returns null if the HashMap contains no mapping
560 final Entry<K,V> removeEntryForKey(Object key) {
561 int hash = (key == null) ? 0 : hash(key.hashCode());
562 int i = indexFor(hash, table.length);
563 Entry<K,V> prev = table[i];
567 Entry<K,V> next = e.next;
569 if (e.hash == hash &&
570 ((k = e.key) == key || (key != null && key.equals(k)))) {
577 e.recordRemoval(this);
588 * Special version of remove for EntrySet.
590 final Entry<K,V> removeMapping(Object o) {
591 if (!(o instanceof Map.Entry))
594 Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
595 Object key = entry.getKey();
596 int hash = (key == null) ? 0 : hash(key.hashCode());
597 int i = indexFor(hash, table.length);
598 Entry<K,V> prev = table[i];
602 Entry<K,V> next = e.next;
603 if (e.hash == hash && e.equals(entry)) {
610 e.recordRemoval(this);
621 * Removes all of the mappings from this map.
622 * The map will be empty after this call returns.
624 public void clear() {
627 for (int i = 0; i < tab.length; i++)
633 * Returns <tt>true</tt> if this map maps one or more keys to the
636 * @param value value whose presence in this map is to be tested
637 * @return <tt>true</tt> if this map maps one or more keys to the
640 public boolean containsValue(Object value) {
642 return containsNullValue();
645 for (int i = 0; i < tab.length ; i++)
646 for (Entry e = tab[i] ; e != null ; e = e.next)
647 if (value.equals(e.value))
653 * Special-case code for containsValue with null argument
655 private boolean containsNullValue() {
657 for (int i = 0; i < tab.length ; i++)
658 for (Entry e = tab[i] ; e != null ; e = e.next)
665 * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and
666 * values themselves are not cloned.
668 * @return a shallow copy of this map
670 public Object clone() {
671 HashMap<K,V> result = null;
673 result = (HashMap<K,V>)super.clone();
674 } catch (CloneNotSupportedException e) {
677 result.table = new Entry[table.length];
678 result.entrySet = null;
682 result.putAllForCreate(this);
687 static class Entry<K,V> implements Map.Entry<K,V> {
696 Entry(int h, K k, V v, Entry<K,V> n) {
703 public final K getKey() {
707 public final V getValue() {
711 public final V setValue(V newValue) {
717 public final boolean equals(Object o) {
718 if (!(o instanceof Map.Entry))
720 Map.Entry e = (Map.Entry)o;
721 Object k1 = getKey();
722 Object k2 = e.getKey();
723 if (k1 == k2 || (k1 != null && k1.equals(k2))) {
724 Object v1 = getValue();
725 Object v2 = e.getValue();
726 if (v1 == v2 || (v1 != null && v1.equals(v2)))
732 public final int hashCode() {
733 return (key==null ? 0 : key.hashCode()) ^
734 (value==null ? 0 : value.hashCode());
737 public final String toString() {
738 return getKey() + "=" + getValue();
742 * This method is invoked whenever the value in an entry is
743 * overwritten by an invocation of put(k,v) for a key k that's already
746 void recordAccess(HashMap<K,V> m) {
750 * This method is invoked whenever the entry is
751 * removed from the table.
753 void recordRemoval(HashMap<K,V> m) {
758 * Adds a new entry with the specified key, value and hash code to
759 * the specified bucket. It is the responsibility of this
760 * method to resize the table if appropriate.
762 * Subclass overrides this to alter the behavior of put method.
764 void addEntry(int hash, K key, V value, int bucketIndex) {
765 Entry<K,V> e = table[bucketIndex];
766 table[bucketIndex] = new Entry<>(hash, key, value, e);
767 if (size++ >= threshold)
768 resize(2 * table.length);
772 * Like addEntry except that this version is used when creating entries
773 * as part of Map construction or "pseudo-construction" (cloning,
774 * deserialization). This version needn't worry about resizing the table.
776 * Subclass overrides this to alter the behavior of HashMap(Map),
777 * clone, and readObject.
779 void createEntry(int hash, K key, V value, int bucketIndex) {
780 Entry<K,V> e = table[bucketIndex];
781 table[bucketIndex] = new Entry<>(hash, key, value, e);
785 private abstract class HashIterator<E> implements Iterator<E> {
786 Entry<K,V> next; // next entry to return
787 int expectedModCount; // For fast-fail
788 int index; // current slot
789 Entry<K,V> current; // current entry
792 expectedModCount = modCount;
793 if (size > 0) { // advance to first entry
795 while (index < t.length && (next = t[index++]) == null)
800 public final boolean hasNext() {
804 final Entry<K,V> nextEntry() {
805 if (modCount != expectedModCount)
806 throw new ConcurrentModificationException();
809 throw new NoSuchElementException();
811 if ((next = e.next) == null) {
813 while (index < t.length && (next = t[index++]) == null)
820 public void remove() {
822 throw new IllegalStateException();
823 if (modCount != expectedModCount)
824 throw new ConcurrentModificationException();
825 Object k = current.key;
827 HashMap.this.removeEntryForKey(k);
828 expectedModCount = modCount;
833 private final class ValueIterator extends HashIterator<V> {
835 return nextEntry().value;
839 private final class KeyIterator extends HashIterator<K> {
841 return nextEntry().getKey();
845 private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
846 public Map.Entry<K,V> next() {
851 // Subclass overrides these to alter behavior of views' iterator() method
852 Iterator<K> newKeyIterator() {
853 return new KeyIterator();
855 Iterator<V> newValueIterator() {
856 return new ValueIterator();
858 Iterator<Map.Entry<K,V>> newEntryIterator() {
859 return new EntryIterator();
865 private transient Set<Map.Entry<K,V>> entrySet = null;
868 * Returns a {@link Set} view of the keys contained in this map.
869 * The set is backed by the map, so changes to the map are
870 * reflected in the set, and vice-versa. If the map is modified
871 * while an iteration over the set is in progress (except through
872 * the iterator's own <tt>remove</tt> operation), the results of
873 * the iteration are undefined. The set supports element removal,
874 * which removes the corresponding mapping from the map, via the
875 * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
876 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
877 * operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
880 public Set<K> keySet() {
882 return (ks != null ? ks : (keySet = new KeySet()));
885 private final class KeySet extends AbstractSet<K> {
886 public Iterator<K> iterator() {
887 return newKeyIterator();
892 public boolean contains(Object o) {
893 return containsKey(o);
895 public boolean remove(Object o) {
896 return HashMap.this.removeEntryForKey(o) != null;
898 public void clear() {
899 HashMap.this.clear();
904 * Returns a {@link Collection} view of the values contained in this map.
905 * The collection is backed by the map, so changes to the map are
906 * reflected in the collection, and vice-versa. If the map is
907 * modified while an iteration over the collection is in progress
908 * (except through the iterator's own <tt>remove</tt> operation),
909 * the results of the iteration are undefined. The collection
910 * supports element removal, which removes the corresponding
911 * mapping from the map, via the <tt>Iterator.remove</tt>,
912 * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
913 * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not
914 * support the <tt>add</tt> or <tt>addAll</tt> operations.
916 public Collection<V> values() {
917 Collection<V> vs = values;
918 return (vs != null ? vs : (values = new Values()));
921 private final class Values extends AbstractCollection<V> {
922 public Iterator<V> iterator() {
923 return newValueIterator();
928 public boolean contains(Object o) {
929 return containsValue(o);
931 public void clear() {
932 HashMap.this.clear();
937 * Returns a {@link Set} view of the mappings contained in this map.
938 * The set is backed by the map, so changes to the map are
939 * reflected in the set, and vice-versa. If the map is modified
940 * while an iteration over the set is in progress (except through
941 * the iterator's own <tt>remove</tt> operation, or through the
942 * <tt>setValue</tt> operation on a map entry returned by the
943 * iterator) the results of the iteration are undefined. The set
944 * supports element removal, which removes the corresponding
945 * mapping from the map, via the <tt>Iterator.remove</tt>,
946 * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
947 * <tt>clear</tt> operations. It does not support the
948 * <tt>add</tt> or <tt>addAll</tt> operations.
950 * @return a set view of the mappings contained in this map
952 public Set<Map.Entry<K,V>> entrySet() {
956 private Set<Map.Entry<K,V>> entrySet0() {
957 Set<Map.Entry<K,V>> es = entrySet;
958 return es != null ? es : (entrySet = new EntrySet());
961 private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
962 public Iterator<Map.Entry<K,V>> iterator() {
963 return newEntryIterator();
965 public boolean contains(Object o) {
966 if (!(o instanceof Map.Entry))
968 Map.Entry<K,V> e = (Map.Entry<K,V>) o;
969 Entry<K,V> candidate = getEntry(e.getKey());
970 return candidate != null && candidate.equals(e);
972 public boolean remove(Object o) {
973 return removeMapping(o) != null;
978 public void clear() {
979 HashMap.this.clear();
984 private static final long serialVersionUID = 362498820763181265L;
987 // These methods are used when serializing HashSets
988 int capacity() { return table.length; }
989 float loadFactor() { return loadFactor; }