public class Hashtable extends java.util.Dictionary implements Map, java.lang.Cloneable, java.io.Serializable, Rollbackable
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.
HashMap
,
TreeMap
,
IdentityHashMap
,
LinkedHashMap
,
Serialized FormModifier and Type | Field and Description |
---|---|
protected Checkpoint |
$CHECKPOINT |
protected CheckpointRecord |
$RECORD$$CHECKPOINT |
Constructor and Description |
---|
Hashtable()
Construct a new Hashtable with the default capacity (11) and the default
load factor (0.75).
|
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.
|
Hashtable(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. |
Modifier and Type | Method and Description |
---|---|
void |
$COMMIT(long timestamp)
Commit changes up to the given timestamp, but not including changes
made at timestamp and afterward.
|
Checkpoint |
$GET$CHECKPOINT()
Get the checkpoint object that monitors this rollbackable object.
|
void |
$RESTORE(long timestamp,
boolean trim)
Restore a previous state to all the private fields of this rollbackable
object.
|
java.lang.Object |
$SET$CHECKPOINT(Checkpoint checkpoint)
Set the checkpoint object of this rollbackable object.
|
void |
clear()
Clears the hashtable so it has no keys.
|
java.lang.Object |
clone()
Returns a shallow clone of this Hashtable.
|
boolean |
contains(java.lang.Object value)
Returns true if this Hashtable contains a value
o ,
such that o.equals(value) . |
boolean |
containsKey(java.lang.Object key)
Returns true if the supplied object
equals() a key
in this Hashtable. |
boolean |
containsValue(java.lang.Object value)
Returns true if this Hashtable contains a value
o , such that
o.equals(value) . |
java.util.Enumeration |
elements()
Return an enumeration of the values of this table.
|
Set |
entrySet()
Returns a "set view" of this Hashtable's entries.
|
boolean |
equals(java.lang.Object o)
Returns true if this Hashtable equals the supplied Object
o . |
java.lang.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. |
int |
hashCode()
Returns the hashCode for this Hashtable.
|
boolean |
isEmpty()
Returns true if there are no key-value mappings currently in this table.
|
java.util.Enumeration |
keys()
Return an enumeration of the keys of this table.
|
Set |
keySet()
Returns a "set view" of this Hashtable's keys.
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Puts the supplied value into the Map, mapped by the supplied key.
|
void |
putAll(Map 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.
|
java.lang.Object |
remove(java.lang.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.
|
java.lang.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.
|
protected transient Checkpoint $CHECKPOINT
protected transient CheckpointRecord $RECORD$$CHECKPOINT
public Hashtable()
public Hashtable(Map m)
m
or the default of 11.
Every element in Map m will be put into this new Hashtable.
m
- a Map whose key / value pairs will be put into
the new Hashtable. NOTE: key / value pairs
are not cloned in this constructor.java.lang.NullPointerException
- if m is null, or if m contains a mapping
to or from `null'.public Hashtable(int initialCapacity)
initialCapacity
- the initial capacity of this Hashtable (>= 0)java.lang.IllegalArgumentException
- if (initialCapacity < 0)public Hashtable(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacity (>= 0)loadFactor
- the load factor (> 0, not NaN)java.lang.IllegalArgumentException
- if (initialCapacity < 0) ||
! (loadFactor > 0.0)public int size()
public boolean isEmpty()
public java.util.Enumeration keys()
keys
in class java.util.Dictionary
elements()
,
keySet()
public java.util.Enumeration elements()
public boolean contains(java.lang.Object value)
o
,
such that o.equals(value)
. This is the same as
containsValue()
, and is O(n).
value
- the value to search for in this Hashtablejava.lang.NullPointerException
- if value
is nullcontainsValue(Object)
,
containsKey(Object)
public boolean containsValue(java.lang.Object value)
o
, such that
o.equals(value)
. This is the new API for the old
contains()
.containsValue
in interface Map
value
- the value to search for in this Hashtablejava.lang.NullPointerException
- if value
is nullcontains(Object)
,
containsKey(Object)
public boolean containsKey(java.lang.Object key)
equals()
a key
in this Hashtable.containsKey
in interface Map
key
- the key to search for in this Hashtablejava.lang.NullPointerException
- if key is nullcontainsValue(Object)
public java.lang.Object get(java.lang.Object key)
null
if the key maps to nothing.get
in interface Map
get
in class java.util.Dictionary
key
- the key for which to fetch an associated valuejava.lang.NullPointerException
- if key is nullput(Object, Object)
,
containsKey(Object)
public java.lang.Object put(java.lang.Object key, java.lang.Object value)
equals()
this key.put
in interface Map
put
in class java.util.Dictionary
key
- the key used to locate the valuevalue
- the value to be stored in the tablejava.lang.NullPointerException
- if key or value is nullget(Object)
,
Object.equals(Object)
public java.lang.Object remove(java.lang.Object key)
null
is returned.public void putAll(Map m)
putAll
in interface Map
m
- the map to be hashed into thisjava.lang.NullPointerException
- if m is null, or contains null keys or valuesMap.put(Object, Object)
public void clear()
public java.lang.Object clone()
clone
in class java.lang.Object
public java.lang.String toString()
"{a=1, b=2}"
.
NOTE: if the toString()
method of any key or value
throws an exception, this will fail for the same reason.
toString
in class java.lang.Object
public Set keySet()
keySet
in interface Map
values()
,
entrySet()
public Collection values()
values
in interface Map
keySet()
,
entrySet()
public Set entrySet()
public boolean equals(java.lang.Object o)
o
.
As specified by Map, this is:
(o instanceof Map) && entrySet().equals(((Map) o).entrySet());
equals
in interface Map
equals
in class java.lang.Object
o
- the object to compare toSet.equals(Object)
public int hashCode()
hashCode
in interface Map
hashCode
in class java.lang.Object
Map.Entry.hashCode()
protected void rehash()
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.
public void $COMMIT(long timestamp)
Rollbackable
$COMMIT
in interface Rollbackable
$COMMIT
in interface Map
timestamp
- The timestamp.public void $RESTORE(long timestamp, boolean trim)
Rollbackable
$RESTORE
in interface Rollbackable
$RESTORE
in interface Map
timestamp
- The timestamp taken at the time when the previous
state was recorded.trim
- Whether to delete the records used for the rollback.Checkpoint.rollback(long, boolean)
public final Checkpoint $GET$CHECKPOINT()
Rollbackable
$GET$CHECKPOINT
in interface Rollbackable
$GET$CHECKPOINT
in interface Map
public final java.lang.Object $SET$CHECKPOINT(Checkpoint checkpoint)
Rollbackable
$SET$CHECKPOINT
in interface Rollbackable
$SET$CHECKPOINT
in interface Map
checkpoint
- The new checkpoint object.Checkpoint.setCheckpoint(Checkpoint)