Java Hashtable Class
Hashtable is part of the original java.util and is a concrete implementation of Dictionary.
However, the Hashtable refactored in Java 2 implements the Map interface, so Hashtable is now integrated into the collections framework. It is very similar to the HashMap class, but it supports synchronization.
Like HashMap, Hashtable stores key/value pairs in a hash table. When using a hash table, you must specify the object to be used as the key and the value to be linked to that key.
Then the key is hashed, and the resulting hash code is used as the index for storing the value in the table.
Hashtable defines four constructors. The first is the default constructor:
Hashtable()
The second constructor creates a hash table of the specified size:
Hashtable(int size)
The third constructor creates a hash table of the specified size and specifies the fill ratio through fillRatio.
The fill ratio must be between 0.0 and 1.0; it determines how full the hash table becomes before it is resized:
Hashtable(int size,float fillRatio)
The fourth constructor creates a hash table initialized with the elements in M.
The capacity of the hash table is set to twice that of M.
Hashtable(Map m)
In addition to the methods defined in the Map interface, Hashtable also defines the following methods:
| No. | Method Description |
|---|---|
| 1 | void clear( ) |
| Clears this hash table so that it contains no keys. | |
| 2 | Object clone( ) |
| Creates a shallow copy of this hash table. | |
| 3 | boolean contains(Object value) |
| Tests whether there is a key associated with the specified value in this map. | |
| 4 | boolean containsKey(Object key) |
| Tests whether the specified object is a key in this hash table. | |
| 5 | boolean containsValue(Object value) |
| Returns true if this Hashtable maps one or more keys to this value. | |
| 6 | Enumeration elements( ) |
| Returns an enumeration of the values in this hash table. | |
| 7 | Object get(Object key) |
| Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. More precisely, if this map contains a mapping from key k to value v such that (key.equals(k)), this method returns v; otherwise it returns null. | |
| 8 | boolean isEmpty( ) |
| Tests whether this hash table has no key mapped to a value. | |
| 9 | Enumeration keys( ) |
| Returns an enumeration of the keys in this hash table. | |
| 10 | Object put(Object key, Object value) |
| Maps the specified key to the specified value in this hash table. | |
| 11 | void rehash( ) |
| Increases the capacity of this hash table and reorganizes it internally so as to accommodate and access its elements more efficiently. | |
| 12 | Object remove(Object key) |
| Removes the key and its corresponding value from the hash table. | |
| 13 | int size( ) |
| Returns the number of keys in this hash table. | |
| 14 | String toString( ) |
| Returns a string representation of this Hashtable object, in the form of a set of entries enclosed in braces and separated by the ASCII characters ", " (comma followed by space). |
Example
The following program illustrates several methods supported by this data structure:
import java.util.*;
public class HashTableDemo {
public static void main(String args[]) {
// Create a hash map
Hashtable balance = new Hashtable();
Enumeration names;
String str;
double bal;
balance.put("Zara", new Double(3434.34));
balance.put("Mahnaz", new Double(123.22));
balance.put("Ayan", new Double(1378.00));
balance.put("Daisy", new Double(99.22));
balance.put("Qadir", new Double(-19.08));
// Show all balances in hash table.
names = balance.keys();
while(names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ": " +
balance.get(str));
}
System.out.println();
// Deposit 1,000 into Zara's account
bal = ((Double)balance.get("Zara")).doubleValue();
balance.put("Zara", new Double(bal+1000));
System.out.println("Zara's new balance: " +
balance.get("Zara"));
}
}
The compilation and execution results of the above example are as follows:
Qadir: -19.08
Zara: 3434.34
Mahnaz: 123.22
Daisy: 99.22
Ayan: 1378.0
Zara's new balance: 4434.34
Java Data Structures