< prev index next >
src/java.base/share/classes/java/util/Hashtable.java
Print this page
*** 30,46 ****
import java.util.function.Function;
import java.util.function.BiFunction;
/**
* This class implements a hash table, which maps keys to values. Any
! * non-<code>null</code> object can be used as a key or as a value. <p>
*
* To successfully store and retrieve objects from a hashtable, the
! * objects used as keys must implement the <code>hashCode</code>
! * method and the <code>equals</code> method. <p>
*
! * An instance of <code>Hashtable</code> has two parameters that affect its
* performance: <i>initial capacity</i> and <i>load factor</i>. The
* <i>capacity</i> is the number of <i>buckets</i> in the hash table, and the
* <i>initial capacity</i> is simply the capacity at the time the hash table
* is created. Note that the hash table is <i>open</i>: in the case of a "hash
* collision", a single bucket stores multiple entries, which must be searched
--- 30,46 ----
import java.util.function.Function;
import java.util.function.BiFunction;
/**
* This class implements a hash table, which maps keys to values. Any
! * non-{@code null} object can be used as a key or as a value. <p>
*
* To successfully store and retrieve objects from a hashtable, the
! * objects used as keys must implement the {@code hashCode}
! * method and the {@code equals} method. <p>
*
! * An instance of {@code Hashtable} has two parameters that affect its
* performance: <i>initial capacity</i> and <i>load factor</i>. The
* <i>capacity</i> is the number of <i>buckets</i> in the hash table, and the
* <i>initial capacity</i> is simply the capacity at the time the hash table
* is created. Note that the hash table is <i>open</i>: in the case of a "hash
* collision", a single bucket stores multiple entries, which must be searched
*** 51,70 ****
* method is invoked are implementation-dependent.<p>
*
* Generally, the default load factor (.75) offers a good tradeoff between
* time and space costs. Higher values decrease the space overhead but
* increase the time cost to look up an entry (which is reflected in most
! * <tt>Hashtable</tt> operations, including <tt>get</tt> and <tt>put</tt>).<p>
*
* The initial capacity controls a tradeoff between wasted space and the
! * need for <code>rehash</code> operations, which are time-consuming.
! * No <code>rehash</code> operations will <i>ever</i> occur if the initial
* capacity is greater than the maximum number of entries the
! * <tt>Hashtable</tt> will contain divided by its load factor. However,
* setting the initial capacity too high can waste space.<p>
*
! * If many entries are to be made into a <code>Hashtable</code>,
* creating it with a sufficiently large capacity may allow the
* entries to be inserted more efficiently than letting it perform
* automatic rehashing as needed to grow the table. <p>
*
* This example creates a hashtable of numbers. It uses the names of
--- 51,70 ----
* method is invoked are implementation-dependent.<p>
*
* Generally, the default load factor (.75) offers a good tradeoff between
* time and space costs. Higher values decrease the space overhead but
* increase the time cost to look up an entry (which is reflected in most
! * {@code Hashtable} operations, including {@code get} and {@code put}).<p>
*
* The initial capacity controls a tradeoff between wasted space and the
! * need for {@code rehash} operations, which are time-consuming.
! * No {@code rehash} operations will <i>ever</i> occur if the initial
* capacity is greater than the maximum number of entries the
! * {@code Hashtable} will contain divided by its load factor. However,
* setting the initial capacity too high can waste space.<p>
*
! * If many entries are to be made into a {@code Hashtable},
* creating it with a sufficiently large capacity may allow the
* entries to be inserted more efficiently than letting it perform
* automatic rehashing as needed to grow the table. <p>
*
* This example creates a hashtable of numbers. It uses the names of
*** 81,95 ****
* Integer n = numbers.get("two");
* if (n != null) {
* System.out.println("two = " + n);
* }}</pre>
*
! * <p>The iterators returned by the <tt>iterator</tt> method of the collections
* returned by all of this class's "collection view methods" are
* <em>fail-fast</em>: if the Hashtable is structurally modified at any time
* after the iterator is created, in any way except through the iterator's own
! * <tt>remove</tt> method, the iterator will throw a {@link
* ConcurrentModificationException}. Thus, in the face of concurrent
* modification, the iterator fails quickly and cleanly, rather than risking
* arbitrary, non-deterministic behavior at an undetermined time in the future.
* The Enumerations returned by Hashtable's {@link #keys keys} and
* {@link #elements elements} methods are <em>not</em> fail-fast; if the
--- 81,95 ----
* Integer n = numbers.get("two");
* if (n != null) {
* System.out.println("two = " + n);
* }}</pre>
*
! * <p>The iterators returned by the {@code iterator} method of the collections
* returned by all of this class's "collection view methods" are
* <em>fail-fast</em>: if the Hashtable is structurally modified at any time
* after the iterator is created, in any way except through the iterator's own
! * {@code remove} method, the iterator will throw a {@link
* ConcurrentModificationException}. Thus, in the face of concurrent
* modification, the iterator fails quickly and cleanly, rather than risking
* arbitrary, non-deterministic behavior at an undetermined time in the future.
* The Enumerations returned by Hashtable's {@link #keys keys} and
* {@link #elements elements} methods are <em>not</em> fail-fast; if the
*** 97,107 ****
* created then the results of enumerating are undefined.
*
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
* as it is, generally speaking, impossible to make any hard guarantees in the
* presence of unsynchronized concurrent modification. Fail-fast iterators
! * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
* Therefore, it would be wrong to write a program that depended on this
* exception for its correctness: <i>the fail-fast behavior of iterators
* should be used only to detect bugs.</i>
*
* <p>As of the Java 2 platform v1.2, this class was retrofitted to
--- 97,107 ----
* created then the results of enumerating are undefined.
*
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
* as it is, generally speaking, impossible to make any hard guarantees in the
* presence of unsynchronized concurrent modification. Fail-fast iterators
! * throw {@code ConcurrentModificationException} on a best-effort basis.
* Therefore, it would be wrong to write a program that depended on this
* exception for its correctness: <i>the fail-fast behavior of iterators
* should be used only to detect bugs.</i>
*
* <p>As of the Java 2 platform v1.2, this class was retrofitted to
*** 239,250 ****
}
/**
* Tests if this hashtable maps no keys to values.
*
! * @return <code>true</code> if this hashtable maps no keys to values;
! * <code>false</code> otherwise.
*/
public synchronized boolean isEmpty() {
return count == 0;
}
--- 239,250 ----
}
/**
* Tests if this hashtable maps no keys to values.
*
! * @return {@code true} if this hashtable maps no keys to values;
! * {@code false} otherwise.
*/
public synchronized boolean isEmpty() {
return count == 0;
}
*** 288,302 ****
* <p>Note that this method is identical in functionality to
* {@link #containsValue containsValue}, (which is part of the
* {@link Map} interface in the collections framework).
*
* @param value a value to search for
! * @return <code>true</code> if and only if some key maps to the
! * <code>value</code> argument in this hashtable as
! * determined by the <tt>equals</tt> method;
! * <code>false</code> otherwise.
! * @exception NullPointerException if the value is <code>null</code>
*/
public synchronized boolean contains(Object value) {
if (value == null) {
throw new NullPointerException();
}
--- 288,302 ----
* <p>Note that this method is identical in functionality to
* {@link #containsValue containsValue}, (which is part of the
* {@link Map} interface in the collections framework).
*
* @param value a value to search for
! * @return {@code true} if and only if some key maps to the
! * {@code value} argument in this hashtable as
! * determined by the {@code equals} method;
! * {@code false} otherwise.
! * @exception NullPointerException if the value is {@code null}
*/
public synchronized boolean contains(Object value) {
if (value == null) {
throw new NullPointerException();
}
*** 317,343 ****
*
* <p>Note that this method is identical in functionality to {@link
* #contains contains} (which predates the {@link Map} interface).
*
* @param value value whose presence in this hashtable is to be tested
! * @return <tt>true</tt> if this map maps one or more keys to the
* specified value
! * @throws NullPointerException if the value is <code>null</code>
* @since 1.2
*/
public boolean containsValue(Object value) {
return contains(value);
}
/**
* Tests if the specified object is a key in this hashtable.
*
* @param key possible key
! * @return <code>true</code> if and only if the specified object
* is a key in this hashtable, as determined by the
! * <tt>equals</tt> method; <code>false</code> otherwise.
! * @throws NullPointerException if the key is <code>null</code>
* @see #contains(Object)
*/
public synchronized boolean containsKey(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
--- 317,343 ----
*
* <p>Note that this method is identical in functionality to {@link
* #contains contains} (which predates the {@link Map} interface).
*
* @param value value whose presence in this hashtable is to be tested
! * @return {@code true} if this map maps one or more keys to the
* specified value
! * @throws NullPointerException if the value is {@code null}
* @since 1.2
*/
public boolean containsValue(Object value) {
return contains(value);
}
/**
* Tests if the specified object is a key in this hashtable.
*
* @param key possible key
! * @return {@code true} if and only if the specified object
* is a key in this hashtable, as determined by the
! * {@code equals} method; {@code false} otherwise.
! * @throws NullPointerException if the key is {@code null}
* @see #contains(Object)
*/
public synchronized boolean containsKey(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
*** 442,464 ****
count++;
modCount++;
}
/**
! * Maps the specified <code>key</code> to the specified
! * <code>value</code> in this hashtable. Neither the key nor the
! * value can be <code>null</code>. <p>
*
! * The value can be retrieved by calling the <code>get</code> method
* with a key that is equal to the original key.
*
* @param key the hashtable key
* @param value the value
* @return the previous value of the specified key in this hashtable,
! * or <code>null</code> if it did not have one
* @exception NullPointerException if the key or value is
! * <code>null</code>
* @see Object#equals(Object)
* @see #get(Object)
*/
public synchronized V put(K key, V value) {
// Make sure the value is not null
--- 442,464 ----
count++;
modCount++;
}
/**
! * Maps the specified {@code key} to the specified
! * {@code value} in this hashtable. Neither the key nor the
! * value can be {@code null}. <p>
*
! * The value can be retrieved by calling the {@code get} method
* with a key that is equal to the original key.
*
* @param key the hashtable key
* @param value the value
* @return the previous value of the specified key in this hashtable,
! * or {@code null} if it did not have one
* @exception NullPointerException if the key or value is
! * {@code null}
* @see Object#equals(Object)
* @see #get(Object)
*/
public synchronized V put(K key, V value) {
// Make sure the value is not null
*** 488,499 ****
* Removes the key (and its corresponding value) from this
* hashtable. This method does nothing if the key is not in the hashtable.
*
* @param key the key that needs to be removed
* @return the value to which the key had been mapped in this hashtable,
! * or <code>null</code> if the key did not have a mapping
! * @throws NullPointerException if the key is <code>null</code>
*/
public synchronized V remove(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
--- 488,499 ----
* Removes the key (and its corresponding value) from this
* hashtable. This method does nothing if the key is not in the hashtable.
*
* @param key the key that needs to be removed
* @return the value to which the key had been mapped in this hashtable,
! * or {@code null} if the key did not have a mapping
! * @throws NullPointerException if the key is {@code null}
*/
public synchronized V remove(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
*** 566,580 ****
throw new InternalError(e);
}
}
/**
! * Returns a string representation of this <tt>Hashtable</tt> object
* in the form of a set of entries, enclosed in braces and separated
! * by the ASCII characters "<tt>, </tt>" (comma and space). Each
! * entry is rendered as the key, an equals sign <tt>=</tt>, and the
! * associated element, where the <tt>toString</tt> method is used to
* convert the key and element to strings.
*
* @return a string representation of this hashtable
*/
public synchronized String toString() {
--- 566,580 ----
throw new InternalError(e);
}
}
/**
! * Returns a string representation of this {@code Hashtable} object
* in the form of a set of entries, enclosed in braces and separated
! * by the ASCII characters "<code> , </code>" (comma and space). Each
! * entry is rendered as the key, an equals sign {@code =}, and the
! * associated element, where the {@code toString} method is used to
* convert the key and element to strings.
*
* @return a string representation of this hashtable
*/
public synchronized String toString() {
*** 631,646 ****
/**
* Returns a {@link Set} view of the keys contained in this map.
* The set is backed by the map, so changes to the map are
* reflected in the set, and vice-versa. If the map is modified
* while an iteration over the set is in progress (except through
! * the iterator's own <tt>remove</tt> operation), the results of
* the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
! * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
! * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
! * operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
* operations.
*
* @since 1.2
*/
public Set<K> keySet() {
--- 631,646 ----
/**
* Returns a {@link Set} view of the keys contained in this map.
* The set is backed by the map, so changes to the map are
* reflected in the set, and vice-versa. If the map is modified
* while an iteration over the set is in progress (except through
! * the iterator's own {@code remove} operation), the results of
* the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
! * {@code Iterator.remove}, {@code Set.remove},
! * {@code removeAll}, {@code retainAll}, and {@code clear}
! * operations. It does not support the {@code add} or {@code addAll}
* operations.
*
* @since 1.2
*/
public Set<K> keySet() {
*** 670,687 ****
/**
* Returns a {@link Set} view of the mappings contained in this map.
* The set is backed by the map, so changes to the map are
* reflected in the set, and vice-versa. If the map is modified
* while an iteration over the set is in progress (except through
! * the iterator's own <tt>remove</tt> operation, or through the
! * <tt>setValue</tt> operation on a map entry returned by the
* iterator) the results of the iteration are undefined. The set
* supports element removal, which removes the corresponding
! * mapping from the map, via the <tt>Iterator.remove</tt>,
! * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
! * <tt>clear</tt> operations. It does not support the
! * <tt>add</tt> or <tt>addAll</tt> operations.
*
* @since 1.2
*/
public Set<Map.Entry<K,V>> entrySet() {
if (entrySet==null)
--- 670,687 ----
/**
* Returns a {@link Set} view of the mappings contained in this map.
* The set is backed by the map, so changes to the map are
* reflected in the set, and vice-versa. If the map is modified
* while an iteration over the set is in progress (except through
! * the iterator's own {@code remove} operation, or through the
! * {@code setValue} operation on a map entry returned by the
* iterator) the results of the iteration are undefined. The set
* supports element removal, which removes the corresponding
! * mapping from the map, via the {@code Iterator.remove},
! * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
! * {@code clear} operations. It does not support the
! * {@code add} or {@code addAll} operations.
*
* @since 1.2
*/
public Set<Map.Entry<K,V>> entrySet() {
if (entrySet==null)
*** 752,768 ****
/**
* Returns a {@link Collection} view of the values contained in this map.
* The collection is backed by the map, so changes to the map are
* reflected in the collection, and vice-versa. If the map is
* modified while an iteration over the collection is in progress
! * (except through the iterator's own <tt>remove</tt> operation),
* the results of the iteration are undefined. The collection
* supports element removal, which removes the corresponding
! * mapping from the map, via the <tt>Iterator.remove</tt>,
! * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
! * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not
! * support the <tt>add</tt> or <tt>addAll</tt> operations.
*
* @since 1.2
*/
public Collection<V> values() {
if (values==null)
--- 752,768 ----
/**
* Returns a {@link Collection} view of the values contained in this map.
* The collection is backed by the map, so changes to the map are
* reflected in the collection, and vice-versa. If the map is
* modified while an iteration over the collection is in progress
! * (except through the iterator's own {@code remove} operation),
* the results of the iteration are undefined. The collection
* supports element removal, which removes the corresponding
! * mapping from the map, via the {@code Iterator.remove},
! * {@code Collection.remove}, {@code removeAll},
! * {@code retainAll} and {@code clear} operations. It does not
! * support the {@code add} or {@code addAll} operations.
*
* @since 1.2
*/
public Collection<V> values() {
if (values==null)
< prev index next >