What attributes affects performance of HashMap?
Hashmap have two important attributes which affects its performance:
- Initial Capacity
- Load Factor
What is Initial Capacity of HashMap?
HashMap capacity is number of buckets in hashmap at given time. Initial capacity is the capacity of HashMap when it is created.
What is Load Factor?
Load Factor is a measure which defines how much HashMap should be full before the capacity of HashMap is automatically increased.
When is the capacity of HashMap increased?
When the capacity of HashMap exceeds the product of load factory and current capacity.
What happens when HashMap capacity is increased?
The Hashmap is rehashed. In re-hashing internal data structure of HashMap is rebuilt and keys are moved from one bucket to another, so that keys are equally distributed throughout hashmap.
What is the default load factor?
Default load factor is 0.75, and this value provides a good tradeoff between time and space costs.
How to avoid rehash operations?
If initial capacity is more than maximum number of entries divided by load factor.
Initial Capacity >= Maximum Size / Load Factor
What is the impact of higher values of Load Factor?
If Load Factor value is very high, then this will help in decreasing space overhead, but it will increase the lookup cost. As number of keys in single bucket will be more.