Methods Summary |
---|
public synchronized void | clear()Clears this hashtable so that it contains no keys.
HashtableEntry tab[] = table;
for (int index = tab.length; --index >= 0; )
tab[index] = null;
count = 0;
|
public synchronized boolean | contains(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.
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 boolean | containsKey(java.lang.Object key)Tests if the specified object is a key in this 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)) {
return true;
}
}
return false;
|
public synchronized java.util.Enumeration | elements()Returns an enumeration of the values in this hashtable.
Use the Enumeration methods on the returned object to fetch the elements
sequentially.
return new HashtableEnumerator(table, false);
|
public synchronized java.lang.Object | get(java.lang.Object key)Returns the value to which the specified key is mapped in this 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)) {
return e.value;
}
}
return null;
|
public boolean | isEmpty()Tests if this hashtable maps no keys to values.
return count == 0;
|
public synchronized java.util.Enumeration | keys()Returns an enumeration of the keys in this hashtable.
return new HashtableEnumerator(table, true);
|
public synchronized java.lang.Object | put(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.
// 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 void | rehash()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.
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.Object | remove(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.
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 int | size()Returns the number of keys in this hashtable.
return count;
|
public synchronized java.lang.String | toString()Returns a rather long string representation of this hashtable.
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();
|