HashMap Setup
HashMap Operations
Hash Function
hash(key) = key.hashCode() % map_size
HashMap is a key-value data structure that uses hashing for fast access and implements the Map interface.
Key Characteristics:
Hash Calculation Example: For string "user" with map size 8:
Step 1: Calculate hashCode(): "user".hashCode() = 3599307
Step 2: Apply modulo: 3599307 % 8 = 3
Therefore, hash("user") = 3, so "user" is stored in bucket 3.
Key Differences:
| Operation | Average | Worst |
|---|---|---|
| Put | O(1) | O(n) |
| Get | O(1) | O(n) |
| Remove | O(1) | O(n) |
| Component | Space |
|---|---|
| Map Storage | O(n) |
| Load Factor | 0.75 |