Skip to content

Commit f733bf6

Browse files
committed
HashMap vs Hashtable in Java
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 5e61cb4 commit f733bf6

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
HashMap vs Hashtable in Java
2+
3+
Introduction
4+
------------
5+
Both HashMap and Hashtable are classes in Java that implement the Map interface and store data in key-value pairs.
6+
They are hash table–based data structures but differ significantly in design, features, and usage.
7+
8+
---
9+
10+
1. Package and Legacy
11+
---------------------
12+
- HashMap: Introduced in Java 1.2 as part of the Collections Framework (`java.util` package).
13+
- Hashtable: Legacy class from Java 1.0 (before Collections Framework).
14+
15+
---
16+
17+
2. Synchronization and Thread Safety
18+
------------------------------------
19+
- HashMap: Not synchronized → faster in single-threaded environments. Must be externally synchronized when used by
20+
multiple threads.
21+
- Hashtable: Synchronized → all methods are thread-safe but at the cost of performance.
22+
23+
---
24+
25+
3. Null Handling
26+
----------------
27+
- HashMap: Allows one `null` key and multiple `null` values.
28+
- Hashtable: Does **not** allow `null` keys or `null` values. Attempting to insert them throws `NullPointerException`.
29+
30+
---
31+
32+
4. Performance
33+
--------------
34+
- HashMap: Faster because it is unsynchronized (no locking overhead).
35+
- Hashtable: Slower due to synchronized methods.
36+
37+
---
38+
39+
5. Iterators
40+
------------
41+
- HashMap: Uses **Iterator**, which is fail-fast.
42+
If the map is structurally modified after the iterator is created, it throws `ConcurrentModificationException`.
43+
- Hashtable: Uses **Enumerator** (legacy) and Iterator. Enumerator is not fail-fast.
44+
45+
---
46+
47+
6. Order of Elements
48+
--------------------
49+
- Both HashMap and Hashtable do not guarantee any specific order of elements.
50+
- If order is required, use `LinkedHashMap` (insertion order) or `TreeMap` (sorted order).
51+
52+
---
53+
54+
7. Modern Usage
55+
---------------
56+
- HashMap: Preferred in most applications. Use `ConcurrentHashMap` for thread-safe operations in concurrent environments.
57+
- Hashtable: Considered obsolete. Still found in some legacy systems but generally avoided in new code.
58+
59+
---
60+
61+
8. Example Code
62+
---------------
63+
64+
HashMap Example:
65+
```java
66+
import java.util.HashMap;
67+
68+
public class HashMapDemo {
69+
public static void main(String[] args) {
70+
HashMap<Integer, String> map = new HashMap<>();
71+
map.put(1, "A");
72+
map.put(2, "B");
73+
map.put(null, "C"); // Allowed
74+
map.put(3, null); // Allowed
75+
76+
System.out.println(map);
77+
}
78+
}
79+
80+
Hashtable Example:
81+
82+
import java.util.Hashtable;
83+
84+
public class HashtableDemo {
85+
public static void main(String[] args) {
86+
Hashtable<Integer, String> table = new Hashtable<>();
87+
table.put(1, "A");
88+
table.put(2, "B");
89+
// table.put(null, "C"); // NullPointerException
90+
// table.put(3, null); // NullPointerException
91+
92+
System.out.println(table);
93+
}
94+
}
95+
96+
97+
98+
99+
Summary
100+
• HashMap → Modern, faster, allows null, not synchronized. Use in single-threaded or with external sync.
101+
• Hashtable → Legacy, synchronized, disallows null. Rarely used today; replaced by ConcurrentHashMap.
102+
103+
104+
105+
Feature | HashMap | Hashtable
106+
----------------------|----------------------------------------------|-----------------------------------------
107+
Ordering | No ordering of keys (unordered) | No ordering of keys (unordered)
108+
Implementation | Hash table and hashing mechanism | Hash table (legacy implementation)
109+
Performance | O(1) average for put(), get(), remove() | O(1) average but slower due to synchronization
110+
Null Keys | Allows one null key and multiple null values | Does not allow null keys or null values
111+
Thread Safety | Not synchronized (must be manually handled) | Synchronized (thread-safe, but slower)
112+
Iterators | Iterator (fail-fast) | Enumerator (legacy, not fail-fast) + Iterator
113+
Modern Usage | Preferred choice; |
114+
| use ConcurrentHashMap for thread-safe needs | Considered legacy; rarely used in new applications
115+
Introduced In | Java 1.2 (Collections Framework) | Java 1.0 (pre-Collections, legacy class)

0 commit comments

Comments
 (0)