Don't obfuscate names of fields in objects - otherwise fields provided by two modules may clash
2 * Copyright (c) 1994, 2011, 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 * This class implements a hash table, which maps keys to values. Any
31 * non-<code>null</code> object can be used as a key or as a value. <p>
33 * To successfully store and retrieve objects from a hashtable, the
34 * objects used as keys must implement the <code>hashCode</code>
35 * method and the <code>equals</code> method. <p>
37 * An instance of <code>Hashtable</code> has two parameters that affect its
38 * performance: <i>initial capacity</i> and <i>load factor</i>. The
39 * <i>capacity</i> is the number of <i>buckets</i> in the hash table, and the
40 * <i>initial capacity</i> is simply the capacity at the time the hash table
41 * is created. Note that the hash table is <i>open</i>: in the case of a "hash
42 * collision", a single bucket stores multiple entries, which must be searched
43 * sequentially. The <i>load factor</i> is a measure of how full the hash
44 * table is allowed to get before its capacity is automatically increased.
45 * The initial capacity and load factor parameters are merely hints to
46 * the implementation. The exact details as to when and whether the rehash
47 * method is invoked are implementation-dependent.<p>
49 * Generally, the default load factor (.75) offers a good tradeoff between
50 * time and space costs. Higher values decrease the space overhead but
51 * increase the time cost to look up an entry (which is reflected in most
52 * <tt>Hashtable</tt> operations, including <tt>get</tt> and <tt>put</tt>).<p>
54 * The initial capacity controls a tradeoff between wasted space and the
55 * need for <code>rehash</code> operations, which are time-consuming.
56 * No <code>rehash</code> operations will <i>ever</i> occur if the initial
57 * capacity is greater than the maximum number of entries the
58 * <tt>Hashtable</tt> will contain divided by its load factor. However,
59 * setting the initial capacity too high can waste space.<p>
61 * If many entries are to be made into a <code>Hashtable</code>,
62 * creating it with a sufficiently large capacity may allow the
63 * entries to be inserted more efficiently than letting it perform
64 * automatic rehashing as needed to grow the table. <p>
66 * This example creates a hashtable of numbers. It uses the names of
67 * the numbers as keys:
69 * Hashtable<String, Integer> numbers
70 * = new Hashtable<String, Integer>();
71 * numbers.put("one", 1);
72 * numbers.put("two", 2);
73 * numbers.put("three", 3);}</pre>
75 * <p>To retrieve a number, use the following code:
77 * Integer n = numbers.get("two");
79 * System.out.println("two = " + n);
82 * <p>The iterators returned by the <tt>iterator</tt> method of the collections
83 * returned by all of this class's "collection view methods" are
84 * <em>fail-fast</em>: if the Hashtable is structurally modified at any time
85 * after the iterator is created, in any way except through the iterator's own
86 * <tt>remove</tt> method, the iterator will throw a {@link
87 * ConcurrentModificationException}. Thus, in the face of concurrent
88 * modification, the iterator fails quickly and cleanly, rather than risking
89 * arbitrary, non-deterministic behavior at an undetermined time in the future.
90 * The Enumerations returned by Hashtable's keys and elements methods are
91 * <em>not</em> fail-fast.
93 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
94 * as it is, generally speaking, impossible to make any hard guarantees in the
95 * presence of unsynchronized concurrent modification. Fail-fast iterators
96 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
97 * Therefore, it would be wrong to write a program that depended on this
98 * exception for its correctness: <i>the fail-fast behavior of iterators
99 * should be used only to detect bugs.</i>
101 * <p>As of the Java 2 platform v1.2, this class was retrofitted to
102 * implement the {@link Map} interface, making it a member of the
103 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
105 * Java Collections Framework</a>. Unlike the new collection
106 * implementations, {@code Hashtable} is synchronized. If a
107 * thread-safe implementation is not needed, it is recommended to use
108 * {@link HashMap} in place of {@code Hashtable}. If a thread-safe
109 * highly-concurrent implementation is desired, then it is recommended
110 * to use {@link java.util.concurrent.ConcurrentHashMap} in place of
113 * @author Arthur van Hoff
115 * @author Neal Gafter
116 * @see Object#equals(java.lang.Object)
117 * @see Object#hashCode()
118 * @see Hashtable#rehash()
125 public class Hashtable<K,V>
126 extends Dictionary<K,V>
127 implements Map<K,V>, Cloneable, java.io.Serializable {
130 * The hash table data.
132 private transient Entry[] table;
135 * The total number of entries in the hash table.
137 private transient int count;
140 * The table is rehashed when its size exceeds this threshold. (The
141 * value of this field is (int)(capacity * loadFactor).)
145 private int threshold;
148 * The load factor for the hashtable.
152 private float loadFactor;
155 * The number of times this Hashtable has been structurally modified
156 * Structural modifications are those that change the number of entries in
157 * the Hashtable or otherwise modify its internal structure (e.g.,
158 * rehash). This field is used to make iterators on Collection-views of
159 * the Hashtable fail-fast. (See ConcurrentModificationException).
161 private transient int modCount = 0;
163 /** use serialVersionUID from JDK 1.0.2 for interoperability */
164 private static final long serialVersionUID = 1421746759512286392L;
167 * Constructs a new, empty hashtable with the specified initial
168 * capacity and the specified load factor.
170 * @param initialCapacity the initial capacity of the hashtable.
171 * @param loadFactor the load factor of the hashtable.
172 * @exception IllegalArgumentException if the initial capacity is less
173 * than zero, or if the load factor is nonpositive.
175 public Hashtable(int initialCapacity, float loadFactor) {
176 if (initialCapacity < 0)
177 throw new IllegalArgumentException("Illegal Capacity: "+
179 if (loadFactor <= 0 || Float.isNaN(loadFactor))
180 throw new IllegalArgumentException("Illegal Load: "+loadFactor);
182 if (initialCapacity==0)
184 this.loadFactor = loadFactor;
185 table = new Entry[initialCapacity];
186 threshold = (int)(initialCapacity * loadFactor);
190 * Constructs a new, empty hashtable with the specified initial capacity
191 * and default load factor (0.75).
193 * @param initialCapacity the initial capacity of the hashtable.
194 * @exception IllegalArgumentException if the initial capacity is less
197 public Hashtable(int initialCapacity) {
198 this(initialCapacity, 0.75f);
202 * Constructs a new, empty hashtable with a default initial capacity (11)
203 * and load factor (0.75).
210 * Constructs a new hashtable with the same mappings as the given
211 * Map. The hashtable is created with an initial capacity sufficient to
212 * hold the mappings in the given Map and a default load factor (0.75).
214 * @param t the map whose mappings are to be placed in this map.
215 * @throws NullPointerException if the specified map is null.
218 public Hashtable(Map<? extends K, ? extends V> t) {
219 this(Math.max(2*t.size(), 11), 0.75f);
224 * Returns the number of keys in this hashtable.
226 * @return the number of keys in this hashtable.
228 public synchronized int size() {
233 * Tests if this hashtable maps no keys to values.
235 * @return <code>true</code> if this hashtable maps no keys to values;
236 * <code>false</code> otherwise.
238 public synchronized boolean isEmpty() {
243 * Returns an enumeration of the keys in this hashtable.
245 * @return an enumeration of the keys in this hashtable.
251 public synchronized Enumeration<K> keys() {
252 return this.<K>getEnumeration(KEYS);
256 * Returns an enumeration of the values in this hashtable.
257 * Use the Enumeration methods on the returned object to fetch the elements
260 * @return an enumeration of the values in this hashtable.
261 * @see java.util.Enumeration
266 public synchronized Enumeration<V> elements() {
267 return this.<V>getEnumeration(VALUES);
271 * Tests if some key maps into the specified value in this hashtable.
272 * This operation is more expensive than the {@link #containsKey
273 * containsKey} method.
275 * <p>Note that this method is identical in functionality to
276 * {@link #containsValue containsValue}, (which is part of the
277 * {@link Map} interface in the collections framework).
279 * @param value a value to search for
280 * @return <code>true</code> if and only if some key maps to the
281 * <code>value</code> argument in this hashtable as
282 * determined by the <tt>equals</tt> method;
283 * <code>false</code> otherwise.
284 * @exception NullPointerException if the value is <code>null</code>
286 public synchronized boolean contains(Object value) {
288 throw new NullPointerException();
292 for (int i = tab.length ; i-- > 0 ;) {
293 for (Entry<K,V> e = tab[i] ; e != null ; e = e.next) {
294 if (e.value.equals(value)) {
303 * Returns true if this hashtable maps one or more keys to this value.
305 * <p>Note that this method is identical in functionality to {@link
306 * #contains contains} (which predates the {@link Map} interface).
308 * @param value value whose presence in this hashtable is to be tested
309 * @return <tt>true</tt> if this map maps one or more keys to the
311 * @throws NullPointerException if the value is <code>null</code>
314 public boolean containsValue(Object value) {
315 return contains(value);
319 * Tests if the specified object is a key in this hashtable.
321 * @param key possible key
322 * @return <code>true</code> if and only if the specified object
323 * is a key in this hashtable, as determined by the
324 * <tt>equals</tt> method; <code>false</code> otherwise.
325 * @throws NullPointerException if the key is <code>null</code>
326 * @see #contains(Object)
328 public synchronized boolean containsKey(Object key) {
330 int hash = key.hashCode();
331 int index = (hash & 0x7FFFFFFF) % tab.length;
332 for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
333 if ((e.hash == hash) && e.key.equals(key)) {
341 * Returns the value to which the specified key is mapped,
342 * or {@code null} if this map contains no mapping for the key.
344 * <p>More formally, if this map contains a mapping from a key
345 * {@code k} to a value {@code v} such that {@code (key.equals(k))},
346 * then this method returns {@code v}; otherwise it returns
347 * {@code null}. (There can be at most one such mapping.)
349 * @param key the key whose associated value is to be returned
350 * @return the value to which the specified key is mapped, or
351 * {@code null} if this map contains no mapping for the key
352 * @throws NullPointerException if the specified key is null
353 * @see #put(Object, Object)
355 public synchronized V get(Object key) {
357 int hash = key.hashCode();
358 int index = (hash & 0x7FFFFFFF) % tab.length;
359 for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
360 if ((e.hash == hash) && e.key.equals(key)) {
368 * The maximum size of array to allocate.
369 * Some VMs reserve some header words in an array.
370 * Attempts to allocate larger arrays may result in
371 * OutOfMemoryError: Requested array size exceeds VM limit
373 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
376 * Increases the capacity of and internally reorganizes this
377 * hashtable, in order to accommodate and access its entries more
378 * efficiently. This method is called automatically when the
379 * number of keys in the hashtable exceeds this hashtable's capacity
382 protected void rehash() {
383 int oldCapacity = table.length;
384 Entry[] oldMap = table;
386 // overflow-conscious code
387 int newCapacity = (oldCapacity << 1) + 1;
388 if (newCapacity - MAX_ARRAY_SIZE > 0) {
389 if (oldCapacity == MAX_ARRAY_SIZE)
390 // Keep running with MAX_ARRAY_SIZE buckets
392 newCapacity = MAX_ARRAY_SIZE;
394 Entry[] newMap = new Entry[newCapacity];
397 threshold = (int)(newCapacity * loadFactor);
400 for (int i = oldCapacity ; i-- > 0 ;) {
401 for (Entry<K,V> old = oldMap[i] ; old != null ; ) {
405 int index = (e.hash & 0x7FFFFFFF) % newCapacity;
406 e.next = newMap[index];
413 * Maps the specified <code>key</code> to the specified
414 * <code>value</code> in this hashtable. Neither the key nor the
415 * value can be <code>null</code>. <p>
417 * The value can be retrieved by calling the <code>get</code> method
418 * with a key that is equal to the original key.
420 * @param key the hashtable key
421 * @param value the value
422 * @return the previous value of the specified key in this hashtable,
423 * or <code>null</code> if it did not have one
424 * @exception NullPointerException if the key or value is
426 * @see Object#equals(Object)
429 public synchronized V put(K key, V value) {
430 // Make sure the value is not null
432 throw new NullPointerException();
435 // Makes sure the key is not already in the hashtable.
437 int hash = key.hashCode();
438 int index = (hash & 0x7FFFFFFF) % tab.length;
439 for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
440 if ((e.hash == hash) && e.key.equals(key)) {
448 if (count >= threshold) {
449 // Rehash the table if the threshold is exceeded
453 index = (hash & 0x7FFFFFFF) % tab.length;
456 // Creates the new entry.
457 Entry<K,V> e = tab[index];
458 tab[index] = new Entry<>(hash, key, value, e);
464 * Removes the key (and its corresponding value) from this
465 * hashtable. This method does nothing if the key is not in the hashtable.
467 * @param key the key that needs to be removed
468 * @return the value to which the key had been mapped in this hashtable,
469 * or <code>null</code> if the key did not have a mapping
470 * @throws NullPointerException if the key is <code>null</code>
472 public synchronized V remove(Object key) {
474 int hash = key.hashCode();
475 int index = (hash & 0x7FFFFFFF) % tab.length;
476 for (Entry<K,V> e = tab[index], prev = null ; e != null ; prev = e, e = e.next) {
477 if ((e.hash == hash) && e.key.equals(key)) {
485 V oldValue = e.value;
494 * Copies all of the mappings from the specified map to this hashtable.
495 * These mappings will replace any mappings that this hashtable had for any
496 * of the keys currently in the specified map.
498 * @param t mappings to be stored in this map
499 * @throws NullPointerException if the specified map is null
502 public synchronized void putAll(Map<? extends K, ? extends V> t) {
503 for (Map.Entry<? extends K, ? extends V> e : t.entrySet())
504 put(e.getKey(), e.getValue());
508 * Clears this hashtable so that it contains no keys.
510 public synchronized void clear() {
513 for (int index = tab.length; --index >= 0; )
519 * Creates a shallow copy of this hashtable. All the structure of the
520 * hashtable itself is copied, but the keys and values are not cloned.
521 * This is a relatively expensive operation.
523 * @return a clone of the hashtable
525 public synchronized Object clone() {
527 Hashtable<K,V> t = (Hashtable<K,V>) super.clone();
528 t.table = new Entry[table.length];
529 for (int i = table.length ; i-- > 0 ; ) {
530 t.table[i] = (table[i] != null)
531 ? (Entry<K,V>) table[i].clone() : null;
538 } catch (CloneNotSupportedException e) {
539 // this shouldn't happen, since we are Cloneable
540 throw new InternalError();
545 * Returns a string representation of this <tt>Hashtable</tt> object
546 * in the form of a set of entries, enclosed in braces and separated
547 * by the ASCII characters "<tt>, </tt>" (comma and space). Each
548 * entry is rendered as the key, an equals sign <tt>=</tt>, and the
549 * associated element, where the <tt>toString</tt> method is used to
550 * convert the key and element to strings.
552 * @return a string representation of this hashtable
554 public synchronized String toString() {
555 int max = size() - 1;
559 StringBuilder sb = new StringBuilder();
560 Iterator<Map.Entry<K,V>> it = entrySet().iterator();
563 for (int i = 0; ; i++) {
564 Map.Entry<K,V> e = it.next();
566 V value = e.getValue();
567 sb.append(key == this ? "(this Map)" : key.toString());
569 sb.append(value == this ? "(this Map)" : value.toString());
572 return sb.append('}').toString();
578 private <T> Enumeration<T> getEnumeration(int type) {
580 return Collections.emptyEnumeration();
582 return new Enumerator<>(type, false);
586 private <T> Iterator<T> getIterator(int type) {
588 return Collections.emptyIterator();
590 return new Enumerator<>(type, true);
597 * Each of these fields are initialized to contain an instance of the
598 * appropriate view the first time this view is requested. The views are
599 * stateless, so there's no reason to create more than one of each.
601 private transient volatile Set<K> keySet = null;
602 private transient volatile Set<Map.Entry<K,V>> entrySet = null;
603 private transient volatile Collection<V> values = null;
606 * Returns a {@link Set} view of the keys contained in this map.
607 * The set is backed by the map, so changes to the map are
608 * reflected in the set, and vice-versa. If the map is modified
609 * while an iteration over the set is in progress (except through
610 * the iterator's own <tt>remove</tt> operation), the results of
611 * the iteration are undefined. The set supports element removal,
612 * which removes the corresponding mapping from the map, via the
613 * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
614 * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
615 * operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
620 public Set<K> keySet() {
622 keySet = Collections.synchronizedSet(new KeySet(), this);
626 private class KeySet extends AbstractSet<K> {
627 public Iterator<K> iterator() {
628 return getIterator(KEYS);
633 public boolean contains(Object o) {
634 return containsKey(o);
636 public boolean remove(Object o) {
637 return Hashtable.this.remove(o) != null;
639 public void clear() {
640 Hashtable.this.clear();
645 * Returns a {@link Set} view of the mappings contained in this map.
646 * The set is backed by the map, so changes to the map are
647 * reflected in the set, and vice-versa. If the map is modified
648 * while an iteration over the set is in progress (except through
649 * the iterator's own <tt>remove</tt> operation, or through the
650 * <tt>setValue</tt> operation on a map entry returned by the
651 * iterator) the results of the iteration are undefined. The set
652 * supports element removal, which removes the corresponding
653 * mapping from the map, via the <tt>Iterator.remove</tt>,
654 * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
655 * <tt>clear</tt> operations. It does not support the
656 * <tt>add</tt> or <tt>addAll</tt> operations.
660 public Set<Map.Entry<K,V>> entrySet() {
662 entrySet = Collections.synchronizedSet(new EntrySet(), this);
666 private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
667 public Iterator<Map.Entry<K,V>> iterator() {
668 return getIterator(ENTRIES);
671 public boolean add(Map.Entry<K,V> o) {
675 public boolean contains(Object o) {
676 if (!(o instanceof Map.Entry))
678 Map.Entry entry = (Map.Entry)o;
679 Object key = entry.getKey();
681 int hash = key.hashCode();
682 int index = (hash & 0x7FFFFFFF) % tab.length;
684 for (Entry e = tab[index]; e != null; e = e.next)
685 if (e.hash==hash && e.equals(entry))
690 public boolean remove(Object o) {
691 if (!(o instanceof Map.Entry))
693 Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
694 K key = entry.getKey();
696 int hash = key.hashCode();
697 int index = (hash & 0x7FFFFFFF) % tab.length;
699 for (Entry<K,V> e = tab[index], prev = null; e != null;
700 prev = e, e = e.next) {
701 if (e.hash==hash && e.equals(entry)) {
720 public void clear() {
721 Hashtable.this.clear();
726 * Returns a {@link Collection} view of the values contained in this map.
727 * The collection is backed by the map, so changes to the map are
728 * reflected in the collection, and vice-versa. If the map is
729 * modified while an iteration over the collection is in progress
730 * (except through the iterator's own <tt>remove</tt> operation),
731 * the results of the iteration are undefined. The collection
732 * supports element removal, which removes the corresponding
733 * mapping from the map, via the <tt>Iterator.remove</tt>,
734 * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
735 * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not
736 * support the <tt>add</tt> or <tt>addAll</tt> operations.
740 public Collection<V> values() {
742 values = Collections.synchronizedCollection(new ValueCollection(),
747 private class ValueCollection extends AbstractCollection<V> {
748 public Iterator<V> iterator() {
749 return getIterator(VALUES);
754 public boolean contains(Object o) {
755 return containsValue(o);
757 public void clear() {
758 Hashtable.this.clear();
762 // Comparison and hashing
765 * Compares the specified Object with this Map for equality,
766 * as per the definition in the Map interface.
768 * @param o object to be compared for equality with this hashtable
769 * @return true if the specified Object is equal to this Map
770 * @see Map#equals(Object)
773 public synchronized boolean equals(Object o) {
777 if (!(o instanceof Map))
779 Map<K,V> t = (Map<K,V>) o;
780 if (t.size() != size())
784 Iterator<Map.Entry<K,V>> i = entrySet().iterator();
785 while (i.hasNext()) {
786 Map.Entry<K,V> e = i.next();
788 V value = e.getValue();
790 if (!(t.get(key)==null && t.containsKey(key)))
793 if (!value.equals(t.get(key)))
797 } catch (ClassCastException unused) {
799 } catch (NullPointerException unused) {
807 * Returns the hash code value for this Map as per the definition in the
810 * @see Map#hashCode()
813 public synchronized int hashCode() {
815 * This code detects the recursion caused by computing the hash code
816 * of a self-referential hash table and prevents the stack overflow
817 * that would otherwise result. This allows certain 1.1-era
818 * applets with self-referential hash tables to work. This code
819 * abuses the loadFactor field to do double-duty as a hashCode
820 * in progress flag, so as not to worsen the space performance.
821 * A negative load factor indicates that hash code computation is
825 if (count == 0 || loadFactor < 0)
826 return h; // Returns zero
828 loadFactor = -loadFactor; // Mark hashCode computation in progress
830 for (int i = 0; i < tab.length; i++)
831 for (Entry e = tab[i]; e != null; e = e.next)
832 h += e.key.hashCode() ^ e.value.hashCode();
833 loadFactor = -loadFactor; // Mark hashCode computation complete
839 * Hashtable collision list.
841 private static class Entry<K,V> implements Map.Entry<K,V> {
847 protected Entry(int hash, K key, V value, Entry<K,V> next) {
854 protected Object clone() {
855 return new Entry<>(hash, key, value,
856 (next==null ? null : (Entry<K,V>) next.clone()));
865 public V getValue() {
869 public V setValue(V value) {
871 throw new NullPointerException();
873 V oldValue = this.value;
878 public boolean equals(Object o) {
879 if (!(o instanceof Map.Entry))
881 Map.Entry e = (Map.Entry)o;
883 return (key==null ? e.getKey()==null : key.equals(e.getKey())) &&
884 (value==null ? e.getValue()==null : value.equals(e.getValue()));
887 public int hashCode() {
888 return hash ^ (value==null ? 0 : value.hashCode());
891 public String toString() {
892 return key.toString()+"="+value.toString();
896 // Types of Enumerations/Iterations
897 private static final int KEYS = 0;
898 private static final int VALUES = 1;
899 private static final int ENTRIES = 2;
902 * A hashtable enumerator class. This class implements both the
903 * Enumeration and Iterator interfaces, but individual instances
904 * can be created with the Iterator methods disabled. This is necessary
905 * to avoid unintentionally increasing the capabilities granted a user
906 * by passing an Enumeration.
908 private class Enumerator<T> implements Enumeration<T>, Iterator<T> {
909 Entry[] table = Hashtable.this.table;
910 int index = table.length;
911 Entry<K,V> entry = null;
912 Entry<K,V> lastReturned = null;
916 * Indicates whether this Enumerator is serving as an Iterator
917 * or an Enumeration. (true -> Iterator).
922 * The modCount value that the iterator believes that the backing
923 * Hashtable should have. If this expectation is violated, the iterator
924 * has detected concurrent modification.
926 protected int expectedModCount = modCount;
928 Enumerator(int type, boolean iterator) {
930 this.iterator = iterator;
933 public boolean hasMoreElements() {
934 Entry<K,V> e = entry;
937 /* Use locals for faster loop iteration */
938 while (e == null && i > 0) {
946 public T nextElement() {
947 Entry<K,V> et = entry;
950 /* Use locals for faster loop iteration */
951 while (et == null && i > 0) {
957 Entry<K,V> e = lastReturned = entry;
959 return type == KEYS ? (T)e.key : (type == VALUES ? (T)e.value : (T)e);
961 throw new NoSuchElementException("Hashtable Enumerator");
965 public boolean hasNext() {
966 return hasMoreElements();
970 if (modCount != expectedModCount)
971 throw new ConcurrentModificationException();
972 return nextElement();
975 public void remove() {
977 throw new UnsupportedOperationException();
978 if (lastReturned == null)
979 throw new IllegalStateException("Hashtable Enumerator");
980 if (modCount != expectedModCount)
981 throw new ConcurrentModificationException();
983 synchronized(Hashtable.this) {
984 Entry[] tab = Hashtable.this.table;
985 int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;
987 for (Entry<K,V> e = tab[index], prev = null; e != null;
988 prev = e, e = e.next) {
989 if (e == lastReturned) {
1001 throw new ConcurrentModificationException();