FileDocCategorySizeDatePackage
Hashtable.javaAPI DocphoneME MR2 API (J2ME)14408Wed May 02 17:59:56 BST 2007java.util

Hashtable

public class Hashtable extends Object
This class implements a hashtable, which maps keys to values. Any non-null object can be used as a key or as a value.

To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.

An instance of Hashtable has two parameters that affect its efficiency: its capacity and its load factor. The load factor in the CLDC implementation of the hashtable class is always 75 percent. When the number of entries in the hashtable exceeds the product of the load factor and the current capacity, the capacity is increased by calling the rehash method.

If many entries are to be made into a Hashtable, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.

This example creates a hashtable of numbers. It uses the names of the numbers as keys:

Hashtable numbers = new Hashtable();
numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));

To retrieve a number, use the following code:

Integer n = (Integer)numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}
version
12/17/01 (CLDC 1.1)
see
java.lang.Object#equals(java.lang.Object)
see
java.lang.Object#hashCode()
see
java.util.Hashtable#rehash()
since
JDK1.0, CLDC 1.0

Fields Summary
private transient HashtableEntry[]
table
The hash table data.
private transient int
count
The total number of entries in the hash table.
private int
threshold
Rehashes the table when count exceeds this threshold.
private static final int
loadFactorPercent
The load factor for the hashtable. In CLDC, the default load factor is 75%.
Constructors Summary
public Hashtable(int initialCapacity)
Constructs a new, empty hashtable with the specified initial capacity.

param
initialCapacity the initial capacity of the hashtable.
exception
IllegalArgumentException if the initial capacity is less than zero
since
JDK1.0


                                                                 
       
        if (initialCapacity < 0) {
            throw new IllegalArgumentException();
        }
        if (initialCapacity == 0) {
            initialCapacity = 1;
        }
        table = new HashtableEntry[initialCapacity];
        threshold = (int)((initialCapacity * loadFactorPercent) / 100);
    
public Hashtable()
Constructs a new, empty hashtable with a default capacity and load factor.

since
JDK1.0

        this(11);
    
Methods Summary
public synchronized voidclear()
Clears this hashtable so that it contains no keys.

since
JDK1.0

        HashtableEntry tab[] = table;
        for (int index = tab.length; --index >= 0; )
            tab[index] = null;
        count = 0;
    
public synchronized booleancontains(java.lang.Object value)
Tests if some key maps into the specified value in this hashtable. This operation is more expensive than the containsKey method.

param
value a value to search for.
return
true if some key maps to the value argument in this hashtable; false otherwise.
exception
NullPointerException if the value is null.
see
java.util.Hashtable#containsKey(java.lang.Object)
since
JDK1.0

        if (value == null) {
            throw new NullPointerException();
        }

        HashtableEntry tab[] = table;
        for (int i = tab.length ; i-- > 0 ;) {
            for (HashtableEntry e = tab[i] ; e != null ; e = e.next) {
                if (e.value.equals(value)) {
                    return true;
                }
            }
        }
        return false;
    
public synchronized booleancontainsKey(java.lang.Object key)
Tests if the specified object is a key in this hashtable.

param
key possible key.
return
true if the specified object is a key in this hashtable; false otherwise.
see
java.util.Hashtable#contains(java.lang.Object)
since
JDK1.0

        HashtableEntry tab[] = table;
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        for (HashtableEntry e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                return true;
            }
        }
        return false;
    
public synchronized java.util.Enumerationelements()
Returns an enumeration of the values in this hashtable. Use the Enumeration methods on the returned object to fetch the elements sequentially.

return
an enumeration of the values in this hashtable.
see
java.util.Enumeration
see
java.util.Hashtable#keys()
since
JDK1.0

        return new HashtableEnumerator(table, false);
    
public synchronized java.lang.Objectget(java.lang.Object key)
Returns the value to which the specified key is mapped in this hashtable.

