Running the following code shows a rather serious bug in the hashtable.remove
code. In particular, if you run the code, new Integer(600) is never removed from
the hashtable. However the hash table count (as indicated by .size()) is decremented
by 1.
I've managed to create hash tables whose size() was negative!
I know what the problem has. The line
prev = e.next
should be
prev.next = e.next
The bug only shows up when the table starts to get full, and you remove anything but
the first item in a HashtableEntry. Most HashtableEntry's only have one (or zero) items,
so this bug seldom appears.
import java.util.Hashtable;
class test {
public static void main (String argv[]) {
Hashtable hash = new Hashtable(1000, 1.0);
for (int i = 0; i < 1000; i++) {
Integer I = new Integer(i * 10);
hash.put(I, I);
}
Object value1 = hash.remove(new Integer(600));
Object value2 = hash.remove(new Integer(600));
Object value3 = hash.remove(new Integer(600));
System.out.println(value1 + " " + value2 + " " + value3);
}
}
code. In particular, if you run the code, new Integer(600) is never removed from
the hashtable. However the hash table count (as indicated by .size()) is decremented
by 1.
I've managed to create hash tables whose size() was negative!
I know what the problem has. The line
prev = e.next
should be
prev.next = e.next
The bug only shows up when the table starts to get full, and you remove anything but
the first item in a HashtableEntry. Most HashtableEntry's only have one (or zero) items,
so this bug seldom appears.
import java.util.Hashtable;
class test {
public static void main (String argv[]) {
Hashtable hash = new Hashtable(1000, 1.0);
for (int i = 0; i < 1000; i++) {
Integer I = new Integer(i * 10);
hash.put(I, I);
}
Object value1 = hash.remove(new Integer(600));
Object value2 = hash.remove(new Integer(600));
Object value3 = hash.remove(new Integer(600));
System.out.println(value1 + " " + value2 + " " + value3);
}
}