Tuesday, 28 February 2017

What is initial capacity and load factor?

Initial capacity is nothing but the number of buckets when the hash table is created, capacity is automatically increased when hash table get full of element.

Load factor is a measurement of how full collection (hash table) is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

How it affects performance?
As a general rule, the default load factor (.75) offers a good trade-off 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 expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

So finally we can conclude default load capacity (.75) used by Hash Map is a good value in most situations. We can set the initial capacity of Hash Map based on own knowledge of how many items it will hold.

Set it like, initial-capacity = number of items/.75 (round up).

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...