Algorithms

HashMap / Hash Table

๐Ÿ“‹ Quick Reference

PropertyValue
TypeKey-value mapping with hashing
Get/PutO(1) average, O(n) worst case
ContainsO(1) average
MemoryO(n) + load factor overhead
OrderingNo guaranteed order
Best ForFast key-based lookup, caching, counting
One-liner: Hash-based map providing O(1) average-time key lookup via hash codes and buckets.

๐ŸŽฎ Interactive Visualizer

Watch how HashMap stores and retrieves values using hash codes and bucket arrays:

Loading visualizer...
Try these operations:
  1. Put key-value pairs - see hash calculation and bucket placement
  2. Cause a collision - watch chaining in action
  3. Get a value - observe the O(1) lookup path
  4. Remove an entry - see bucket cleanup

๐Ÿ”ง Key Operations

Creating

JAVA(11 lines)
Code
Loading syntax highlighter...

Adding/Updating

JAVA(8 lines)
Code
Loading syntax highlighter...

Retrieving

JAVA(4 lines)
Code
Loading syntax highlighter...

Removing

JAVA(3 lines)
Code
Loading syntax highlighter...

Iteration

JAVA(14 lines)
Code
Loading syntax highlighter...

๐Ÿ“Š Complexity Analysis

OperationAverageWorst CaseNotes
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)
containsKeyO(1)O(n)
containsValueO(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
...
Load Factor = entries / buckets (default 0.75)
  • 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) or TreeMap (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)
Code
Loading syntax highlighter...

2. Missing equals/hashCode

JAVA(8 lines)
Code
Loading syntax highlighter...

3. Null Confusion

JAVA(9 lines)
Code
Loading syntax highlighter...

4. Iteration Order Assumption

JAVA(7 lines)
Code
Loading syntax highlighter...

๐ŸŽฏ Interview Practice

Test your hash table knowledge with 10 curated interview questions:

Loading visualizer...

๐ŸŽค Interview Tips

Q: How does HashMap handle collisions?
"

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).

Q: Why is load factor 0.75 the default?
"

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.

Q: What makes a good hash function?
"

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.

Q: HashMap vs Hashtable?
"

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


Visualizer: HashMap from @tomaszjarosz/react-visualizers