Sunday, May 5, 2019

HashMap Vs HashTable

Q- What is the difference between HashMap and HashTable in java?
Both the HashMap and Hashtable implement the interface java.util.Map but there are some differences which are mentioned below.

                                HashMap                                 Hashtable
     HashMap is non-synchronized     Hashtable is synchronized
    HashMap is not thread-safe.
    Whereas Hashmap is not thread-safe and
    it cannot be shared between threads without     synchronization.
    Map m = Collections.synchronizedMap(hashMap);
    Hashtable is thread-safe,
    and can be shared among multiple threads.
    As only one thread can access the Hashtable     at a time
    HashMap permits one null key and multiple null values.     Hashtable will not allow null key or value.
    HashMap is faster.     Hashtable is slower than HashMap due to     thread-safe.
    HashMap can be traversed by using the iterator.     Hashtable can be traversed by using     enumerator and iterator.
    HashMap inherits AbstractMap class.     Hashtable inherits Dictionary class.

No comments:

Post a Comment