com.xmlmind.util
Class LinearHashtable<K,V>

java.lang.Object
  extended by com.xmlmind.util.LinearHashtable<K,V>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
IdentityLinearHashtable

public class LinearHashtable<K,V>
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

A hashtable which is more compact and less efficient than 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.

See Also:
Serialized Form

Constructor Summary
LinearHashtable()
          Constructs a hashtable with an initial capacity of 3 key/value pairs.
LinearHashtable(int capacity)
          Constructs a hashtable with the specified initial capacity.
 
Method Summary
 void clear()
           
 java.lang.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.
 java.util.Iterator<V> elements()
           
 java.util.Iterator<KeyValuePair<K,V>> entries()
          Returns an Iterator over all the entries contained in this hashtable.
 V get(K key)
           
 java.lang.Object[] getKeyValueTable()
          Returns the Object table used to implement this kind of hashtables.
 boolean isEmpty()
           
 java.util.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.
 java.lang.String toString()
          Returns a string representation of this hashtable which may be used during debugging.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LinearHashtable

public LinearHashtable()
Constructs a hashtable with an initial capacity of 3 key/value pairs.


LinearHashtable

public LinearHashtable(int capacity)
Constructs a hashtable with the specified initial capacity.

Parameters:
capacity - the initial capacity expressed in number of key/value pairs
Method Detail

size

public int size()
Returns the number of key/value pairs currently stored in this hashtable. Note that calling this method is more expensive than calling Hashtable.size() because the size is recomputed each time this method is called.

Returns:
the number of key/value pairs currently stored in this hashtable

isEmpty

public boolean isEmpty()

contains

public boolean contains(V value)

containsKey

public boolean containsKey(K key)

get

public V get(K key)

put

public V put(K key,
             V value)

remove

public V remove(K key)

clear

public void clear()

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

toString

public java.lang.String toString()
Returns a string representation of this hashtable which may be used during debugging.

Overrides:
toString in class java.lang.Object

copyKeysInto

public void copyKeysInto(K[] array)
Copies the keys contained in this hashtable to the specified array.

Parameters:
array - the array where the keys are to be copied

copyElementsInto

public void copyElementsInto(V[] array)
Copies the elements contained in this hashtable to the specified array.

Parameters:
array - the array where the elements are to be copied

keys

public java.util.Iterator<K> keys()

elements

public java.util.Iterator<V> elements()

entries

public java.util.Iterator<KeyValuePair<K,V>> entries()
Returns an Iterator over all the entries contained in this hashtable.

For efficiency reasons, the entry returned by this Iterator is always the same object. Copy it if you need to store this entry.


getKeyValueTable

public java.lang.Object[] getKeyValueTable()
Returns the 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.