public class LinearHashtable<K,V> extends Object implements Cloneable, Serializable
Hashtable
.
This implementation is the one used by Smalltalk. It is useful if you need to have hundreds or even thousands of very small hashtables (with up to 3 entries for most instances).
LinearHashtable has exactly the same API as
java.util.Hashtable
, therefore there is no need to document
again this API here.
Note that the iterators returned by the methods of this class do not
support the remove
operation and do not detect concurrent
modification.
This class is not thread-safe.
Constructor and Description |
---|
LinearHashtable()
Constructs a hashtable with an initial capacity of 3 key/value pairs.
|
LinearHashtable(int capacity)
Constructs a hashtable with the specified initial capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
Object |
clone() |
boolean |
contains(V value) |
boolean |
containsKey(K key) |
void |
copyElementsInto(V[] array)
Copies the elements contained in this hashtable to the specified array.
|
void |
copyKeysInto(K[] array)
Copies the keys contained in this hashtable to the specified array.
|
Iterator<V> |
elements() |
Iterator<KeyValuePair<K,V>> |
entries()
Returns an Iterator over all the entries contained in this hashtable.
|
V |
get(K key) |
Object[] |
getKeyValueTable()
Returns the
Object table used to implement this kind of
hashtables. |
boolean |
isEmpty() |
Iterator<K> |
keys() |
V |
put(K key,
V value) |
V |
remove(K key) |
int |
size()
Returns the number of key/value pairs currently stored in this
hashtable.
|
String |
toString()
Returns a string representation of this hashtable which may be used
during debugging.
|
public LinearHashtable()
public LinearHashtable(int capacity)
capacity
- the initial capacity expressed in number of key/value
pairspublic int size()
Hashtable.size()
because the size is recomputed each
time this method is called.public boolean isEmpty()
public boolean contains(V value)
public boolean containsKey(K key)
public void clear()
public String toString()
public void copyKeysInto(K[] array)
array
- the array where the keys are to be copiedpublic void copyElementsInto(V[] array)
array
- the array where the elements are to be copiedpublic Iterator<KeyValuePair<K,V>> entries()
For efficiency reasons, the entry returned by this Iterator is always the same object. Copy it if you need to store this entry.
public Object[] getKeyValueTable()
Object
table used to implement this kind of
hashtables.
Keys are found at even indices.
If the key object is not null
, it is immediately
followed by its associated value (which are therefore found at even
indices).
For example, the following code can be used to dump a
LinearHashtable
:
Object[] table = htab.getKeyValueTable(); for (int i = 0; i < table.length; i += 2) { if (table[i] != null) { System.err.println("key=" + table[i]); System.err.println("value=" + table[i+1]); } }
Only use this method when you have a
desperate need for speed.
Otherwise, please use entries()
.
And, of course, do not modify the contents of the table returned by this method.