HashMap / Hash Table
๐ Quick Reference
| Property | Value |
|---|---|
| Type | Key-value mapping with hashing |
| Get/Put | O(1) average, O(n) worst case |
| Contains | O(1) average |
| Memory | O(n) + load factor overhead |
| Ordering | No guaranteed order |
| Best For | Fast key-based lookup, caching, counting |
๐ฎ Interactive Visualizer
Watch how HashMap stores and retrieves values using hash codes and bucket arrays:
Loading visualizer...
- Put key-value pairs - see hash calculation and bucket placement
- Cause a collision - watch chaining in action
- Get a value - observe the O(1) lookup path
- Remove an entry - see bucket cleanup
๐ง Key Operations
Creating
JAVA(11 lines)CodeLoading syntax highlighter...
Adding/Updating
JAVA(8 lines)CodeLoading syntax highlighter...
Retrieving
JAVA(4 lines)CodeLoading syntax highlighter...
Removing
JAVA(3 lines)CodeLoading syntax highlighter...
Iteration
JAVA(14 lines)CodeLoading syntax highlighter...
๐ Complexity Analysis
| Operation | Average | Worst Case | Notes |
|---|---|---|---|
get(key) | O(1) | O(n) | Worst when all hash to same bucket |
put(key, val) | O(1) | O(n) | May trigger rehash |
remove(key) | O(1) | O(n) | |
containsKey | O(1) | O(n) | |
containsValue | O(n) | O(n) | Always scans all |
size() | O(1) | O(1) | Stored as field |
Hash Table Internals
Hash Code โ Bucket Index โ Search Chain bucket[0]: null bucket[1]: Entry("cat", 5) โ Entry("act", 3) โ null // collision chain bucket[2]: Entry("dog", 7) โ null bucket[3]: null ...
- When exceeded, table doubles and rehashes all entries
โ When to Use HashMap
Good Use Cases
- Fast key lookup - caching, memoization
- Counting occurrences - word frequency, etc.
- Grouping data - partition by some key
- De-duplication - track seen items
Avoid When
- Need ordering - use
LinkedHashMap(insertion) orTreeMap(sorted) - Need thread safety - use
ConcurrentHashMap - Null keys matter - HashMap allows one null key, unlike some alternatives
- Keys are mutable - changing key after insertion breaks the map!
โ ๏ธ Common Pitfalls
1. Mutable Keys
JAVA(7 lines)CodeLoading syntax highlighter...
2. Missing equals/hashCode
JAVA(8 lines)CodeLoading syntax highlighter...
3. Null Confusion
JAVA(9 lines)CodeLoading syntax highlighter...
4. Iteration Order Assumption
JAVA(7 lines)CodeLoading syntax highlighter...
๐ฏ Interview Practice
Test your hash table knowledge with 10 curated interview questions:
Loading visualizer...
๐ค Interview Tips
"In Java 8+, it uses chaining with linked lists that convert to red-black trees when a bucket exceeds 8 entries. This improves worst-case from O(n) to O(log n).
"It's a balance between space and time. Lower means fewer collisions but more memory. Higher means more collisions. 0.75 provides good performance for most use cases.
"It should distribute keys uniformly across buckets, be fast to compute, and be consistent (same input always gives same output). Poor distribution causes clustering and O(n) lookups.
"HashMap is not synchronized (faster for single-threaded), allows null key/values. Hashtable is legacy, synchronized, no nulls. Use ConcurrentHashMap for thread safety.
๐ Series Navigation
HashMap from @tomaszjarosz/react-visualizers