System Design
10-Min Deep Dive

How does a HashMap work?

HashMap Explained: A 10-Minute Crash Course

Keywords: HashMap, data structure, hash function, collision resolution, Java

When working with large datasets, efficient storage and retrieval are crucial for maintaining performance. In programming, one popular solution is the HashMap (or dictionary) – a data structure that associates keys with values using a hash function. But how does it work?

Intro

HashMaps are used extensively in software development to store and manage key-value pairs efficiently. Understanding how they operate can help you write more effective code. In this post, we'll dive into the basics of HashMaps and explore their inner workings.

Main Content

A HashMap is a collection that stores unique keys and associated values. The key feature (pun intended) is its use of a hash function to map keys to indices of a backing array. This allows for constant-time access to stored elements, making it an excellent choice for applications requiring fast lookups.

Here's how it works:

  • Hashing: When you insert or retrieve a key-value pair, the HashMap applies a hash function to the key. The resulting value is used as an index into the backing array.
  • Collision Resolution: What happens when two keys hash to the same index? This is known as a collision. To handle this, HashMaps typically use one of several strategies:
    • Chaining: Store colliding entries in a linked list at the corresponding index.
    • Open Addressing: Probe other locations in the array until an empty slot is found.

Let's explore an example using Java:

public class MyHashMap {
    private static final int INITIAL_CAPACITY = 16;
    private Entry[] table;

    public MyHashMap() {
        table = new Entry[INITIAL_CAPACITY];
    }

    public void put(String key, Integer value) {
        int index = hash(key);
        if (table[index] == null) {
            // Create a new entry at the specified index
            table[index] = new Entry(key, value);
        } else {
            // Handle collision using chaining or open addressing
            // ...
        }
    }

    private int hash(String key) {
        return key.hashCode() % INITIAL_CAPACITY;
    }

    private static class Entry {
        String key;
        Integer value;

        public Entry(String key, Integer value) {
            this.key = key;
            this.value = value;
        }
    }
}

TL;DR

In summary, HashMaps use a hash function to map keys to indices of an underlying array. When collisions occur, they employ various strategies like chaining or open addressing to handle the issue. By understanding how HashMaps work, you can write more efficient code and optimize your data storage needs.

I hope this 10-minute crash course on HashMaps has been informative!

Propagate this knowledge. Link copied automatically on click.
How does a HashMap work? - 10-Minute Engineering Brief | DevExCode | DevExCode