Package java.util
Class Hashtable<K,V>
java.lang.Object
java.util.Dictionary<K,V>
java.util.Hashtable<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
Serializable,Cloneable,Map<K,V>
- Direct Known Subclasses:
CountersTable,Properties,WeakHashtable
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable
Hashtable is a synchronized implementation of
Map. All optional operations are supported.
Neither keys nor values can be null. (Use HashMap or LinkedHashMap if you
need null keys or values.)
- See Also:
HashMap, Serialized Form
-
Nested Class Summary
-
Constructor Summary
Constructors Constructor Description Hashtable()Constructs a newHashtableusing the default capacity and load factor.Hashtable(int capacity)Constructs a newHashtableusing the specified capacity and the default load factor.Hashtable(int capacity, float loadFactor)Constructs a newHashtableusing the specified capacity and load factor.Hashtable(Map<? extends K,? extends V> map)Constructs a new instance ofHashtablecontaining the mappings from the specified map. -
Method Summary
Modifier and Type Method Description voidclear()Removes all key/value pairs from thisHashtable, leaving the size zero and the capacity unchanged.Objectclone()Returns a newHashtablewith the same key/value pairs, capacity and load factor.booleancontains(Object value)Returns true if thisHashtablecontains the specified object as the value of at least one of the key/value pairs.booleancontainsKey(Object key)Returns true if thisHashtablecontains the specified object as a key of one of the key/value pairs.booleancontainsValue(Object value)Searches thisHashtablefor the specified value.Enumeration<V>elements()Returns an enumeration on the values of thisHashtable.Set<Map.Entry<K,V>>entrySet()Returns a set of the mappings contained in thisHashtable.booleanequals(Object object)Compares thisHashtablewith the specified object and indicates if they are equal.Vget(Object key)Returns the value associated with the specified key in thisHashtable.inthashCode()Returns an integer hash code for this object.booleanisEmpty()Returns true if thisHashtablehas no key/value pairs.Enumeration<K>keys()Returns an enumeration on the keys of thisHashtableinstance.Set<K>keySet()Returns a set of the keys contained in thisHashtable.Vput(K key, V value)Associate the specified value with the specified key in thisHashtable.voidputAll(Map<? extends K,? extends V> map)Copies every mapping to thisHashtablefrom the specified map.protected voidrehash()Increases the capacity of thisHashtable.Vremove(Object key)Removes the key/value pair with the specified key from thisHashtable.intsize()Returns the number of key/value pairs in thisHashtable.StringtoString()Returns the string representation of thisHashtable.Collection<V>values()Returns a collection of the values contained in thisHashtable.
-
Constructor Details
-
Hashtable
public Hashtable()Constructs a newHashtableusing the default capacity and load factor. -
Hashtable
public Hashtable(int capacity)Constructs a newHashtableusing the specified capacity and the default load factor.- Parameters:
capacity- the initial capacity.
-
Hashtable
public Hashtable(int capacity, float loadFactor)Constructs a newHashtableusing the specified capacity and load factor.- Parameters:
capacity- the initial capacity.loadFactor- the initial load factor.
-
Hashtable
Constructs a new instance ofHashtablecontaining the mappings from the specified map.- Parameters:
map- the mappings to add.
-
-
Method Details
-
clone
Returns a newHashtablewith the same key/value pairs, capacity and load factor. -
isEmpty
public boolean isEmpty()Returns true if thisHashtablehas no key/value pairs. -
size
public int size()Returns the number of key/value pairs in thisHashtable.- Specified by:
sizein interfaceMap<K,V>- Specified by:
sizein classDictionary<K,V>- Returns:
- the number of key/value pairs in this
Hashtable. - See Also:
elements(),keys()
-
get
Returns the value associated with the specified key in thisHashtable. -
containsKey
Returns true if thisHashtablecontains the specified object as a key of one of the key/value pairs.- Specified by:
containsKeyin interfaceMap<K,V>- Parameters:
key- the object to look for as a key in thisHashtable.- Returns:
trueif object is a key in thisHashtable,falseotherwise.- See Also:
contains(java.lang.Object),Object.equals(java.lang.Object)
-
containsValue
Searches thisHashtablefor the specified value.- Specified by:
containsValuein interfaceMap<K,V>- Parameters:
value- the object to search for.- Returns:
trueifvalueis a value of thisHashtable,falseotherwise.
-
contains
Returns true if thisHashtablecontains the specified object as the value of at least one of the key/value pairs.- Parameters:
value- the object to look for as a value in thisHashtable.- Returns:
trueif object is a value in thisHashtable,falseotherwise.- See Also:
containsKey(java.lang.Object),Object.equals(java.lang.Object)
-
put
Associate the specified value with the specified key in thisHashtable. If the key already exists, the old value is replaced. The key and value cannot be null.- Specified by:
putin interfaceMap<K,V>- Specified by:
putin classDictionary<K,V>- Parameters:
key- the key to add.value- the value to add.- Returns:
- the old value associated with the specified key, or
nullif the key did not exist. - See Also:
elements(),get(java.lang.Object),keys(),Object.equals(java.lang.Object)
-
putAll
Copies every mapping to thisHashtablefrom the specified map. -
rehash
protected void rehash()Increases the capacity of thisHashtable. This method is called when the size of thisHashtableexceeds the load factor. -
remove
Removes the key/value pair with the specified key from thisHashtable.- Specified by:
removein interfaceMap<K,V>- Specified by:
removein classDictionary<K,V>- Parameters:
key- the key to remove.- Returns:
- the value associated with the specified key, or
nullif the specified key did not exist. - See Also:
get(java.lang.Object),put(K, V)
-
clear
public void clear()Removes all key/value pairs from thisHashtable, leaving the size zero and the capacity unchanged. -
keySet
Returns a set of the keys contained in thisHashtable. The set is backed by thisHashtableso changes to one are reflected by the other. The set does not support adding. -
values
Returns a collection of the values contained in thisHashtable. The collection is backed by thisHashtableso changes to one are reflected by the other. The collection does not support adding. -
entrySet
Returns a set of the mappings contained in thisHashtable. Each element in the set is aMap.Entry. The set is backed by thisHashtableso changes to one are reflected by the other. The set does not support adding. -
keys
Returns an enumeration on the keys of thisHashtableinstance. The results of the enumeration may be affected if the contents of thisHashtableare modified.- Specified by:
keysin classDictionary<K,V>- Returns:
- an enumeration of the keys of this
Hashtable. - See Also:
elements(),size,Enumeration
-
elements
Returns an enumeration on the values of thisHashtable. The results of the Enumeration may be affected if the contents of thisHashtableare modified.- Specified by:
elementsin classDictionary<K,V>- Returns:
- an enumeration of the values of this
Hashtable. - See Also:
keys(),size,Enumeration
-
equals
Compares thisHashtablewith the specified object and indicates if they are equal. In order to be equal,objectmust be an instance of Map and contain the same key/value pairs. -
hashCode
public int hashCode()Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod. -
toString
Returns the string representation of thisHashtable.
-