How to Build a Hashmap
This week, we’re diving into one of the most powerful and fundamental data structures in programming: the Hash Map, often called a Hash Table or Dictionary. Hash maps are vital in nearly every language for quickly storing and retrieving data as key-value pairs.
What’s a Hash Map, Really?
A Hash Map is an array-based structure that gives you near-instant access to a stored value. It achieves this speed by using a special hash function to convert the key (e.g., a person’s name) into an index (a number) in the underlying array.
Think of it like a coat check:
The key is the unique ticket number you receive.
The hash function is the attendant who instantly knows which hook (the index) your coat is on.
The value is your coat itself.
For this simple example, we’ll use a fixed-size array of 10 slots. Important: To keep the code simple, this implementation does not handle collisions—a scenario where two different keys hash to the same index.
Python’s Built-in Hash Map: The dict
Before we build our own, let’s look at Python’s highly optimized, native implementation: the dict. Python
Create an empty dictionary
user_profile = {}
Store key-value pairs
user_profile["username"] = "coder_x"
user_profile["status"] = "online"
Retrieve a value instantly using its key
print(user_profile["username"]) # Output: coder_x
The final structure
print(user_profile) # Output: {'username': 'coder_x', 'status': 'online'}
Building a Custom HashMap Class
Now, let’s implement the core logic ourselves. Our HashMap will clearly show how keys are converted to array indices for storage and retrieval:
class HashMap:
def __init__(self, size):
self.size = size
# Initialize the map: a list of 'None' values
self.map = [None] * self.size
def _hash(self, key):
return hash(key) % self.size
def set(self, key, value):
index = self._hash(key)
self.map[index] = value
def get(self, key):
index = self._hash(key)
return self.map[index]
def __str__(self):
return str(self.map)
Practical Example and Indexing
Let’s see our custom HashMap in action and observe the indices it generates: Python
1. Initialize the hash map with 10 slots
contact_list = HashMap(10)
2. Store entries (Note: the hash index is calculated internally)
Key: “Alice” -> hash index: 3
contact_list.set("Alice", "555-0101")
Key: “Bob” -> hash index: 6
contact_list.set("Bob", "555-0202")
Key: “Charlie” -> hash index: 7
contact_list.set("Charlie", "555-0303")
3. Retrieve values using the original keys
print(f"Alice's Number: {contact_list.get('Alice')}") # Output: 555-0101
print(f"Bob's Number: {contact_list.get('Bob')}") # Output: 555-0202
4. View the underlying array after insertions
print("\n--- Map Array State ---")
print(str(contact_list))
Output (Indices 0-9):
[None, None, None, '555-0101', None, None, '555-0202', '555-0303', None, None]
This simplified model demonstrates the core principle: using a hash function to achieve fast O(1) (constant time) lookups by jumping directly to the correct array slot. Real-world hash maps add complexity to handle growth and collisions, but the foundational concept remains this efficient indexing strategy!
I hope this walkthrough has given you a solid understanding of how a Hash Map works at its core! While our example was simple, it demonstrates the efficient hashing and indexing that makes this data structure so powerful. Got a question or need clarification on collisions or resizing? Don’t hesitate to reach out in the comments below! Happy coding!