What is difference between HashTable and HashMap.




  • HashMap works on the principal of hashing.
  • HashMap uses the hashCode() method to calculate a hash value. Hash value is calculated using the key object. This hash value is used to find the correct bucket where Entry object will be stored.
  • HashMap uses the equals() method to find the correct key whose value is to be retrieved in case of get() and to find if that key already exists or not in case of put().
  • Hashing collision means more than one key having the same hash value, in that case Entry objects are stored as a linked-list with in a same bucket.
  • With in a bucket values are stored as Entry objects which contain both key and value.
  • In Java 8 hash elements use balanced trees instead of linked lists after a certain threshold is reached while storing values. This improves the worst case performance from O(n) to O(log n).
      


         
           HashTable
       HashMap
    Methods of Hashtable are synchronized by default
    Methods are not synchronized by default
    Does not permit null values (trying to add, throws NullPointerException)
    Permits null values (either key or value)
    Enumeration interface is used for iterating keys
    Iterator is used for iterating the keys
    Enumerator is not fail-fast
    Iterator is fail-fast
    Safe for multithreaded applications
    Better for non-threaded applications
    Low performance due to synchronization
    High performance
    Hashtable is serialized
    HashMap is not serialized

    What is difference between HashTable and HashMap.   What  is   difference  between  HashTable  and HashMap.   Reviewed by Mukesh Jha on 3:12 AM Rating: 5

    No comments:

    Add your comment

    All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.