What is difference between HashMap and Hashtable classs


  1. Synchronization & Thread Safe - All methods in Hashtable are synchronized and thread safe whereas methods in HashMap are not synchronized and it is not thread safe.
    HashMap is more faster than Hashtable because its methods are not synchronized.
  2. Null Key & Null Value - HashMap allows one null key and any number of null values whereas null key and null value is not allowed in Hashtable. Hashtable's put method will throw NullPointerException if null is used as key or value
  3. Iterating the values - HashMap uses Iterator to iterate over the values whereas Hashtable uses Enumerator.
  4. Super class & Lagacy - Hashtable is subclass of Dictionary class which is now obsolete in JDK1.7. HashMap is sub class of AbstractMap
    Note :- It is better of externally synchronize a HashMap or Using concurrentMap implementation. (i.e. ConcurrentHashMap)

Comments

Popular posts from this blog

Data types and Literals in Java

How to define Auxiliary Constructor in case class in Scala

equals() method of Object class in Java