Java Hashtable Class
Hashtable is part of the original java.util and is a concrete implementation of a Dictionary.
However, Java 2 reengineered Hashtable to implement the Map interface, so it is now integrated into the collections framework. It is similar to HashMap, but is synchronized.
Like HashMap, Hashtable stores key/value pairs in a hash table. When using a hash table, 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.
Hashtable defines four constructors. The first is the default constructor:
Hashtable()
The second constructor creates a hash table of a specified size:
Hashtable(int size)
The third constructor creates a hash table of a specified size and a specified fill ratio.
The fill ratio must be between 0.0 and 1.0, and it determines how full the hash table can be before it is resized upward:
Hashtable(int size, float fillRatio)
The fourth constructor creates a hash table that initially contains the elements in m.
The capacity of the hash table is set to twice the number of elements in m.
Hashtable(Map m)
In addition to the methods defined by the Map interface, Hashtable defines the following methods:
序号 | 方法描述 |
---|---|
1 | void clear() <br> Empties this hash table so that it contains no keys. |
2 | Object clone() <br> Creates a shallow copy of this hash table. |
3 | boolean contains(Object value) <br> Tests if some key maps into the specified value in this hash table. |
4 | boolean containsKey(Object key) <br> Tests if the specified object is a key in this hash table. |
5 | boolean containsValue(Object value) <br> Returns true if this Hashtable maps one or more keys to this value. |
6 | Enumeration elements() <br> Returns an enumeration of the values in this hash table. |
7 | Object get(Object key) <br> 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() <br> Tests if this hash table maps no keys to values. |
9 | Enumeration keys() <br> Returns an enumeration of the keys in this hash table. |
10 | Object put(Object key, Object value) <br> Maps the specified key to the specified value in this hash table. |
11 | void rehash() <br> 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) <br> Removes the key (and its corresponding value) from this hash table. |
13 | int size() <br> Returns the number of keys in this hash table. |
14 | String toString() <br> 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 and space). |
Example
The following program illustrates several of the 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));
// 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"));
}
}
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 above example compiles and runs as follows:
Qadir: -19.08
Zara: 3434.34
Mahnaz: 123.22
Daisy: 99.22
Ayan: 1378.0
Zara's new balance: 4434.34