Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.util

Class Hashtable

java.lang.Object
|
+--java.util.Dictionary
   |
   +--java.util.Hashtable

All Implemented Interfaces:

Map, Cloneable, Serializable


public class Hashtable

extends Dictionary

implements Map, Cloneable, Serializable

A class which implements a hashtable data structure.

This implementation of Hashtable uses a hash-bucket approach. That is: linear probing and rehashing is avoided; instead, each hashed value maps to a simple linked-list which, in the best case, only has one node. Assuming a large enough table, low enough load factor, and / or well implemented hashCode() methods, Hashtable should provide O(1) insertion, deletion, and searching of keys. Hashtable is O(n) in the worst case for all of these (if all keys hash to the same bucket).

This is a JDK-1.2 compliant implementation of Hashtable. As such, it belongs, partially, to the Collections framework (in that it implements Map). For backwards compatibility, it inherits from the obsolete and utterly useless Dictionary class.

Being a hybrid of old and new, Hashtable has methods which provide redundant capability, but with subtle and even crucial differences. For example, one can iterate over various aspects of a Hashtable with either an Iterator (which is the JDK-1.2 way of doing things) or with an Enumeration. The latter can end up in an undefined state if the Hashtable changes while the Enumeration is open.

Unlike HashMap, Hashtable does not accept `null' as a key value. Also, all accesses are synchronized: in a single thread environment, this is expensive, but in a multi-thread environment, this saves you the effort of extra synchronization. However, the old-style enumerators are not synchronized, because they can lead to unspecified behavior even if they were synchronized. You have been warned.

The iterators are fail-fast, meaning that any structural modification, except for remove() called on the iterator itself, cause the iterator to throw a ConcurrentModificationException rather than exhibit non-deterministic behavior.

Since:Authors:See Also:

Constructor Summary

Hashtable()

Construct a new Hashtable with the default capacity (11) and the default load factor (0.75).
Hashtable(java.util.Map m)

Construct a new Hashtable from the given Map, with initial capacity the greater of the size of m or the default of 11.
Hashtable(int initialCapacity)

Construct a new Hashtable with a specific inital capacity and default load factor of 0.75.
Hashtable(int initialCapacity, float loadFactor)

Construct a new Hashtable with a specific initial capacity and load factor.

Method Summary

synchronized voidclear()

Clears the hashtable so it has no keys.
synchronized java.lang.Objectclone()

Returns a shallow clone of this Hashtable.
synchronized booleancontains(java.lang.Object value)

Returns true if this Hashtable contains a value o, such that o.equals(value).
synchronized booleancontainsKey(java.lang.Object key)

Returns true if the supplied object equals() a key in this Hashtable.
booleancontainsValue(java.lang.Object value)

Returns true if this Hashtable contains a value o, such that o.equals(value).
java.util.Enumerationelements()

Return an enumeration of the values of this table.
java.util.SetentrySet()

Returns a "set view" of this Hashtable's entries.
booleanequals(java.lang.Object o)

Returns true if this Hashtable equals the supplied Object o.
synchronized java.lang.Objectget(java.lang.Object key)

Return the value in this Hashtable associated with the supplied key, or null if the key maps to nothing.
synchronized inthashCode()

Returns the hashCode for this Hashtable.
synchronized booleanisEmpty()

Returns true if there are no key-value mappings currently in this table.
java.util.SetkeySet()

Returns a "set view" of this Hashtable's keys.
java.util.Enumerationkeys()

Return an enumeration of the keys of this table.
synchronized java.lang.Objectput(java.lang.Object key, java.lang.Object value)

Puts the supplied value into the Map, mapped by the supplied key.
synchronized voidputAll(java.util.Map m)

Copies all elements of the given map into this hashtable.
voidrehash()

Increases the size of the Hashtable and rehashes all keys to new array indices; this is called when the addition of a new value would cause size() > threshold.
synchronized java.lang.Objectremove(java.lang.Object key)

Removes from the table and returns the value which is mapped by the supplied key.
synchronized intsize()

Returns the number of key-value mappings currently in this hashtable.
synchronized java.lang.StringtoString()

Converts this Hashtable to a String, surrounded by braces, and with key/value pairs listed with an equals sign between, separated by a comma and space.
java.util.Collectionvalues()

Returns a "collection view" (or "bag view") of this Hashtable's values.

Constructor Details

Hashtable

public Hashtable()

Construct a new Hashtable with the default capacity (11) and the default load factor (0.75).


Hashtable

public Hashtable(int initialCapacity)

Construct a new Hashtable with a specific inital capacity and default load factor of 0.75.

Parameters:

Throws:


Hashtable

public Hashtable(int initialCapacity, float loadFactor)

Construct a new Hashtable with a specific initial capacity and load factor.

Parameters:

Throws:


Hashtable

public Hashtable(java.util.Map m)

Construct a new Hashtable from the given Map, with initial capacity the greater of the size of m or the default of 11.

Every element in Map m will be put into this new Hashtable.

Since:Parameters:

Throws:


Method Details

clear

public synchronized void clear()

Clears the hashtable so it has no keys. This is O(1).


clone

public synchronized Object clone()

Returns a shallow clone of this Hashtable. The Map itself is cloned, but its contents are not. This is O(n).

Returns:


contains

public synchronized boolean contains(java.lang.Object value)

Returns true if this Hashtable contains a value o, such that o.equals(value). This is the same as containsValue(), and is O(n).

Parameters:

Returns:

Throws:

See Also:


containsKey

public synchronized boolean containsKey(java.lang.Object key)

Returns true if the supplied object equals() a key in this Hashtable.

Parameters:

Returns:

Throws:

See Also:


containsValue

public boolean containsValue(java.lang.Object value)

Returns true if this Hashtable contains a value o, such that o.equals(value). This is the new API for the old contains().

Since:Parameters:

Returns:

Throws:

See Also:


elements

public Enumeration elements()

Return an enumeration of the values of this table. There's no point in synchronizing this, as you have already been warned that the enumeration is not specified to be thread-safe.

Returns:

See Also:


entrySet

public Set entrySet()

Returns a "set view" of this Hashtable's entries. The set is backed by the hashtable, so changes in one show up in the other. The set supports element removal, but not element addition. The set is properly synchronized on the original hashtable. Sun has not documented the proper interaction of null with this set, but has inconsistent behavior in the JDK. Therefore, in this implementation, contains, remove, containsAll, retainAll, removeAll, and equals just ignore a null entry, or an entry with a null key or value, rather than throwing a NullPointerException. However, calling entry.setValue(null) will fail.

Note that the iterators for all three views, from keySet(), entrySet(), and values(), traverse the hashtable in the same sequence.

Since:Returns:

See Also:


equals

public boolean equals(java.lang.Object o)

Returns true if this Hashtable equals the supplied Object o. As specified by Map, this is: (o instanceof Map) && entrySet().equals(((Map) o).entrySet());

Since:Parameters:

Returns:


get

public synchronized Object get(java.lang.Object key)

Return the value in this Hashtable associated with the supplied key, or null if the key maps to nothing.

Parameters:

Returns:

Throws:

See Also:


hashCode

public synchronized int hashCode()

Returns the hashCode for this Hashtable. As specified by Map, this is the sum of the hashCodes of all of its Map.Entry objects

Since:Returns:


isEmpty

public synchronized boolean isEmpty()

Returns true if there are no key-value mappings currently in this table.

Returns:


keySet

public Set keySet()

Returns a "set view" of this Hashtable's keys. The set is backed by the hashtable, so changes in one show up in the other. The set supports element removal, but not element addition. The set is properly synchronized on the original hashtable. Sun has not documented the proper interaction of null with this set, but has inconsistent behavior in the JDK. Therefore, in this implementation, contains, remove, containsAll, retainAll, removeAll, and equals just ignore a null key rather than throwing a NullPointerException.

Since:Returns:

See Also:


keys

public Enumeration keys()

Return an enumeration of the keys of this table. There's no point in synchronizing this, as you have already been warned that the enumeration is not specified to be thread-safe.

Returns:

See Also:


put

public synchronized Object put(java.lang.Object key, java.lang.Object value)

Puts the supplied value into the Map, mapped by the supplied key. Neither parameter may be null. The value may be retrieved by any object which equals() this key.

Parameters:

Returns:

Throws:

See Also:


putAll

public synchronized void putAll(java.util.Map m)

Copies all elements of the given map into this hashtable. However, no mapping can contain null as key or value. If this table already has a mapping for a key, the new mapping replaces the current one.

Parameters:

Throws:


rehash

protected void rehash()

Increases the size of the Hashtable and rehashes all keys to new array indices; this is called when the addition of a new value would cause size() > threshold. Note that the existing Entry objects are reused in the new hash table.

This is not specified, but the new size is twice the current size plus one; this number is not always prime, unfortunately. This implementation is not synchronized, as it is only invoked from synchronized methods.


remove

public synchronized Object remove(java.lang.Object key)

Removes from the table and returns the value which is mapped by the supplied key. If the key maps to nothing, then the table remains unchanged, and null is returned.

Parameters:

Returns:


size

public synchronized int size()

Returns the number of key-value mappings currently in this hashtable.

Returns:


toString

public synchronized String toString()

Converts this Hashtable to a String, surrounded by braces, and with key/value pairs listed with an equals sign between, separated by a comma and space. For example, "{a=1, b=2}".

NOTE: if the toString() method of any key or value throws an exception, this will fail for the same reason.

Returns:


values

public Collection values()

Returns a "collection view" (or "bag view") of this Hashtable's values. The collection is backed by the hashtable, so changes in one show up in the other. The collection supports element removal, but not element addition. The collection is properly synchronized on the original hashtable. Sun has not documented the proper interaction of null with this set, but has inconsistent behavior in the JDK. Therefore, in this implementation, contains, remove, containsAll, retainAll, removeAll, and equals just ignore a null value rather than throwing a NullPointerException.

Since:Returns:

See Also: