Logo/
mehadi

Shared by mehadi

Log in

Make a lecture like this from your own PDF

Drop a PDF, slides or notes here. You'll sign up, then yours starts.

0:00 / 0:07

0:00 / 0:07
Inside a Java HashMap
mehadi

Shared by mehadi

Summary

Think of a HashMap not as something mysterious, but simply as an array called a table that starts with 16 slots, which we call buckets. When you call put to store a key-value pair, Java doesn't just toss it anywhere. It takes your key, runs a hash function on it, and calculates exactly which bucket that data belongs in.

The reason HashMap is so fast—giving you O(1) performance—is that it doesn't need to search through the whole list to find your data. Because it calculates the bucket index mathematically, it can jump directly to the right spot in the array, just like looking up an item by its index number.

One clever trick Java uses is "spreading" the bits of the hash code. This ensures that the data is distributed evenly across all the buckets instead of getting bunched up in one place. By keeping everything spread out, the map stays organized and lightning-fast, no matter how much data you add.

Full transcript

0:00You've used HashMap a hundred times. But do you know what happens inside when you call put? Today I'm going to show you every single step visually. This is the video that will make HashMap crystal clear forever. You've used HashMap a hundred times. But do you know what happens inside when you call put? Today I'm going to show you every single step—visually, interactively. This is the video that will make HashMap crystal clear forever.

0:25A HashMap is not magic. It's just an array called table of type Node. Default size: 16 slots. Each slot is called a bucket. When you put a key-value pair, Java figures out which bucket to drop it into using the key's hash. When you get a value, it recomputes the same bucket index and finds your entry there. That's why get and put are O-1, it's just array access. You've used HashMap a hundred times. But do you know what happens inside? Today, I’ll show you exactly how it stores your data.

0:57A HashMap is not magic. It's just an array of Node objects, called table. By default, it has 16 slots, which we call buckets. When you put a key-value pair, Java calculates a hash, finds the bucket index, and drops it there. It's just simple array access, which is why HashMap gives you lightning-fast performance. But it doesn't use the hash code directly. It spreads bits to minimize collisions. This ensures your data stays distributed across the table, not bunched up in one place. At its core, a HashMap is an array called `table` of nodes. Default size: 16 slots. When you put a pair, it hashes your key to find a bucket index. That’s why access is O(1).

1:39A HashMap is an array of nodes, with a default size of 16. Each slot is called a bucket. When you put a key, Java calculates its index using the key's hash code. We jump directly to that bucket. Because it's direct array access, performance is constant, or O(1).

Ask AI about this video

Answers cite the source. Free account, no credit card.