Java Reference
In-Depth Information
You can find out how many elements are in the structure by using the size() method,
much as you did with the Vector class:
int size = look.size();
8
You also can check whether the structure is empty by using the isEmpty() method:
boolean isEmpty = look.isEmpty();
Hash Tables
The Hashtable class, which is derived from Dictionary , implements the Map interface
and provides a complete implementation of a key-mapped data structure. Hash tables
enable you to store data based on some type of key and have an efficiency defined by the
load factor of the table. The load factor is a floating-point number between 0.0 and 1.0
that determines how and when the hash table allocates space for more elements.
Like vectors, hash tables have a capacity, or an amount of allocated memory. Hash tables
allocate memory by comparing the current size of the table with the product of the
capacity and the load factor. If the size of the hash table exceeds this product, the table
increases its capacity by rehashing itself.
Load factors closer to 1.0 result in a more efficient use of memory at the expense of a
longer lookup time for each element. Similarly, load factors closer to 0.0 result in more
efficient lookups but tend to be more wasteful with memory. Determining the load factor
for your own hash tables depends on how you use each hash table and whether your pri-
ority is performance or memory efficiency.
You can create hash tables in any one of three ways. The first constructor creates a
default hash table:
Hashtable hash = new Hashtable();
The second constructor creates a hash table with the specified initial capacity:
Hashtable hash = new Hashtable(20);
Finally, the third constructor creates a hash table with the specified initial capacity and
load factor:
Hashtable hash = new Hashtable(20, 0.75F);
All the abstract methods defined in Map are implemented in the Hashtable class. In addi-
tion, the Hashtable class implements a few others that perform functions specific to sup-
porting hash tables. One of these is the clear() method, which clears a hash table of all
its keys and elements:
hash.clear();
Search WWH ::




Custom Search