Saturday, November 10, 2012

Difference between HashMap and HashTable

The Following are the differences between HashMaps and HashTables

  • HashMap is unsynchronized and Hashtable is synchronized.Java 5 introduces ConcurrentHashMap which is an alternative of Hashtable and provides better scalability than Hashtable in Java.
  • HashMap allows null values as key and value whereas Hashtable doesnt allow. 
  • HashMap does not guarantee that the order of the map will remain constant over time. 
  • HashMaps have better performance for non threaded applications.
  • Another significant difference between HashMap vs Hashtable is that Iterator in the HashMap is a fail-fast iterator while the enumerator for the Hashtable is not and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator's own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort.


No comments:

Post a Comment