163
164 /**
165 * The number of times this Hashtable has been structurally modified
166 * Structural modifications are those that change the number of entries in
167 * the Hashtable or otherwise modify its internal structure (e.g.,
168 * rehash). This field is used to make iterators on Collection-views of
169 * the Hashtable fail-fast. (See ConcurrentModificationException).
170 */
171 private transient int modCount = 0;
172
173 /** use serialVersionUID from JDK 1.0.2 for interoperability */
174 @java.io.Serial
175 private static final long serialVersionUID = 1421746759512286392L;
176
177 /**
178 * Constructs a new, empty hashtable with the specified initial
179 * capacity and the specified load factor.
180 *
181 * @param initialCapacity the initial capacity of the hashtable.
182 * @param loadFactor the load factor of the hashtable.
183 * @exception IllegalArgumentException if the initial capacity is less
184 * than zero, or if the load factor is nonpositive.
185 */
186 public Hashtable(int initialCapacity, float loadFactor) {
187 if (initialCapacity < 0)
188 throw new IllegalArgumentException("Illegal Capacity: "+
189 initialCapacity);
190 if (loadFactor <= 0 || Float.isNaN(loadFactor))
191 throw new IllegalArgumentException("Illegal Load: "+loadFactor);
192
193 if (initialCapacity==0)
194 initialCapacity = 1;
195 this.loadFactor = loadFactor;
196 table = new Entry<?,?>[initialCapacity];
197 threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
198 }
199
200 /**
201 * Constructs a new, empty hashtable with the specified initial capacity
202 * and default load factor (0.75).
203 *
204 * @param initialCapacity the initial capacity of the hashtable.
205 * @exception IllegalArgumentException if the initial capacity is less
206 * than zero.
207 */
208 public Hashtable(int initialCapacity) {
209 this(initialCapacity, 0.75f);
210 }
211
212 /**
213 * Constructs a new, empty hashtable with a default initial capacity (11)
214 * and load factor (0.75).
215 */
216 public Hashtable() {
217 this(11, 0.75f);
218 }
219
220 /**
221 * Constructs a new hashtable with the same mappings as the given
222 * Map. The hashtable is created with an initial capacity sufficient to
223 * hold the mappings in the given Map and a default load factor (0.75).
224 *
225 * @param t the map whose mappings are to be placed in this map.
287 * @see Map
288 */
289 public synchronized Enumeration<V> elements() {
290 return this.<V>getEnumeration(VALUES);
291 }
292
293 /**
294 * Tests if some key maps into the specified value in this hashtable.
295 * This operation is more expensive than the {@link #containsKey
296 * containsKey} method.
297 *
298 * <p>Note that this method is identical in functionality to
299 * {@link #containsValue containsValue}, (which is part of the
300 * {@link Map} interface in the collections framework).
301 *
302 * @param value a value to search for
303 * @return {@code true} if and only if some key maps to the
304 * {@code value} argument in this hashtable as
305 * determined by the {@code equals} method;
306 * {@code false} otherwise.
307 * @exception NullPointerException if the value is {@code null}
308 */
309 public synchronized boolean contains(Object value) {
310 if (value == null) {
311 throw new NullPointerException();
312 }
313
314 Entry<?,?> tab[] = table;
315 for (int i = tab.length ; i-- > 0 ;) {
316 for (Entry<?,?> e = tab[i] ; e != null ; e = e.next) {
317 if (e.value.equals(value)) {
318 return true;
319 }
320 }
321 }
322 return false;
323 }
324
325 /**
326 * Returns true if this hashtable maps one or more keys to this value.
327 *
448 // Creates the new entry.
449 @SuppressWarnings("unchecked")
450 Entry<K,V> e = (Entry<K,V>) tab[index];
451 tab[index] = new Entry<>(hash, key, value, e);
452 count++;
453 modCount++;
454 }
455
456 /**
457 * Maps the specified {@code key} to the specified
458 * {@code value} in this hashtable. Neither the key nor the
459 * value can be {@code null}. <p>
460 *
461 * The value can be retrieved by calling the {@code get} method
462 * with a key that is equal to the original key.
463 *
464 * @param key the hashtable key
465 * @param value the value
466 * @return the previous value of the specified key in this hashtable,
467 * or {@code null} if it did not have one
468 * @exception NullPointerException if the key or value is
469 * {@code null}
470 * @see Object#equals(Object)
471 * @see #get(Object)
472 */
473 public synchronized V put(K key, V value) {
474 // Make sure the value is not null
475 if (value == null) {
476 throw new NullPointerException();
477 }
478
479 // Makes sure the key is not already in the hashtable.
480 Entry<?,?> tab[] = table;
481 int hash = key.hashCode();
482 int index = (hash & 0x7FFFFFFF) % tab.length;
483 @SuppressWarnings("unchecked")
484 Entry<K,V> entry = (Entry<K,V>)tab[index];
485 for(; entry != null ; entry = entry.next) {
486 if ((entry.hash == hash) && entry.key.equals(key)) {
487 V old = entry.value;
488 entry.value = value;
|
163
164 /**
165 * The number of times this Hashtable has been structurally modified
166 * Structural modifications are those that change the number of entries in
167 * the Hashtable or otherwise modify its internal structure (e.g.,
168 * rehash). This field is used to make iterators on Collection-views of
169 * the Hashtable fail-fast. (See ConcurrentModificationException).
170 */
171 private transient int modCount = 0;
172
173 /** use serialVersionUID from JDK 1.0.2 for interoperability */
174 @java.io.Serial
175 private static final long serialVersionUID = 1421746759512286392L;
176
177 /**
178 * Constructs a new, empty hashtable with the specified initial
179 * capacity and the specified load factor.
180 *
181 * @param initialCapacity the initial capacity of the hashtable.
182 * @param loadFactor the load factor of the hashtable.
183 * @throws IllegalArgumentException if the initial capacity is less
184 * than zero, or if the load factor is nonpositive.
185 */
186 public Hashtable(int initialCapacity, float loadFactor) {
187 if (initialCapacity < 0)
188 throw new IllegalArgumentException("Illegal Capacity: "+
189 initialCapacity);
190 if (loadFactor <= 0 || Float.isNaN(loadFactor))
191 throw new IllegalArgumentException("Illegal Load: "+loadFactor);
192
193 if (initialCapacity==0)
194 initialCapacity = 1;
195 this.loadFactor = loadFactor;
196 table = new Entry<?,?>[initialCapacity];
197 threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
198 }
199
200 /**
201 * Constructs a new, empty hashtable with the specified initial capacity
202 * and default load factor (0.75).
203 *
204 * @param initialCapacity the initial capacity of the hashtable.
205 * @throws IllegalArgumentException if the initial capacity is less
206 * than zero.
207 */
208 public Hashtable(int initialCapacity) {
209 this(initialCapacity, 0.75f);
210 }
211
212 /**
213 * Constructs a new, empty hashtable with a default initial capacity (11)
214 * and load factor (0.75).
215 */
216 public Hashtable() {
217 this(11, 0.75f);
218 }
219
220 /**
221 * Constructs a new hashtable with the same mappings as the given
222 * Map. The hashtable is created with an initial capacity sufficient to
223 * hold the mappings in the given Map and a default load factor (0.75).
224 *
225 * @param t the map whose mappings are to be placed in this map.
287 * @see Map
288 */
289 public synchronized Enumeration<V> elements() {
290 return this.<V>getEnumeration(VALUES);
291 }
292
293 /**
294 * Tests if some key maps into the specified value in this hashtable.
295 * This operation is more expensive than the {@link #containsKey
296 * containsKey} method.
297 *
298 * <p>Note that this method is identical in functionality to
299 * {@link #containsValue containsValue}, (which is part of the
300 * {@link Map} interface in the collections framework).
301 *
302 * @param value a value to search for
303 * @return {@code true} if and only if some key maps to the
304 * {@code value} argument in this hashtable as
305 * determined by the {@code equals} method;
306 * {@code false} otherwise.
307 * @throws NullPointerException if the value is {@code null}
308 */
309 public synchronized boolean contains(Object value) {
310 if (value == null) {
311 throw new NullPointerException();
312 }
313
314 Entry<?,?> tab[] = table;
315 for (int i = tab.length ; i-- > 0 ;) {
316 for (Entry<?,?> e = tab[i] ; e != null ; e = e.next) {
317 if (e.value.equals(value)) {
318 return true;
319 }
320 }
321 }
322 return false;
323 }
324
325 /**
326 * Returns true if this hashtable maps one or more keys to this value.
327 *
448 // Creates the new entry.
449 @SuppressWarnings("unchecked")
450 Entry<K,V> e = (Entry<K,V>) tab[index];
451 tab[index] = new Entry<>(hash, key, value, e);
452 count++;
453 modCount++;
454 }
455
456 /**
457 * Maps the specified {@code key} to the specified
458 * {@code value} in this hashtable. Neither the key nor the
459 * value can be {@code null}. <p>
460 *
461 * The value can be retrieved by calling the {@code get} method
462 * with a key that is equal to the original key.
463 *
464 * @param key the hashtable key
465 * @param value the value
466 * @return the previous value of the specified key in this hashtable,
467 * or {@code null} if it did not have one
468 * @throws NullPointerException if the key or value is
469 * {@code null}
470 * @see Object#equals(Object)
471 * @see #get(Object)
472 */
473 public synchronized V put(K key, V value) {
474 // Make sure the value is not null
475 if (value == null) {
476 throw new NullPointerException();
477 }
478
479 // Makes sure the key is not already in the hashtable.
480 Entry<?,?> tab[] = table;
481 int hash = key.hashCode();
482 int index = (hash & 0x7FFFFFFF) % tab.length;
483 @SuppressWarnings("unchecked")
484 Entry<K,V> entry = (Entry<K,V>)tab[index];
485 for(; entry != null ; entry = entry.next) {
486 if ((entry.hash == hash) && entry.key.equals(key)) {
487 V old = entry.value;
488 entry.value = value;
|