Sunday, August 19, 2012

5 difference between Hashtable and HashMap in Java

Hashtable vs HashMap in Java
Hashtable and HashMap are two hash based collection in Java and used to store objects as key value pair. Despite being hash based and similar in functionality there are significant difference between Hashtable and HashMap and without understanding those difference if you use Hashtable in place of HashMap than you may run into series of subtle programs which is hard to find and debug.Unlike Difference between ArrayList and HashMap,  Difference between Hashtable and HashMap are more subtle because both are similar kind of collection. Before seeing difference between HashMap and Hashtable let's see some common things between HashMap and Hashtable in Java.



Similarities between Hashtable and HashMap in Java
What is Difference between HashMap and Hashtable in Java CollectionThere are lot of similar things between Hashtable and HashMap in Java which is good to know and these also helps to find exactly what is different between HashMap and Hashtable in Java:


1) Both Hashtable and HashMap implements java.util.Map interface.
2) Hashtable and HashMap both are hash based collection and works on principle of hashing.
3) Hashtable and HashMap both provide constant time performance for put and get method if objects are distributed uniformly across bucket.
4) From JDK 4 both Hashtable and HashMap are part of Java collection framework.


Difference between Hashtable and HashMap in Java

Despite being so similar there are some differences between Hashtable and HashMap in Java which separates them completely, let's have a look :

1) First and most significant different between Hashtable and HashMap is that, HashMap is not thread-safe  while Hashtable is a thread-safe collection.

2) Second important difference between Hashtable and HashMap is performance, since HashMap is not synchronized it perform better than Hashtable.

3) Third difference on Hashtable vs HashMap is that Hashtable is obsolete class and you should be using ConcurrentHashMap in place of Hashtable in Java.

These were some important difference on Hashtable and HashMap in Java. If you know any other difference which is not included here than feel free to add them in comment section. Remember this is an important question on Java interview and good to prepare it well.

Other Java Collection article:


12 comments:

  1. HashMap permits null values in it, while Hashtable doesn't

    ReplyDelete
    Replies
    1. plus HashMap is slower than Hashtable because of the synchronoization factor.

      Delete
    2. Hi Anonymous HashMap is faster than HashTable -- HashMap is unsynchronized while hash table is synchronized

      Delete
  2. Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't

    ReplyDelete
  3. You can use LinkedHashMap with HashMap so you can have predictable iteration which is inseration order by default.

    ReplyDelete
  4. one common diff also is that hashtable not allowed any null keys or any null value where as hashmap can have oe null key and any more null values..

    ReplyDelete
  5. Nice ,
    But if given the thread safe example then it would be better to understand.

    ReplyDelete
  6. Hash table maintains order over a constant period of time whereas hash map doesnot mantian order

    ReplyDelete
  7. why hashtable not allowed any null keys or any null value where as hashmap can have oe null key and any more null values.

    ReplyDelete
  8. HashMap permits one null key and multiple null values.
    Hashtable doesn't permit any sort of nulls (key or values).

    ReplyDelete

Java67 Headline Animator