Difference Between HashMap and Hashtable

Naveen AnamaneniUpdated · 3 min read

HashMap and Hashtable both store key-value pairs in a hash table and both implement the Map interface. The main differences are that Hashtable is synchronized and rejects null, while HashMap is not synchronized and accepts null. In this lesson, you will learn all the differences, see them in code, and learn which map to use for shared data.

Example: The null Difference

Let's see an example that stores null in both maps.

NullTest.java▶ Try it
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
 
public class NullTest {
    static void test(Map<String, String> map) {
        String name = map.getClass().getSimpleName();
        try {
            map.put(null, "no key");
            map.put("Navika", null);
            System.out.println(name + ": accepted, " + map);
        } catch (NullPointerException e) {
            System.out.println(name + ": NullPointerException");
        }
    }
 
    public static void main(String[] args) {
        test(new HashMap<>());
        test(new Hashtable<>());
    }
}
Output
HashMap: accepted, {null=no key, Navika=null}
Hashtable: NullPointerException

Explanation: The same method is called with both maps. HashMap stores a null key and a null value. Hashtable throws as soon as it meets the null key.

Differences Between HashMap and Hashtable

HashMapHashtable
SynchronizationNot synchronizedSynchronized
Thread safetyNot thread-safeThread-safe for single calls
PerformanceFasterSlower, because of locking
null keysOne allowedNot allowed
null valuesAllowedNot allowed
Introduced inJava 1.2Java 1.0 (legacy)
Parent classAbstractMapDictionary
IterationFail-fast IteratorIterator and old Enumeration
Default capacity16, doubles11, grows to 2 x old + 1
Long collision chainsTurned into trees (Java 8)Stay as lists

What to Use for Shared Maps

Hashtable locks the whole map for every call. ConcurrentHashMap was designed for threads: reads don't block, and writes lock only a small part of the map. It also provides atomic compound operations such as merge and computeIfAbsent.

SituationUse
One threadHashMap
Many threadsConcurrentHashMap
Old code that already uses itHashtable (only when you can't change it)

Common Mistakes

Assuming Hashtable makes "check, then put" safe. Two separate calls can still interleave; use putIfAbsent or merge.

Moving code from HashMap to ConcurrentHashMap without checking for null. ConcurrentHashMap rejects null too.

Interview Corner

What is the difference between HashMap and Hashtable?

HashMap isn't synchronized, so it's faster but not thread-safe; it allows one null key and any number of null values. Hashtable is a legacy class from Java 1.0 whose methods are all synchronized; it allows no null keys or values and supports the old Enumeration. HashMap also converts long collision chains into trees. For concurrent use, ConcurrentHashMap is preferred over both.

Practice

  1. Easy: Replace the Hashtable in the example with a ConcurrentHashMap and predict the output before running it.
  2. Harder: Use two threads to add 10,000 keys each to a shared HashMap and to a shared ConcurrentHashMap. Compare the final sizes.

Quick check: HashMap vs Hashtable

4 questions
1Which allows a null key?
2Which is synchronized?
3Which is faster in single-threaded code?
4What should replace Hashtable for shared data?

Key Takeaways

Was this tutorial helpful?

Lesson 290 of 482