Difference between HashMap and Hashtable in Java
- HashMap allows null values as key and value whereas Hashtable doesn't allow nulls
- HashMap is non synchronized whereas Hashtable is synchronized.
- Java 5 introduces ConcurrentHashMap which is an alternative of Hashtable and provides better scalability than Hashtable in Java.
- Iterator in the HashMap is a fail-fast iterator while the enumerator for the Hashtable is not.
- HashMap 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.
- One more notable difference between Hashtable and HashMap is that because of thread-safety and synchronization Hashtable is much slower than HashMap if used in Single threaded environment.
- HashMap does not guarantee that the order of the map will remain constant over time.
No comments:
Post a Comment