I am wondering if the default implementation of Java Hashtable#hashCode()
broken when the Hashtable
contains only entries with the same keys and values ββfor each pair.
See, for example, the following appendix:
public class HashtableHash { public static void main(final String[] args) { final Hashtable<String, String> ht = new Hashtable<String, String>(); final int h1 = ht.hashCode(); System.out.println(h1);
The hash code for an empty Hashtable is 0. After the record with the key "Test"
and the value "Test"
been added to the Hastable, the hash code is still 0.
The problem is that in the Hashtable hashCode()
method, the hash code of each record is computed and added to the hash code as follows
h += e.key.hashCode() ^ e.value.hashCode()
However, the XOR
for identical hash codes (which is the case for identical strings) is always 0. Thus, records with identical keys and values ββare not part of the Hashtable hash.
This implementation is broken because the Hashtable has actually changed. It doesn't matter if the key and value match.
source share