public class Hashtable<K, V>
- Object
- Dictionary<K, V>
- Hashtable
ImplementsMap<K, V>
Hashtable associates keys with values. Both keys and values cannot be null.
The size of the Hashtable is the number of key/value pairs it contains. The
capacity is the number of key/value pairs the Hashtable can hold. The load
factor is a float value which determines how full the Hashtable gets before
expanding the capacity. If the load factor of the Hashtable is exceeded, the
capacity is doubled.
Constructors
public Hashtable() | Constructs a new Hashtable using the default capacity and load factor. |
public Hashtable(int capacity) | Constructs a new Hashtable using the specified capacity and the default load factor. |
public Hashtable(int capacity, float loadFactor) | Constructs a new Hashtable using the specified capacity and load factor. |
public Hashtable(Map<? extends K, ? extends V> map) | Constructs a new instance of Hashtable containing the mappings from the specified map. |
Methods
public synchronized void clear() | Removes all key/value pairs from this Hashtable, leaving the size zero and the capacity unchanged. |
public synchronized boolean contains(Object value) | Returns true if this Hashtable contains the specified object as the value of at least one of the key/value pairs. |
public synchronized boolean containsKey(Object key) | Returns true if this Hashtable contains the specified object as a key of one of the key/value pairs. |
public boolean containsValue(Object value) | Searches this Hashtable for the specified value. |
public synchronized Enumeration<V> elements() | Returns an enumeration on the values of this Hashtable. |
public Set<Map.Entry<K, V>> entrySet() | Returns a set of the mappings contained in this Hashtable. |
public synchronized boolean equals(Object object) | Compares this Hashtable with the specified object and indicates if they are equal. |
public synchronized V get(Object key) | Returns the value associated with the specified key in this Hashtable. |
public synchronized int hashCode() | Returns a hash code value for the object. |
public synchronized boolean isEmpty() | Returns true if this Hashtable has no key/value pairs. |
public synchronized Enumeration<K> keys() | Returns an enumeration on the keys of this Hashtable instance. |
public Set<K> keySet() | Returns a set of the keys contained in this Hashtable. |
public synchronized V put(K key, V value) | Associate the specified value with the specified key in this Hashtable. |
public synchronized void putAll(Map<? extends K, ? extends V> map) | Copies every mapping to this Hashtable from the specified map. |
protected void rehash() | Increases the capacity of this Hashtable. |
public synchronized V remove(Object key) | Removes the key/value pair with the specified key from this Hashtable. |
public synchronized int size() | Returns the number of key/value pairs in this Hashtable. |
public synchronized String toString() | Returns the string representation of this Hashtable. |
public Collection<V> values() | Returns a collection of the values contained in this Hashtable. |
Inherited methods
Constructor details
Hashtable
public Hashtable()Constructs a new
Hashtable using the default capacity and load
factor.Hashtable
public Hashtable(int capacity)Constructs a new
Hashtable using the specified capacity and the
default load factor.Parameters
capacityint- the initial capacity.
Hashtable
public Hashtable(int capacity, float loadFactor)Constructs a new
Hashtable using the specified capacity and load
factor.Parameters
capacityint- the initial capacity.
loadFactorfloat- the initial load factor.
Hashtable
public Hashtable(Map<? extends K, ? extends V> map)Constructs a new instance of
Hashtable containing the mappings
from the specified map.Parameters
mapMap<? extends K, ? extends V>- the mappings to add.
Method details
clear
public synchronized void clear()Removes all key/value pairs from this
Hashtable, leaving the
size zero and the capacity unchanged.Throws
UnsupportedOperationException- if removing elements from this
Mapis not supported.
contains
public synchronized boolean contains(Object value)Returns true if this
Hashtable contains the specified object as
the value of at least one of the key/value pairs.Parameters
valueObject- the object to look for as a value in this
Hashtable.
Returns
true if object is a value in this Hashtable,
false otherwise.containsKey
public synchronized boolean containsKey(Object key)Returns true if this
Hashtable contains the specified object as a
key of one of the key/value pairs.Parameters
keyObject- the object to look for as a key in this
Hashtable.
Returns
true if object is a key in this Hashtable,
false otherwise.See also
containsValue
public boolean containsValue(Object value)Searches this
Hashtable for the specified value.Parameters
valueObject- the object to search for.
Returns
true if value is a value of this
Hashtable, false otherwise.elements
public synchronized Enumeration<V> elements()Returns an enumeration on the values of this
Hashtable. The
results of the Enumeration may be affected if the contents of this
Hashtable are modified.Returns
an enumeration of the values of this
Hashtable.See also
entrySet
public Set<Map.Entry<K, V>> entrySet()Returns a set of the mappings contained in this
Hashtable. Each
element in the set is a Map.Entry. The set is backed by this
Hashtable so changes to one are reflected by the other. The set
does not support adding.Returns
a set of the mappings.
equals
public synchronized boolean equals(Object object)Compares this
Hashtable with the specified object and indicates
if they are equal. In order to be equal, object must be an
instance of Map and contain the same key/value pairs.Parameters
objectObject- the object to compare with this object.
Returns
true if the specified object is equal to this Map,
false otherwise.See also
get
public synchronized V get(Object key)Returns the value associated with the specified key in this
Hashtable.Parameters
keyObject- the key of the value returned.
Returns
the value associated with the specified key, or
null if
the specified key does not exist.See also
hashCode
public synchronized int hashCode()Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
isEmpty
public synchronized boolean isEmpty()Returns true if this
Hashtable has no key/value pairs.Returns
true if this Hashtable has no key/value pairs,
false otherwise.See also
keys
public synchronized Enumeration<K> keys()Returns an enumeration on the keys of this
Hashtable instance.
The results of the enumeration may be affected if the contents of this
Hashtable are modified.Returns
an enumeration of the keys of this
Hashtable.See also
keySet
public Set<K> keySet()Returns a set of the keys contained in this
Hashtable. The set
is backed by this Hashtable so changes to one are reflected by
the other. The set does not support adding.Returns
a set of the keys.
put
public synchronized V put(K key, V value)Associate the specified value with the specified key in this
Hashtable. If the key already exists, the old value is replaced.
The key and value cannot be null.Parameters
keyK- the key to add.
valueV- the value to add.
Returns
the old value associated with the specified key, or
null
if the key did not exist.putAll
public synchronized void putAll(Map<? extends K, ? extends V> map)Copies every mapping to this
Hashtable from the specified map.Parameters
mapMap<? extends K, ? extends V>- the map to copy mappings from.
Throws
UnsupportedOperationException- if adding to this
Mapis not supported. ClassCastException- if the class of a key or a value of the specified
Mapis inappropriate for thisMap. IllegalArgumentException- if a key or value cannot be added to this
Map. NullPointerException- if a key or value is
nulland thisMapdoes not supportnullkeys or values.
rehash
protected void rehash()Increases the capacity of this
Hashtable. This method is called
when the size of this Hashtable exceeds the load factor.remove
public synchronized V remove(Object key)Removes the key/value pair with the specified key from this
Hashtable.Parameters
keyObject- the key to remove.
Returns
the value associated with the specified key, or
null if
the specified key did not exist.size
public synchronized int size()Returns the number of key/value pairs in this
Hashtable.Returns
the number of key/value pairs in this
Hashtable.toString
public synchronized String toString()Returns the string representation of this
Hashtable.Returns
the string representation of this
Hashtable.values
public Collection<V> values()Returns a collection of the values contained in this
Hashtable.
The collection is backed by this Hashtable so changes to one are
reflected by the other. The collection does not support adding.Returns
a collection of the values.