java.lang.Object | +--java.util.Dictionary | +--java.util.Hashtable
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
performance: initial capacity and load factor. The
capacity is the number of buckets in the hash table, and the
initial capacity is simply the capacity at the time the hash table
is created. Note that the hash table is open: in the case a "hash
collision", a single bucket stores multiple entries, which must be searched
sequentially. The load factor is a measure of how full the hash
table is allowed to get before its capacity is automatically increased.
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.
Generally, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the time cost to look up an entry (which is reflected in most Hashtable operations, including get and put).
The initial capacity controls a tradeoff between wasted space and the
need for rehash operations, which are time-consuming.
No rehash operations will ever occur if the initial
capacity is greater than the maximum number of entries the
Hashtable will contain divided by its load factor. However,
setting the initial capacity too high can waste space.
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);
}
As of the Java 2 platform v1.2, this class has been retrofitted to implement Map, so that it becomes a part of Java's collection framework. Unlike the new collection implementations, Hashtable is synchronized.
The Iterators returned by the iterator and listIterator methods of the Collections returned by all of Hashtable's "collection view methods" are fail-fast: if the Hashtable is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by Hashtable's keys and values methods are not fail-fast.
Object.equals(java.lang.Object),
Object.hashCode(),
rehash(),
Collection,
Map,
HashMap,
TreeMap, Serialized Form
| Inner classes inherited from class java.util.Map |
Map.Entry |
| Constructor Summary | |
Hashtable
copy-> new Hashtable()copy-> <Hashtable var> = new Hashtable();
|
|
Hashtable
copy-> new Hashtable( )copy-> <Hashtable var> = new Hashtable(<int initialCapacity>);
|
|
Hashtable
copy-> new Hashtable(, )copy-> <Hashtable var> = new Hashtable(<int initialCapacity>, <float loadFactor>);
|
|
Hashtable
copy-> new Hashtable( )copy-> <Hashtable var> = new Hashtable(<Map t>);
|
|
| Method Summary | |
void |
clear()
copy-> .clear()copy-> <Hashtable>.clear();
|
Object |
clone()
copy-> .clone()copy-> <Object var>=<Hashtable>.clone(); |
boolean |
contains(Object value)
copy-> .contains( )copy-> <boolean var>=<Hashtable>.contains(<Object value>);
|
boolean |
containsKey(Object key)
copy-> .containsKey( )copy-> <boolean var>=<Hashtable>.containsKey(<Object key>);
|
boolean |
containsValue(Object value)
copy-> .containsValue( )copy-> <boolean var>=<Hashtable>.containsValue(<Object value>);
|
Enumeration |
elements()
copy-> .elements()copy-> <Enumeration var>=<Hashtable>.elements();
|
Set |
entrySet()
copy-> .entrySet()copy-> <Set var>=<Hashtable>.entrySet();
|
boolean |
equals(Object o)
copy-> .equals( )copy-> <boolean var>=<Hashtable>.equals(<Object o>);
|
Object |
get(Object key)
copy-> .get( )copy-> <Object var>=<Hashtable>.get(<Object key>);
|
int |
hashCode()
copy-> .hashCode()copy-> <int var>=<Hashtable>.hashCode();
|
boolean |
isEmpty()
copy-> .isEmpty()copy-> <boolean var>=<Hashtable>.isEmpty();
|
Enumeration |
keys()
copy-> .keys()copy-> <Enumeration var>=<Hashtable>.keys();
|
Set |
keySet()
copy-> .keySet()copy-> <Set var>=<Hashtable>.keySet();
|
Object |
put(Object key,
Object value)
copy-> .put(, )copy-> <Object var>=<Hashtable>.put(<Object key>, <Object value>);
|
void |
putAll(Map t)
copy-> .putAll( )copy-> <Hashtable>.putAll(<Map t>); |
protected void |
rehash()
copy-> .rehash()copy-> <Hashtable>.rehash();
|
Object |
remove(Object key)
copy-> .remove( )copy-> <Object var>=<Hashtable>.remove(<Object key>);
|
int |
size()
copy-> .size()copy-> <int var>=<Hashtable>.size();
|
String |
toString()
copy-> .toString()copy-> <String var>=<Hashtable>.toString();
|
Collection |
values()
copy-> .values()copy-> <Collection var>=<Hashtable>.values();
|
| Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |