Packages  This Package  Prev  Next  Index  

§3.4 Class Hashtable

public  class  java.util.Hashtable
    extends  java.util.Dictionary  (I-§3.3)
    implements java.lang.Cloneable  (I-§1.22)
{
        // Constructors
    public Hashtable();	§3.4.1
    public Hashtable(int  initialCapacity);	§3.4.2
    public Hashtable(int  initialCapacity, float  loadFactor);	§3.4.3

        // Methods
    public void clear();	§3.4.4
    public Object clone();	§3.4.5
    public boolean contains(Object  value);	§3.4.6
    public boolean containsKey(Object  key);	§3.4.7
    public Enumeration elements();	§3.4.8
    public Object get(Object  key);	§3.4.9
    public boolean isEmpty();	§3.4.10
    public Enumeration keys();	§3.4.11
    public Object put(Object  key, Object  value);	§3.4.12
    protected void rehash();	§3.4.13
    public Object remove(Object  key);	§3.4.14
    public int size();	§3.4.15
    public String toString();	§3.4.16
}
This class implements a hash table, 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 hash table, the objects used as keys must implement the hashCode method (I-§1.12.6) and the equals method (I-§1.12.3).

An instance of 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 hash table exceeds the product of the load factor and the current capacity, the capacity is increased by calling the rehash method (I-§3.4.13). 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:

To retrieve a number use the following code:


Constructors

Hashtable

public Hashtable()
Constructs a new empty hashtable.

Hashtable

public Hashtable(int initialCapacity)
Constructs a new, empty hash table with the specified initial capacity.
Parameters:
initialCapacity - the initial capacity of the hash table

Hashtable

public
Hashtable(int initialCapacity, float loadFactor)
Constructs a new, empty hashtable with the specified initial capacity and the specified load factor.
Parameters:
initialCapacity - the initial size of the hashtable
loadFactor - a number between 0.0 and 1.0,
Throws
IllegalArgumentException (I-§1.32)
If the initial capacity is less than or equal to zero, or if the load factor is less than or equal to zero.

Methods

clear

public void clear()
Clears this hash table so that it contains no keys.

clone

public Object clone()
Creates a shallow copy of this hash table. The keys and values themselves are not cloned.
Returns:
a clone of the hash table.
Overrides:
clone in class Object (I-§1.12.2).

contains

public boolean contains(Object value)
Parameters:
value - a value to search for
Returns:
true if some key maps to the value argument in this hash table; false otherwise.
Throws
NullPointerException (I-§1.40)
If the value is null.
See Also:
containsKey (I-§3.4.7).

containsKey

public boolean containsKey(Object key)
Parameters:
key - possible key
Returns:
true if the specified object is a key in this hash table; false otherwise.
See Also:
contains (I-§3.4.6).

elements

public Enumeration elements()
Returns:
an enumeration (I-§3.11) of the values in this hash table.
Overrides:
elements in class Dictionary (I-§3.3.2).
See Also:
keys (I-§3.4.11).

get

public Object get(Object key)
Parameters:
key - a key in the hash table
Returns:
the value to which the key is mapped in this hash table; null if the key is not mapped to any value is this hash table.
Overrides:
get in class Dictionary (I-§3.3.3).
See Also:
put (I-§3.4.12).

isEmpty

public boolean isEmpty()
Returns:
true if this hash table maps no keys to values; false otherwise.
Overrides:
isEmpty in class Dictionary (I-§3.3.4).

keys

public Enumeration keys()
Returns:
an enumeration (I-§3.11) of the keys in this hash table.
Overrides:
keys in class Dictionary (I-§3.3.5).
See Also:
elements (I-§3.4.8).

put

public Object put(Object key, Object value)
Maps the specified key to the specified value in this hash table. Neither the key nor the value can be null.
The value can be retrieved by calling the the get method (I-§3.4.9) with a key that is equal (I-§1.12.3) to the original key.

Parameters:
key - the hashtable key
value - the value
Returns:
the previous value of of the specified key in this hash table, or null if it did not have one.
Throws
NullPointerException (I-§1.40)
If the key or value is null.
Overrides:
put in class Dictionary (I-§3.3.6).

rehash

protected void rehash()
Rehashes the contents of the hash table into a hash table with a larger capacity. This method is called automatically when the number of keys in the hash table exceeds this hash table's capacity and load factor.

remove

public Object remove(Object key)
Removes the key (and its corresponding value) from this hash table. This method does nothing if the key is not in the hash table.
Parameters:
key - the key that needs to be removed
Returns:
the value to which the key had been mapped in this hash table, or null if the key did not have a mapping.
Overrides:
remove in class Dictionary (I-§3.3.7).

size

public int size()
Returns:
the number of keys in this hash table.
Overrides:
size in class Dictionary (I-§3.3.8).

toString

public String toString()
Returns:
a string representation of this hash table.
Overrides:
toString in class Object (I-§1.12.9).

Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com