Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if the load factor of a HashMap is greater than 1?

Default value of hashmap's load factor os 0.75f i.e it will re-hash the hash map once the 75% of capacity of hasmap is filled. What if I set the value of load factor greater than 1 for example lets say 2 (super(capacity+1, 2.0f, true);)

How it will work in sch case and how the hashing will work here?

like image 349
Samar Avatar asked Aug 14 '16 19:08

Samar


People also ask

What happens if load factor greater than 1?

A load factor greater than 1 will cause the stall speed to increase by a factor equal to the square root of the load factor. For example, if the load factor is 2, the stall speed will increase by about 40%.

Can load factor be greater than 1 hash table?

So the load factor is greater than 1 if there are more entries than buckets. Storing more than one entry in a bucket is very common. The most familiar example is separate chaining (e.g. a linked list of entries in the bucket), used for in-memory hash tables.

Why load factor is .75 in HashMap?

As a general rule, the default load factor (. 75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put).

What's the load factor of HashMap?

The meaning of operational complexity of O(1) means the retrieval and insertion operations take constant time. The default load factor of a HashMap is 0.75f.


1 Answers

What if I set the value of load factor greater than 1 for example lets say 2 (super(capacity+1, 2.0f, true);)

You already have the answer;

... it will re-hash the hash map once the 200% of capacity of hashmap is filled.

The hashing works the same, it just uses a smaller capacity which impacts performance. If you make the initial capacity large enough the load factor never comes into play. The load factor only applies when the map is resized.

Note: the actual capacity is always a power of 2.

I suggest you try it.

BTW Changing the load factor can change the order the elements appear as there is less buckets. Trying printing out the Set or Map and comparing.

like image 84
Peter Lawrey Avatar answered Oct 13 '22 22:10

Peter Lawrey