This class implements a hashtable, which maps keys to values. Any
non-null object can be used as a key or as a value.
To successfully store and retrieve objects from a hashtable, the
objects used as keys must implement the hashCode
method and the equals method.
An instance of Hashtable has two parameters that
affect its efficiency: its capacity and its load
factor. The load factor in the CLDC implementation of
the hashtable class is always 75 percent. When the number
of entries in the hashtable exceeds the product of the
load factor and the current capacity, the capacity is increased
by calling the rehash method.
If many entries are to be made into a Hashtable ,
creating it with a sufficiently large capacity may allow the
entries to be inserted more efficiently than letting it perform
automatic rehashing as needed to grow the table.
This example creates a hashtable of numbers. It uses the names of
the numbers as keys:
Hashtable numbers = new Hashtable();
numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));
To retrieve a number, use the following code:
Integer n = (Integer)numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}
version: 12/17/01 (CLDC 1.1) See Also: java.lang.Object.equals(java.lang.Object) See Also: java.lang.Object.hashCode See Also: java.util.Hashtable.rehash since: JDK1.0, CLDC 1.0 |