|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--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 Object.hashCode() and
Object.equals(java.lang.Object) methods.
A hashtable has two parameters that affect its efficiency: its
capacity and its load factor. The load factor should be
between 0.0 and 1.0. When the number of entries in the hashtable exceeds the
product of the load factor and the current capacity, the capacity is
increased by invoking rehash(). Larger load factors use memory more
efficiently, at the expense of larger expected time per lookup.
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);
}
This hashtable implementation is significantly more object-efficient than
the original JDK version. However, for hashtables mapping integers to
objects or integers to integers, see
IntHashtable and
IntIntHashtable.
| Constructor Summary | ||
Hashtable()
Construct a new, empty hashtable with a default capacity and load factor. |
||
Hashtable(int initialCapacity)
Construct a new, empty hashtable with the specified capacity. |
||
| Method Summary | ||
void |
clear()
Flush this hashtable. |
|
boolean |
contains(Object value)
Determines if some key maps into the specified value in this hashtable. |
|
boolean |
containsKey(Object key)
Determines if the specified object is a key in this hashtable. |
|
Enumeration |
elements()
Retrieves an enumeration of the values in this hashtable. |
|
Object |
get(Object key)
Retrieves value by key. |
|
boolean |
isEmpty()
Determines if this hashtable maps no keys to values. |
|
Enumeration |
keys()
Retrieves an enumeration of the keys in this hashtable. |
|
Object |
put(Object key,
Object value)
Maps value to key in this hashtable. |
|
protected void |
rehash()
Rehashes the contents of this hashtable. |
|
Object |
remove(Object key)
Removes value by key. |
|
int |
size()
Retrieves the number of keys in this hashtable. |
|
String |
toString()
Retrieves string representation of this hashtable. |
|
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
public Hashtable()
public Hashtable(int initialCapacity)
initialCapacity - Initial capacity of the hashtable.IllegalArgumentException - If the initial capacity is less than
zero| Method Detail |
public void clear()
Invoke this method to remove all keys and values from this hashtable.
public boolean contains(Object value)
This operation is more expensive than containsKey(java.lang.Object).
value - Value to search for.NullPointerException - If null value provided.public boolean containsKey(Object key)
key - Key to test.contains(java.lang.Object)public Enumeration elements()
Use the Enumeration methods on the returned object to fetch the elements sequentially.
Enumeration,
keys()public Object get(Object key)
key - Key in this hashtable.public boolean isEmpty()
public Enumeration keys()
Enumeration,
elements()public Object put(Object key, Object value)
The value can be retrieved by invoking get(java.lang.Object) with a key that
is equal to the original key (as determined by Object.equals(java.lang.Object).
key - Key to associate with the value.value - Value to put into table.NullPointerException - If null key or null value.protected void rehash()
Invoke this method to rehash this hashtable into one with a larger capacity. This hashtable invokes this method automatically when the number of keys exceeds its capacity and load factor.
public Object remove(Object key)
If the key does not exist in this hashtable, then this method does nothing.
key - Key for value to remove.public int size()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
Copyright 1999-2007 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Copyright 1993-2003 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
Copyright 2002-2003 Nokia Corporation All Rights Reserved.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.