Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
To add a key and a value, use theput( )method. Useget( )to retrieve the value of a given
key. The keys and values can each be returned as anEnumerationby thekeys( )andelements( )
methods, respectively. Thesize( )method returns the number of key/value pairs stored in a
dictionary, andisEmpty( )returnstruewhen the dictionary is empty. You can use theremove( )
method to delete a key/value pair.

REMEMBEREMEMBER TheDictionaryclass is obsolete. You should implement theMapinterface to obtain
key/value storage functionality.

Hashtable


Hashtablewas part of the originaljava.utiland is a concrete implementation of aDictionary.
However, with the advent of collections,Hashtablewas reengineered to also implement the
Mapinterface. Thus,Hashtableis now integrated into the Collections Framework. It is similar
toHashMap, but is synchronized.
LikeHashMap,Hashtablestores key/value pairs in a hash table. However, neither keys
nor values can benull. When using aHashtable, you specify an object that is used as a key,
and the value that you want linked to that key. The key is then hashed, and the resulting
hash code is used as the index at which the value is stored within the table.
Hashtablewas made generic by JDK 5. It is declared like this:

class Hashtable<K, V>

Here,Kspecifies the type of keys, andVspecifies the type of values.
A hash table can only store objects that override thehashCode( )andequals( )methods
that are defined byObject. ThehashCode( )method must compute and return the hash code
for the object. Of course,equals( )compares two objects. Fortunately, many of Java’s built-in
classes already implement thehashCode( )method. For example, the most common type of
Hashtableuses aStringobject as the key.Stringimplements bothhashCode( )andequals( ).
TheHashtableconstructors are shown here:

Hashtable( )
Hashtable(intsize)
Hashtable(intsize, floatfillRatio)
Hashtable(Map<? extends K,? extends V>m)

The first version is the default constructor. The second version creates a hash table that has
an initial size specified bysize.(The default size is 11.) The third version creates a hash table that
has an initial size specified bysizeand a fill ratio specified byfillRatio.This ratio must be
between 0.0 and 1.0, and it determines how full the hash table can be before it is resized
upward. Specifically, when the number of elements is greater than the capacity of the hash
table multiplied by its fill ratio,the hash table is expanded. If you do not specify a fill ratio,
then 0.75 is used. Finally, the fourth version creates a hash table that is initialized with the
elements inm.The capacityof the hash table is set to twice the number of elements inm.
The default load factor of 0.75 is used.
In addition to the methods defined by theMapinterface, whichHashtablenow
implements,Hashtabledefines the legacy methods listed in Table 17-18. Several methods
throwNullPointerExceptionif an attempt is made to use anullkey or value.

494 Part II: The Java Library

Free download pdf