DEV Community

Cover image for Load Factor and Rehashing
Paul Ngugi
Paul Ngugi

Posted on

3

Load Factor and Rehashing

The load factor measures how full a hash table is. If the load factor is exceeded, increase the hash-table size and reload the entries into a new larger hash table. This is called rehashing. Load factor l (lambda) measures how full a hash table is. It is the ratio of the number of
elements to the size of the hash table, that is, l = n / N, where n denotes the number of elements and N the number of locations in the hash table.

Note that l is zero if the hash table is empty. For the open addressing scheme, l is between 0 and 1; l is 1 if the hash table is full. For the separate chaining scheme, l can be any value.

As l increases, the probability of a collision increases. Studies show that you should maintain the load factor under 0.5 for the open addressing scheme and under 0.9 for the separate chaining scheme.

Keeping the load factor under a certain threshold is important for the performance of hashing. In the implementation of the java.util.HashMap class in the Java API, the threshold 0.75 is used. Whenever the load factor exceeds the threshold, you need to increase the hashtable size and rehash all the entries in the map into a new larger hash table. Notice that you need to change the hash functions, since the hash-table size has been changed. To reduce the likelihood of rehashing, since it is costly, you should at least double the hash-table size. Even with periodic rehashing, hashing is an efficient implementation for map.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay