Thursday 12 July 2012

What is Load factor and initial capacity in Collection-Java


How  Load factor and initial capacity effects the collection performance

As  who are working in java environment well aware about the collection framework we have vast use of the collections so whenever we taking about Hash Map or Hash Set we heard two parameters that is load factor and initial capacity  let see what are these parameter, then will go how they effect the performance of collection.


What is initial capacity and load factor?

The 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 hash table is allowed or number of elements in the hash table. When the hash table gets full of element its capacity automatically gets increased.
Important thing is that hash table is rehashed or in simple words we can say the internal data structures are rebuilt when the number of entries in the hash table exceeds the product of LF and IC this all is performed by calling the rehash method and capacity will increase by twice.

How it effects performance: In collection for basic operation like get ,put implementation is like that it has constant time performance but when we talking about operation like Iteration time is proportional to  capacity of hash map  and the size of the hash map instance. That’s why if we want good performance over this we don’t expect to set initial capacity too high or load factor too low.
The default load factor is 0. 75 if we increase the value of LF it will decrease the space over head but also increase lookup cost.

Performance can be increased if we minimize the rehash operation because every time rebuilt a data structure will cause the performance overhead. so if our initial capacity is greater than maximum number of element or key-value pair divided by load factor rehash operation can be minimized .whenever we set initial capacity always keep load factor and number of key value pair in mind.
.

Conclusion: so finally we can conclude default load capacity (.75) used by Hash Map is a good value in most situations. set the initial capacity of your Hash Map based on your own knowledge of how many items it will hold - set it so that initial-capacity x .75 = number of items (round up).







6 comments: