Methods Summary |
---|
public void | clear()
count = 0;
currentBucket = 0;
current = null;
for (int i = 0; i < table.length; i++)
table [i] = null;
|
private void | d(java.lang.String s)
if (log.isDebugEnabled())
log.debug( "SimpleHashtable: " + s );
|
public java.lang.Object | get(java.lang.String key)Returns the value to which the specified key is mapped in this
hashtable ... the key isn't necessarily interned, though.
Entry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key))
return e.value;
}
return null;
|
public java.lang.Object | getInterned(java.lang.String key)Returns the value to which the specified key is mapped in this hashtable.
Entry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && (e.key == key))
return e.value;
}
return null;
|
public boolean | hasMoreElements()Used to view this as an enumeration; returns true if there
are more keys to be enumerated.
if (current != null)
return true;
while (currentBucket < table.length) {
current = table [currentBucket++];
if (current != null)
return true;
}
return false;
|
public java.util.Enumeration | keys()Returns an enumeration of the keys in this hashtable.
currentBucket = 0;
current = null;
hasMoreElements();
return this;
|
public java.lang.Object | nextElement()Used to view this as an enumeration; returns the next key
in the enumeration.
Object retval;
if (current == null)
throw new IllegalStateException ();
retval = current.key;
current = current.next;
// Advance to the next position ( we may call next after next,
// without hasMore )
hasMoreElements();
return retval;
|
public 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.
Entry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index] ; e != null ; e = e.next) {
// if ((e.hash == hash) && e.key.equals(key)) {
if ((e.hash == hash) && (e.key == key)) {
Object old = e.value;
e.value = value;
return old;
}
}
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();
tab = table;
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
Entry e = new Entry(hash, key, value, tab[index]);
tab[index] = e;
count++;
return null;
|
private void | rehash()Increases the capacity of and internally reorganizes this
hashtable, in order to accommodate and access its entries more
efficiently. 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;
Entry oldMap[] = table;
int newCapacity = oldCapacity * 2 + 1;
Entry newMap[] = new Entry[newCapacity];
threshold = (int)(newCapacity * loadFactor);
table = newMap;
/*
System.out.pr intln("rehash old=" + oldCapacity
+ ", new=" + newCapacity
+ ", thresh=" + threshold
+ ", count=" + count);
*/
for (int i = oldCapacity ; i-- > 0 ;) {
for (Entry old = oldMap[i] ; old != null ; ) {
Entry e = old;
old = old.next;
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = newMap[index];
newMap[index] = e;
}
}
|
public java.lang.Object | remove(java.lang.Object key)
Entry tab[] = table;
Entry prev=null;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
if( dL > 0 ) d("Idx " + index + " " + tab[index] );
for (Entry e = tab[index] ; e != null ; prev=e, e = e.next) {
if( dL > 0 ) d("> " + prev + " " + e.next + " " + e + " " + e.key);
if ((e.hash == hash) && e.key.equals(key)) {
if( prev!=null ) {
prev.next=e.next;
} else {
tab[index]=e.next;
}
if( dL > 0 ) d("Removing from list " + tab[index] + " " + prev +
" " + e.value);
count--;
Object res=e.value;
e.value=null;
return res;
}
}
return null;
|
public int | size()Returns the number of keys in this hashtable.
return count;
|