java.util
Class Hashtable<K,V>
- java.lang.Object
-
- java.util.Hashtable<K,V>
-
public class Hashtable<K,V> extends Object
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.
-
-
Constructor Summary
Constructors Constructor and Description Hashtable()
Constructs a newHashtable
using the default capacity and load factor.Hashtable(int capacity)
Constructs a newHashtable
using the specified capacity and the default load factor.
-
Method Summary
Methods Modifier and Type Method and Description void
clear()
Removes all key/value pairs from thisHashtable
, leaving the size zero and the capacity unchanged.boolean
contains(Object value)
Returns true if thisHashtable
contains the specified object as the value of at least one of the key/value pairs.boolean
containsKey(Object key)
Returns true if thisHashtable
contains the specified object as a key of one of the key/value pairs.Enumeration<V>
elements()
Returns an enumeration on the values of thisHashtable
.boolean
equals(Object object)
Compares thisHashtable
with the specified object and indicates if they are equal.V
get(Object key)
Returns the value associated with the specified key in thisHashtable
.int
hashCode()
Returns the hash code for thisHashtable
.boolean
isEmpty()
Returns whether thisHashtable
contains no elements.Enumeration<K>
keys()
Returns an enumeration on the keys of thisHashtable
instance.V
put(K key, V value)
Associate the specified value with the specified key in thisHashtable
.V
remove(Object key)
Removes the key/value pair with the specified key from thisHashtable
.int
size()
Returns the number of key/value pairs in thisHashtable
.String
toString()
Returns the string representation of thisHashtable
.
-
-
-
Constructor Detail
-
Hashtable
public Hashtable()
Constructs a newHashtable
using the default capacity and load factor.
-
Hashtable
public Hashtable(int capacity)
Constructs a newHashtable
using the specified capacity and the default load factor.- Parameters:
capacity
- the initial capacity.
-
-
Method Detail
-
clear
public void clear()
Removes all key/value pairs from thisHashtable
, leaving the size zero and the capacity unchanged.
-
isEmpty
public boolean isEmpty()
Returns whether thisHashtable
contains no elements.- Returns:
true
if thisHashtable
has no elements,false
otherwise.
-
containsKey
public boolean containsKey(Object key)
Returns true if thisHashtable
contains the specified object as a key of one of the key/value pairs.- Parameters:
key
- the object to look for as a key in thisHashtable
.- Returns:
true
if object is a key in thisHashtable
,false
otherwise.- See Also:
contains(java.lang.Object)
,Object.equals(java.lang.Object)
-
get
public V get(Object key)
Returns the value associated with the specified key in thisHashtable
.- Parameters:
key
- 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:
put(K, V)
-
put
public V put(K key, V value)
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.- Parameters:
key
- the key to add.value
- the value to add.- Returns:
- the old value associated with the specified key, or
null
if the key did not exist. - See Also:
elements()
,get(java.lang.Object)
,keys()
,Object.equals(java.lang.Object)
-
remove
public V remove(Object key)
Removes the key/value pair with the specified key from thisHashtable
.- Parameters:
key
- the key to remove.- Returns:
- the value associated with the specified key, or
null
if the specified key did not exist. - See Also:
get(java.lang.Object)
,put(K, V)
-
size
public int size()
Returns the number of key/value pairs in thisHashtable
.- Returns:
- the number of key/value pairs in this
Hashtable
. - See Also:
elements()
,keys()
-
contains
public boolean contains(Object value)
Returns true if thisHashtable
contains 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:
true
if object is a value in thisHashtable
,false
otherwise.- See Also:
containsKey(java.lang.Object)
,Object.equals(java.lang.Object)
-
elements
public Enumeration<V> elements()
Returns an enumeration on the values of thisHashtable
. The results of the Enumeration may be affected if the contents of thisHashtable
are modified.- Returns:
- an enumeration of the values of this
Hashtable
. - See Also:
keys()
,size()
,Enumeration
-
keys
public Enumeration<K> keys()
Returns an enumeration on the keys of thisHashtable
instance. The results of the enumeration may be affected if the contents of thisHashtable
are modified.- Returns:
- an enumeration of the keys of this
Hashtable
. - See Also:
elements()
,size()
,Enumeration
-
equals
public boolean equals(Object object)
Compares thisHashtable
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.- Overrides:
equals
in classObject
- Parameters:
object
- the object to compare with this object.- Returns:
true
if the specified object is equal to this Map,false
otherwise.- See Also:
hashCode()
-
hashCode
public int hashCode()
Returns the hash code for thisHashtable
. It is calculated by taking each element' hashcode.- Overrides:
hashCode
in classObject
- Returns:
- the hash code of the
Hashtable
. - See Also:
Object.equals(java.lang.Object)
-
-