Woodstock Blog

a tech blog for general algorithmic interview questions

[Design] HashMap vs Hashtable vs HashSet

Overview

HashTable

  1. HashTable is thread safe synchronized. Only 1 thread can access at a time (compromise speed).
  2. HashTable does not allow null for key and value.

HashMap & HashSet

  1. HashMap is not sync, but better performance.
  2. HashMap allows null key or null value.
  3. HashSet is same as HashMap, just interface difference.

Conclusion

  1. HashMap is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
  2. HashSet is same as HashMap. It’s not thread safe.

Implementation

  1. C++ Map is rbtree
  2. Java HashMap is buckets + entries (using Array + LinkedList)
  3. 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).