param
key a key in the hashtable.
return
the value to which the key is mapped in this hashtable; null if the key is not mapped to any value in this hashtable.
see
java.util.Hashtable#put(java.lang.Object, java.lang.Object)
since
JDK1.0

        HashtableEntry tab[] = table;
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        for (HashtableEntry e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                return e.value;
            }
        }
        return null;
    
public booleanisEmpty()
Tests if this hashtable maps no keys to values.

return
true if this hashtable maps no keys to values; false otherwise.
since
JDK1.0

        return count == 0;
    
public synchronized java.util.Enumerationkeys()
Returns an enumeration of the keys in this hashtable.

return
an enumeration of the keys in this hashtable.
see
java.util.Enumeration
see
java.util.Hashtable#elements()
since
JDK1.0

        return new HashtableEnumerator(table, true);
    
public synchronized java.lang.Objectput(java.lang.Object key, java.lang.Object value)
Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null.

The value can be retrieved by calling the get method with a key that is equal to the original key.

param
key the hashtable key.
param
value the value.
return
the previous value of the specified key in this hashtable, or null if it did not have one.
exception
NullPointerException if the key or value is null.
see
java.lang.Object#equals(java.lang.Object)
see
java.util.Hashtable#get(java.lang.Object)
since
JDK1.0

        // Make sure the value is not null
        if (value == null) {
            throw new NullPointerException();
        }

        // Makes sure the key is not already in the hashtable.
        HashtableEntry tab[] = table;
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        for (HashtableEntry e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                Object old = e.value;
                e.value = value;
                return old;
            }
        }

        if (count >= threshold) {
            // Rehash the table if the threshold is exceeded
            rehash();
            return put(key, value);
        }

        // Creates the new entry.
        HashtableEntry e = new HashtableEntry();
        e.hash = hash;
        e.key = key;
        e.value = value;
        e.next = tab[index];
        tab[index] = e;
        count++;
        return null;
    
protected voidrehash()
Rehashes the contents of the hashtable into a hashtable with a larger capacity. This method is called automatically when the number of keys in the hashtable exceeds this hashtable's capacity and load factor.

since
JDK1.0

        int oldCapacity = table.length;
        HashtableEntry oldTable[] = table;

        int newCapacity = oldCapacity * 2 + 1;
        HashtableEntry newTable[] = new HashtableEntry[newCapacity];

        threshold = (int)((newCapacity * loadFactorPercent) / 100);
        table = newTable;

        for (int i = oldCapacity ; i-- > 0 ;) {
            for (HashtableEntry old = oldTable[i] ; old != null ; ) {
                HashtableEntry e = old;
                old = old.next;

                int index = (e.hash & 0x7FFFFFFF) % newCapacity;
                e.next = newTable[index];
                newTable[index] = e;
            }
        }
    
public synchronized java.lang.Objectremove(java.lang.Object key)
Removes the key (and its corresponding value) from this hashtable. This method does nothing if the key is not in the hashtable.

param
key the key that needs to be removed.
return
the value to which the key had been mapped in this hashtable, or null if the key did not have a mapping.
since
JDK1.0

        HashtableEntry tab[] = table;
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        //this loop was reviewed - code is approved!
        for (HashtableEntry e = tab[index], prev = null ; e != null ; prev = e, e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                if (prev != null) {
                    prev.next = e.next;
                } else {
                    tab[index] = e.next;
                }
                count--;
                return e.value;
            }
        }
        return null;
    
public intsize()
Returns the number of keys in this hashtable.

return
the number of keys in this hashtable.
since
JDK1.0

        return count;
    
public synchronized java.lang.StringtoString()
Returns a rather long string representation of this hashtable.

return
a string representation of this hashtable.
since
JDK1.0

        int max = size() - 1;
        StringBuffer buf = new StringBuffer();
        Enumeration k = keys();
        Enumeration e = elements();
        buf.append("{");

        for (int i = 0; i <= max; i++) {
            String s1 = k.nextElement().toString();
            String s2 = e.nextElement().toString();
            buf.append(s1 + "=" + s2);
            if (i < max) {
                buf.append(", ");
            }
        }
        buf.append("}");
        return buf.toString();