---
title: "What are the differences between a HashMap and a Hashtable in Java?"  
description: "What are the differences between a HashMap and a Hashtable in Java?"  
author: "Utpal Vishwas"  
published: 2023-07-21  
updated: 2023-11-17  
canonical: https://www.mindstick.com/forum/159211/what-are-the-differences-between-a-hashmap-and-a-hashtable-in-java  
category: "java"  
tags: ["java", "hashtable"]  
reading_time: 3 minutes  

---

# What are the differences between a HashMap and a Hashtable in Java?

What are the [differences](https://www.mindstick.com/articles/12918/cat-5e-vs-cat-6a-understanding-the-major-differences) between a [HashMap](https://www.mindstick.com/forum/33432/what-is-race-condition-in-hashmap) and a Hashtable in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Aryan Kumar

\
In Java, both **HashMap** and **Hashtable** are implementations of the **Map** interface, and they provide key-value pair storage and retrieval. Despite their similar functionalities, there are several differences between **HashMap** and **Hashtable**:

## Synchronization:

- **Hashtable is synchronized:** All methods in **Hashtable** are synchronized, making it thread-safe. This means that multiple threads can safely access a **Hashtable** concurrently without the need for external synchronization. While this ensures thread safety, it can also introduce performance overhead.
- **HashMap is not synchronized by default:** **HashMap** is not synchronized by default. If multiple threads access a **HashMap** concurrently and at least one of the threads modifies it structurally (i.e., adding or removing elements), it must be synchronized externally. However, you can make a **HashMap** synchronized using **Collections.synchronizedMap()**.

## Null Values:

- **Hashtable does not allow null keys or values:** Neither keys nor values in a **Hashtable** can be **null**. Attempting to store or retrieve **null** values or keys results in a **NullPointerException**.
- **HashMap allows one null key and multiple null values:** In contrast, **HashMap** allows one **null** key and multiple **null** values. This flexibility can be useful in certain scenarios.

## Performance:

- **HashMap generally performs better:** Due to its unsynchronized nature, **HashMap** generally performs better than **Hashtable** in a non-multithreaded environment. If thread safety is not a requirement, using **HashMap** is often preferred for its better performance.

## Iterator Fail-Fast vs Fail-Safe:

- **Hashtable uses Enumeration and is fail-fast:** **Hashtable** uses the Enumeration interface, and if the underlying collection is modified while an Enumeration is in progress, it throws a **ConcurrentModificationException**.
- **HashMap uses Iterator and is fail-fast (if not synchronized):** **HashMap** uses the Iterator interface, and if the underlying collection is modified while an Iterator is in progress (and the **HashMap** is not synchronized), it throws a **ConcurrentModificationException**.

## Inheritance:

- **Hashtable is a legacy class:** **Hashtable** is a legacy class, and its use is discouraged in favor of **HashMap**, which is part of the Java Collections Framework introduced later. However, **Hashtable** is still supported for backward compatibility.
- **HashMap is part of the Java Collections Framework:** **HashMap** is part of the Java Collections Framework and provides additional methods introduced with Java 2.

In summary, the key differences between **HashMap** and **Hashtable** in Java lie in their synchronization, handling of null values, performance characteristics, and the fact that **Hashtable** is a legacy class. If thread safety is not a concern and you are working in a non-multithreaded environment, **HashMap** is generally the preferred choice due to its better performance.


---

Original Source: https://www.mindstick.com/forum/159211/what-are-the-differences-between-a-hashmap-and-a-hashtable-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
