To optimize the use of HashMap, you can tune the initial capacity and load factor.
These 2 parameters affects the performance of HashMap.
Capacity is the number of buckets in the hash table.
Initial capacity is simply the capacity at the time the hash table is created.
Load factor is a measure of how full the 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 capacity is roughly doubled by calling the re-hash method.
Example : if load factor is .75 it will act to re-size the map once it filled 75%.
Constructing HashMap
HashMap (int initialCapacity)
Constructs empty HashMap with the specified initial capacity and default load factor 0.75
HashMap (int initialCapacity, float loadFactor)
Constructs empty HashMap with the specified initial capacity and the specified load factor.
Providing good values
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 (It will affect 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, to minimize the number of re-hash operations.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.