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?
java.util.Hashtable<K,V> is like
java.util.Vector<T>. It's a class added to the SDK very early in development which has been superseded by
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 use
Collections.synchronizedMap for that purpose or a
As stated in Javadoc:
As of the Java 2 platform v1.2, this class was retrofitted to implement the
Mapinterface, making it a member of the Java Collections Framework. Unlike the new collection implementations,
Hashtableis synchronized. If a thread-safe implementation is not needed, it is recommended to use
HashMapin place of
Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use
ConcurrentHashMapin place of
The hashing of the two classes should be the same, as they'll both use
int Object::hashCode for their purpose.