Tuesday, March 8, 2016

Java HashMap Useful information

HashMap permits null values and the null key.  
The HashMap class is roughly equivalent to Hashtable, except that it is
unsynchronized and permits nulls.
This class makes no guarantees as to the order of the map.
In particular, it does not guarantee that the order will remain constant over time.

HashTable has two important parameters which affects its performance
one is initial capacity and load factor.
Capacity is the number of buckets in the Hash Table.
Initial capacity is the capacity of hash table as the time of creation.

LoadFactor is the a measure of how full the hash table to be allowed before its capacity automatically increased.
Hash table rehashed when the number of entries in to the hash table exceeds the product of load factor and current capacity. Rehashed means internal data struture rebuilt.
So the hash table will have twice the number of buckets.

If many key-value mappings are to be stored in hashmap then creating it with a sufficient large capacity
will allow the mapping to be stored more efficiently than letting it perform automatic rehashing.

If many threads access the HashMap object at a single time then its a good practice to wrapp it using the Collections.synchronizedMap at the creating time.
Map mapObject = Collections.synchronizedMap(new HashMap(...));

No comments:

Post a Comment

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...