java.util

Class Hashtable<K,V>

Implemented Interfaces:
Cloneable, Map<K,V>, Serializable
Known Direct Subclasses:
Properties, UIDefaults

public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, 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:
1.0
See Also:
HashMap, TreeMap, IdentityHashMap, LinkedHashMap, Serialized Form

Constructor Summary

Hashtable()
Construct a new Hashtable with the default capacity (11) and the default load factor (0.75).
Hashtable(extends K, V> 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

Set
V>> entrySet()
Returns a "set view" of this Hashtable's entries.
void
clear()
Clears the hashtable so it has no keys.
Object
clone()
Returns a shallow clone of this Hashtable.
boolean
contains(Object value)
Returns true if this Hashtable contains a value o, such that o.equals(value).
boolean
containsKey(Object key)
Returns true if the supplied object equals() a key in this Hashtable.
boolean
containsValue(Object value)
Returns true if this Hashtable contains a value o, such that o.equals(value).
Enumeration
elements()
Return an enumeration of the values of this table.
boolean
equals(Object o)
Returns true if this Hashtable equals the supplied Object o.
V
get(Object key)
Return the value in this Hashtable associated with the supplied key, or null if the key maps to nothing.
int
hashCode()
Returns the hashCode for this Hashtable.
boolean
isEmpty()
Returns true if there are no key-value mappings currently in this table.
Set
keySet()
Returns a "set view" of this Hashtable's keys.
Enumeration
keys()
Return an enumeration of the keys of this table.
V
put(K key, V value)
Puts the supplied value into the Map, mapped by the supplied key.
void
putAll(extends K, V> m)
Copies all elements of the given map into this hashtable.
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.
V
remove(Object key)
Removes from the table and returns the value which is mapped by the supplied key.
int
size()
Returns the number of key-value mappings currently in this hashtable.
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.
Collection
values()
Returns a "collection view" (or "bag view") of this Hashtable's values.

Methods inherited from class java.util.Dictionary<K,V>

elements, get, isEmpty, keys, put, remove, size

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

Hashtable

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

Hashtable

public Hashtable(extends K,
                 V> 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.

Parameters:
m - a Map whose key / value pairs will be put into the new Hashtable. NOTE: key / value pairs are not cloned in this constructor.
Throws:
NullPointerException - if m is null, or if m contains a mapping to or from `null'.
Since:
1.2

Hashtable

public Hashtable(int initialCapacity)
Construct a new Hashtable with a specific inital capacity and default load factor of 0.75.
Parameters:
initialCapacity - the initial capacity of this Hashtable (>= 0)
Throws:
IllegalArgumentException - if (initialCapacity < 0)

Hashtable

public Hashtable(int initialCapacity,
                 float loadFactor)
Construct a new Hashtable with a specific initial capacity and load factor.
Parameters:
initialCapacity - the initial capacity (>= 0)
loadFactor - the load factor (> 0, not NaN)
Throws:
IllegalArgumentException - if (initialCapacity < 0) || ! (loadFactor > 0.0)

Method Details

V>> entrySet

public SetV>> 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.

Specified by:
V>> entrySet in interface Map<K,V>
Returns:
a set view of the entries
Since:
1.2

clear

public void clear()
Clears the hashtable so it has no keys. This is O(1).
Specified by:
clear in interface Map<K,V>

clone

public Object clone()
Returns a shallow clone of this Hashtable. The Map itself is cloned, but its contents are not. This is O(n).
Overrides:
clone in interface Object
Returns:
the clone

contains

public boolean contains(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:
value - the value to search for in this Hashtable
Returns:
true if at least one key maps to the value
Throws:
NullPointerException - if value is null

containsKey

public boolean containsKey(Object key)
Returns true if the supplied object equals() a key in this Hashtable.
Specified by:
containsKey in interface Map<K,V>
Parameters:
key - the key to search for in this Hashtable
Returns:
true if the key is in the table
Throws:
NullPointerException - if key is null

containsValue

public boolean containsValue(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().
Specified by:
containsValue in interface Map<K,V>
Parameters:
value - the value to search for in this Hashtable
Returns:
true if at least one key maps to the value
Throws:
NullPointerException - if value is null
Since:
1.2

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.
Overrides:
elements in interface Dictionary<K,V>
Returns:
the values
See Also:
keys(), values()

equals

public boolean equals(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());
Specified by:
equals in interface Map<K,V>
Overrides:
equals in interface Object
Parameters:
o - the object to compare to
Returns:
true if o is an equal map
Since:
1.2

get

public V get(Object key)
Return the value in this Hashtable associated with the supplied key, or null if the key maps to nothing.
Specified by:
get in interface Map<K,V>
Overrides:
get in interface Dictionary<K,V>
Parameters:
key - the key for which to fetch an associated value
Returns:
what the key maps to, if present
Throws:
NullPointerException - if key is null
See Also:
put(Object, Object), containsKey(Object)

hashCode

public 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
Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in interface Object
Returns:
the sum of the hashcodes of the entries
Since:
1.2

isEmpty

public boolean isEmpty()
Returns true if there are no key-value mappings currently in this table.
Specified by:
isEmpty in interface Map<K,V>
Overrides:
isEmpty in interface Dictionary<K,V>
Returns:
size() == 0

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.
Specified by:
keySet in interface Map<K,V>
Returns:
a set view of the keys
Since:
1.2
See Also:
values(), entrySet()

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.
Overrides:
keys in interface Dictionary<K,V>
Returns:
the keys

put

public V put(K key,
             V 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.
Specified by:
put in interface Map<K,V>
Overrides:
put in interface Dictionary<K,V>
Parameters:
key - the key used to locate the value
value - the value to be stored in the table
Returns:
the prior mapping of the key, or null if there was none
Throws:
NullPointerException - if key or value is null

putAll

public void putAll(extends K,
                   V> 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.
Specified by:
putAll in interface Map<K,V>
Parameters:
m - the map to be hashed into this
Throws:
NullPointerException - if m is null, or contains null keys or values

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 V remove(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.
Specified by:
remove in interface Map<K,V>
Overrides:
remove in interface Dictionary<K,V>
Parameters:
key - the key used to locate the value to remove
Returns:
whatever the key mapped to, if present

size

public int size()
Returns the number of key-value mappings currently in this hashtable.
Specified by:
size in interface Map<K,V>
Overrides:
size in interface Dictionary<K,V>
Returns:
the size

toString

public 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.

Overrides:
toString in interface Object
Returns:
the string representation

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.
Specified by:
values in interface Map<K,V>
Returns:
a bag view of the values
Since:
1.2
See Also:
keySet(), entrySet()

Hashtable.java -- a class providing a basic hashtable data structure, mapping Object --> Object Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.