Monday, 4 June 2012

Difference Between HashMap and HashTable ?

Difference Between HashMap and HashTable ?

HASH MAP
HASH Table

Both provide key-value access to data
Both provide key-value access to data

HashMap not synchronized
Hashtable is synchronized

iterator in the HashMap is fail-safe

Fail-safe is relevant from the context of iterators.If an iterator has been created on a collection object and some other thread tries to modify the collection object "structurally",a concurrent modification exception will be thrown

Synchronized means:- only one thread can modify a hash table at one point of time.
Basically, it means that any thread before performing an update on a hash table will have to acquire a lock on the object while others will wait for lock to be released.
you can make Hashmap as synchronize :
Map m = Collections.synchronizeMap(hashMap)


Since HashMap is not thread safe so it is faster than hashTable



HashMap:  if you are sure that this hashmap will not be accessed by multiple threads 

but if you want to multiple thread to access then use HashTable.

No comments:

Post a Comment