Class java.util.Hashtable
All Packages This Package Previous Next
Class java.util.Hashtable
java.lang.Object
|
+----java.util.Hashtable
-
public class
Hashtable
-
extends Object
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(1));
numbers.put("three", new Integer(1));
To retrieve a number use:
Integer n = (Integer)numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}
-
See Also:
-
hashCode,
equals
-
Version:
-
1.21, 20 Apr 1995
-
Author:
-
Arthur van Hoff
-
Hashtable(int, float)
-
Construct a new, empty hashtable with the specified initial capacity
and the specified load factor.
-
Hashtable(int)
-
Constructs a new, empty hashtable with the specified initial capacity.
-
Hashtable()
-
Constructs a new, empty hashtable.
-
clear()
-
Clears the hash table so that it has no more elements in it.
-
clone()
-
Creates a clone of the hashtable.
-
contains(Object)
-
Returns true if the specified object is an element of the hashtable.
-
containsKey(Object)
-
Returns true if the collection contains an element for the key.
-
elements()
-
Returns an enumeration of the elements.
-
get(Object)
-
Gets the object associated with a key in the hashtable.
-
isEmpty()
-
Returns true if the hashtable contains no elements.
-
keys()
-
Returns an enumeration of the hashtable's keys.
-
put(Object, Object)
-
Puts the specified element into the hashtable, using the specified
key.
-
rehash()
-
Rehashes the content of the table into a bigger table.
-
remove(Object)
-
Removes the element corresponding to the key.
-
size()
-
Returns the hashtable's size (the number of elements it contains).
-
toString()
-
Converts to a rather lengthy string.
Hashtable
public Hashtable(int initialCapacity,
float loadFactor)
-
Construct a new, empty hashtable with the specified initial capacity
and the specified load factor.
-
Parameters:
-
initialCapacity
-
the initial number of buckets
-
loadFactor
-
a number between 0.0 and 1.0, it defines
the threshold for rehashing the hashtable into
a bigger one.
Hashtable
public Hashtable(int initialCapacity)
-
Constructs a new, empty hashtable with the specified initial capacity.
-
Parameters:
-
initialCapacity
-
the initial number of buckets
Hashtable
public Hashtable()
-
Constructs a new, empty hashtable. A default capacity and load factor
is used. Note that the hashtable will automatically grow when it gets
full.
size
public int size()
-
Returns the hashtable's size (the number of elements it contains).
isEmpty
public boolean isEmpty()
-
Returns true if the hashtable contains no elements.
keys
public synchronized Enumeration keys()
-
Returns an enumeration of the hashtable's keys.
-
See Also:
-
elements,
Enumeration
elements
public synchronized Enumeration elements()
-
Returns an enumeration of the elements. Use the Enumeration methods on
the returned object to fetch the elements sequentially.
-
See Also:
-
keys,
Enumeration
contains
public synchronized boolean contains(Object value)
-
Returns true if the specified object is an element of the hashtable.
This operation is more expensive than containsKey()
-
Parameters:
-
value
-
the value that we are looking for
-
See Also:
-
containsKey
containsKey
public synchronized boolean containsKey(Object key)
-
Returns true if the collection contains an element for the key.
-
Parameters:
-
key
-
the key that we are looking for
-
See Also:
-
contains
get
public synchronized Object get(Object key)
-
Gets the object associated with a key in the hashtable.
-
Returns:
-
the element for the key or null if the key
is not defined in the hash table.
-
See Also:
-
put
rehash
protected void rehash()
-
Rehashes the content of the table into a bigger table.
This is method called automatically when the hashtable's
size exeeds a threshold.
put
public synchronized Object put(Object key,
Object value)
-
Puts the specified element into the hashtable, using the specified
key. The element may be retrieved by doing a get() with the same key.
The key can't be null and the element can't be null.
-
See Also:
-
get
-
Returns:
-
the old value of the key, or null if it didn't have one
remove
public synchronized Object remove(Object key)
-
Removes the element corresponding to the key. Does nothing if the
key isn't present.
-
Parameters:
-
key
-
the key that needs to be removed
-
Returns:
-
the value of key, or null if the key was not found
clear
public synchronized void clear()
-
Clears the hash table so that it has no more elements in it.
clone
public synchronized Object clone()
-
Creates a clone of the hashtable. A shallow copy is made,
the keys and elements themselves are NOT cloned. This is a
relatively expensive operation.
-
Overrides:
-
clone in class Object
toString
public synchronized String toString()
-
Converts to a rather lengthy string.
-
Overrides:
-
toString in class Object
All Packages This Package Previous Next