|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object
|
+--java.util.Dictionary
|
+--java.util.Hashtable
Hashtable class. Maps keys to values. Any object can be used as a key and/or value.
To sucessfully store and retrieve objects from a hash table, the object used as the key must implement the hashCode() and equals() methods.
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:
Integer n = (Integer)numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}
Object.hashCode(),
Object.equals(java.lang.Object)| Constructor Summary | |
Hashtable()
Constructs a new, empty hashtable. |
|
Hashtable(int initialCapacity)
Constructs a new, empty hashtable with the specified initial capacity. |
|
Hashtable(int initialCapacity,
float loadFactor)
Constructs a new, empty hashtable with the specified initial capacity and the specified load factor. |
|
| Method Summary | |
void |
clear()
Clears the hash table so that it has no more elements in it. |
Object |
clone()
Creates a clone of the hashtable. |
boolean |
contains(Object value)
Returns true if the specified object is an element of the hashtable. |
boolean |
containsKey(Object key)
Returns true if the collection contains an element for the key. |
Enumeration |
elements()
Returns an enumeration of the elements. |
Object |
get(Object key)
Gets the object associated with the specified key in the hashtable. |
boolean |
isEmpty()
Returns true if the hashtable contains no elements. |
Enumeration |
keys()
Returns an enumeration of the hashtable's keys. |
Object |
put(Object key,
Object value)
Puts the specified element into the hashtable, using the specified key. |
protected void |
rehash()
Rehashes the content of the table into a bigger table. |
Object |
remove(Object key)
Removes the element corresponding to the key. |
int |
size()
Returns the number of elements contained in the hashtable. |
String |
toString()
Converts to a rather lengthy String. |
| Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
public Hashtable(int initialCapacity,
float loadFactor)
initialCapacity - the initial number of bucketsloadFactor - a number between 0.0 and 1.0, it defines
the threshold for rehashing the hashtable into
a bigger one.
IllegalArgumentException - If the initial capacity
is less than or equal to zero.
IllegalArgumentException - If the load factor is
less than or equal to zero.public Hashtable(int initialCapacity)
initialCapacity - the initial number of bucketspublic Hashtable()
| Method Detail |
public int size()
size in class Dictionarypublic boolean isEmpty()
isEmpty in class Dictionarypublic Enumeration keys()
keys in class Dictionaryelements(),
Enumerationpublic Enumeration elements()
elements in class Dictionarykeys(),
Enumerationpublic boolean contains(Object value)
value - the value that we are looking for
NullPointerException - If the value being searched
for is equal to null.containsKey(java.lang.Object)public boolean containsKey(Object key)
key - the key that we are looking forcontains(java.lang.Object)public Object get(Object key)
get in class Dictionarykey - the specified keyput(java.lang.Object, java.lang.Object)protected void rehash()
public Object put(Object key,
Object value)
put in class Dictionarykey - the specified key in the hashtablevalue - the specified element
NullPointerException - If the value of the element
is equal to null.get(java.lang.Object)public Object remove(Object key)
remove in class Dictionarykey - the key that needs to be removed
public void clear()
public Object clone()
clone in class Objectpublic String toString()
toString in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||