Overview
HashTable
- HashTable is thread safe synchronized. Only 1 thread can access at a time (compromise speed).
- HashTable does not allow null for key and value.
HashMap & HashSet
- HashMap is not sync, but better performance.
- HashMap allows null key or null value.
- HashSet is same as HashMap, just interface difference.
Conclusion
- HashMap is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
- HashSet is same as HashMap. It’s not thread safe.
Implementation
- C++ Map is rbtree
- Java HashMap is buckets + entries (using Array + LinkedList)
- HashTable in Java is basically HashMap + lock
One more thing
HashMap is locked-out during rehashing, so it’s better to avoid rehashing (by setting initial size bigger).