I know the difference between Hashtable and HashMap. However, both these classes seemingly are using a hash function to get the job done. Is there a difference between the hash function used in Hashtable, and the hash function used in HashMap?
In particular, is there a difference between the hashing algorithm they use? What is the formula used to hash in these two classes?
In other words, is the way for calculating index (hash value) different?
java.util.Hashtable<K,V>
is likejava.util.Vector<T>
. It's a class added to the SDK very early in development which has been superseded byHashMap<K,V>
(asArrayList<T>
supersededVector<T>
).So you simply shouldn't use it unless you require implicit synchronization of all the operations, which comes by default with
Hashtable
, but you can stil useCollections.synchronizedMap
for that purpose or aConcurrentHashMap<K,V>
.As stated in Javadoc:
The hashing of the two classes should be the same, as they'll both use
int Object::hashCode
for their purpose.The primary hash function used when you use an object as a hash table key is the object's
hashCode()
method. It is up the to the key class to implement a decent hash function.The
Hashtable
andHashMap
classes take the key's hashcode value and convert it to an index in the primary hashtable array-of-chains. However, there are differences in how this happens betweenHashtable
andHashMap
.For
Hashtable
(Java 8) the code is this:For
HashMap
(Java 8) the code is (effectively) this:As you can see,
HashMap
is scrambling the hashcode value returned by the key's hashcode function. This is explained in the source code as follows:Notes:
The
&
versus%
difference is because inHashtable
the hash array size is a prime number, but inHashMap
(Java 8) the size is a power of 2.In Java 8
HashMap
, the implementation will turn a long hash chain into a binary tree if the key class implementsComparable
.HashMap
handlesnull
keys, butHashtable
doesn't.However, all of this extra complexity in
HashMap
only comes into play if your key class has a poorly designed / implementedhashCode()
method ... or if someone is deliberately trying to engineer hash collisions.In other words, if your key class is well designed, the differences should not matter.