Friday, June 14, 2013

HashTable and HashMap


Basic Question on HashTable and HashMap


Difference between Hashtable and HashMap?

Both provide key-value access to data. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2. 

The key difference between the two is that access to the Hashtable is 
synchronized on the table while access to the HashMap isn't. You can add 
it, but it isn't there by default. 

Another difference is that iterator in the HashMap is fail-safe while the 
enumerator for the Hashtable isn't. If you change the map while iterating, 
you'll know. 


And, a third difference is that HashMap permits null values in it, while 
Hashtable doesn't. 


How we can synchronize HashMap? 

Collections.synchronizedMap (Map). 

ConcurrentModificationException

 It is not generally permssible for one thread to modify a Collection 
while another thread is iterating over it. In general, the results of the 
iteration are undefined under these circumstances


No comments:

Post a Comment