Question

What if I know the number of elements I need to store and what if I dont?

Était-ce utile?

La solution

It depends. There's no hard and fast rule on the capacity of a hash table.

As a reference, the Java HashMap has a "load factor" of 0.75. This means once the number of elements reaches 75% of the map's capacity, the map will grow / resize internally (I believe to 2x the capacity). The Java HashMap uses chaining too.

If you used open addressing instead of chaining, you might want a load factor closer to 0.5 since buckets might fill faster.

If you know the number of elements, you can allocate the hash table appropriately initially and not worry about resizing it. If you don't know, you'll have to handle resizing as the table is filled.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top