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?
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%.
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With