Hashtable is part of the original java.util and is a concrete implementation of the Dictionary.
However, Java 2 refactored Hashtable to implement the Map interface, thus integrating Hashtable into the collections framework. It is similar to the HashMap class but supports synchronization.
Like HashMap, Hashtable stores key/value pairs in a hash table. When using a hash table, you specify the object to be used as a key, and the value to be linked to that key.
The key is then hashed, and the resulting hash code is used as the index for the value stored 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 a fill ratio. The fill ratio must be between 0.0 and 1.0, and it determines how full the hash table can become 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 the size of M.
Hashtable(Map m)
Hashtable defines the following methods in addition to those defined by the Map interface:
| No. | Method Description |
|---|---|
| 1 | void clear( ) Empties 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 some key maps to the specified value in this hashtable. |
| 4 | boolean containsKey(Object key) Tests if the specified object is a key in this hashtable. |
| 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 formally, if this map contains a mapping from a key k to a value v such that (key.equals(k)), then this method returns v; otherwise, it returns null. |
| 8 | boolean isEmpty( ) Tests if this hash table maps no keys to values. |
| 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 hashtable. |
| 11 | void rehash( ) Increases the capacity of and internally reorganizes this hash table, in order to accommodate and access its entries more efficiently. |
| 12 | Object remove(Object key) Removes the key (and its corresponding value) from this hashtable. |
| 13 | int size( ) Returns the number of keys in this hashtable. |
| 14 | String toString( ) Returns a string representation of this Hashtable object, as a comma-separated list of entries enclosed in braces and separated by ASCII characters " , " (comma and 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
YouTip