Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 40

HashMap vs Hashtable in Java

In this lesson: HashMap vs Hashtable — synchronization, null keys, performance — and when to use ConcurrentHashMap. In Hindi & English.

Two key-value maps

HashMap and Hashtable both store key-value pairs, but they differ in thread safety, null handling and speed. HashMap is the modern default; Hashtable is an older, legacy class. Knowing the differences helps you pick correctly.

💡 HashMap = fast, not thread-safe, allows one null key. Hashtable = thread-safe, slower, no nulls.

HashMap

import java.util.HashMap;

HashMap<String, Integer> map = new HashMap<>();
map.put("Aman", 88);
map.put(null, 0);        // null key allowed
System.out.println(map.get("Aman"));
88

HashMap gives fast key-based lookup and allows one null key. It is not thread-safe, which is exactly why it is fast — ideal for normal single-threaded programs.

Hashtable

import java.util.Hashtable;

Hashtable<String, Integer> table = new Hashtable<>();
table.put("Aman", 88);
// table.put(null, 0);   // ERROR: no null keys
System.out.println(table.get("Aman"));
88

Hashtable is thread-safe because all its methods are synchronized. But it does not allow null keys or values, and the synchronization makes it slower.

The key differences

  • Thread safety: Hashtable is synchronized; HashMap is not.
  • Speed: HashMap is faster (no locking overhead).
  • Nulls: HashMap allows one null key and null values; Hashtable allows none.
  • Age: Hashtable is legacy; HashMap is the modern choice.

Null keys and values

HashMap<String, String> m = new HashMap<>();
m.put(null, "ok");        // fine in HashMap
m.put("x", null);         // fine

// Hashtable would throw NullPointerException on either

HashMap tolerates a null key and null values, which can be handy. Hashtable rejects both, throwing a NullPointerException — a subtle but important difference.

What to use today

  • Single-threaded code → HashMap (fast, the common case).
  • Thread-safe access → ConcurrentHashMap (modern, efficient).
  • Hashtable → generally avoid in new code; it is legacy.

So in practice you rarely reach for Hashtable — HashMap or ConcurrentHashMap covers today's needs.

Common mistakes

  • Using Hashtable for new code. Prefer HashMap or ConcurrentHashMap.
  • Expecting HashMap to be thread-safe. It is not — use ConcurrentHashMap for that.
  • Putting a null key in Hashtable. It throws NullPointerException.
  • Expecting insertion order. Neither keeps order; use LinkedHashMap if you need it.

Summary

  • Both store key-value pairs; they differ in safety, nulls and speed.
  • HashMap is fast, not thread-safe, allows one null key.
  • Hashtable is thread-safe, slower, allows no nulls, and is legacy.
  • Use HashMap for single-threaded code.
  • Use ConcurrentHashMap, not Hashtable, for thread safety.

Next, revise maps in the Collections Framework, or compare sets in HashSet vs TreeSet.

Frequently Asked Questions

What is the difference between HashMap and Hashtable?
Both store key-value pairs. HashMap is not thread-safe but is fast and allows one null key. Hashtable is thread-safe (synchronized) but slower and allows no null keys or values. HashMap is preferred in modern single-threaded code.
Is HashMap thread-safe?
No, HashMap is not thread-safe, which is why it is fast. If several threads modify it at once, data can be corrupted. For thread safety, use ConcurrentHashMap rather than the old Hashtable.
Can HashMap have a null key?
Yes, HashMap allows one null key and multiple null values. Hashtable allows neither — using a null key or value with Hashtable throws a NullPointerException.
Which is faster, HashMap or Hashtable?
HashMap is faster because it is not synchronized, so it has no locking overhead. Hashtable synchronizes every method, which makes it slower even in single-threaded use.
Should I use Hashtable in new code?
Generally no. Hashtable is a legacy class. Use HashMap for single-threaded code, and ConcurrentHashMap when you need thread-safe access. These are the modern, recommended choices.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicHashSet vs TreeSet Your progress: 40 / 61 (66%) · Lesson 41 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free